32
32
from coherence.upnp.services.servers.media_receiver_registrar_server import MediaReceiverRegistrarServer
33
33
from coherence.upnp.services.servers.media_receiver_registrar_server import FakeMediaReceiverRegistrarBackend
35
from coherence.upnp.devices.basics import BasicAVMixin
37
39
from coherence import log
39
COVER_REQUEST_INDICATOR = re.compile(".*cover\.[A-Z|a-z]{3,4}$")
41
COVER_REQUEST_INDICATOR = re.compile(".*?cover\.[A-Z|a-z]{3,4}$")
43
ATTACHMENT_REQUEST_INDICATOR = re.compile(".*?attachment=.*$")
41
45
class MSRoot(resource.Resource, log.Loggable):
42
46
logCategory = 'mediaserver'
53
57
headers = request.getAllHeaders()
54
58
self.msg( request.getAllHeaders())
56
if(request.method == 'GET' and
57
COVER_REQUEST_INDICATOR.match(request.uri)):
58
self.info("request cover for id %s" % path)
59
ch = self.store.get_by_id(path)
61
request.setResponseCode(200)
63
if os.path.exists(file):
64
self.info("got cover %s" % file)
65
return StaticFile(file)
66
request.setResponseCode(404)
67
return static.Data('<html><p>cover requested not found</p></html>','text/html')
60
if request.method == 'GET':
61
if COVER_REQUEST_INDICATOR.match(request.uri):
62
self.info("request cover for id %s" % path)
63
ch = self.store.get_by_id(path)
65
request.setResponseCode(200)
67
if os.path.exists(file):
68
self.info("got cover %s" % file)
69
return StaticFile(file)
70
request.setResponseCode(404)
71
return static.Data('<html><p>cover requested not found</p></html>','text/html')
73
if ATTACHMENT_REQUEST_INDICATOR.match(request.uri):
74
self.info("request attachment %r for id %s" % (request.args,path))
75
ch = self.store.get_by_id(path)
77
return ch.item.attachments[request.args['attachment'][0]]
79
request.setResponseCode(404)
80
return static.Data('<html><p>the requested attachment was not found</p></html>','text/html')
69
82
if(request.method == 'POST' and
70
83
request.uri.endswith('?import')):
86
99
return self.getChild(path, request)
88
def requestFinished(self, result, id):
101
def requestFinished(self, result, id, request):
89
102
self.info("finished, remove %d from connection table" % id)
103
self.info("finished, sentLength: %d chunked: %d code: %d" % (request.sentLength, request.chunked, request.code))
104
self.info("finished %r" % request.headers)
90
105
self.server.connection_manager_server.remove_connection(id)
92
107
def import_file(self,name,request):
137
152
self.info("startup, add %d to connection table" % new_id)
138
153
d = request.notifyFinish()
139
d.addCallback(self.requestFinished, new_id)
140
d.addErrback(self.requestFinished, new_id)
154
d.addBoth(self.requestFinished, new_id, request)
155
request.setHeader('transferMode.dlna.org', 'Streaming')
156
if hasattr(ch.item, 'res'):
157
if ch.item.res[0].protocolInfo is not None:
158
_,_,_,additional_info = ch.item.res[0].protocolInfo.split(':')
159
if additional_info != '*':
160
request.setHeader('contentFeatures.dlna.org', additional_info)
141
161
return ch.location
143
163
p = ch.get_path()
154
174
self.info("startup, add %d to connection table" % new_id)
155
175
d = request.notifyFinish()
156
d.addCallback(self.requestFinished, new_id)
157
d.addErrback(self.requestFinished, new_id)
176
d.addBoth(self.requestFinished, new_id, request)
177
request.setHeader('transferMode.dlna.org', 'Streaming')
178
if hasattr(ch, 'item') and hasattr(ch.item, 'res'):
179
if ch.item.res[0].protocolInfo is not None:
180
_,_,_,additional_info = ch.item.res[0].protocolInfo.split(':')
181
if additional_info != '*':
182
request.setHeader('contentFeatures.dlna.org', additional_info)
158
183
ch = StaticFile(p.encode('utf-8'))
160
185
self.debug("accessing path %r failed" % p)
324
349
self.xml = """<?xml version="1.0" encoding="utf-8"?>""" + ET.tostring( root, encoding='utf-8')
325
350
static.Data.__init__(self, self.xml, 'text/xml')
327
class MediaServer(log.Loggable):
352
class MediaServer(log.Loggable,BasicAVMixin):
328
353
logCategory = 'mediaserver'
330
355
def __init__(self, coherence, backend, **kwargs):
331
356
self.coherence = coherence
332
357
self.device_type = 'MediaServer'
333
self.version = int(kwargs.get('version',2))
358
self.version = int(kwargs.get('version',self.coherence.config.get('version',2)))
336
361
self.uuid = kwargs['uuid']
338
363
from coherence.upnp.core.uuid import UUID
339
364
self.uuid = UUID()
341
366
self.backend = None
342
367
urlbase = self.coherence.urlbase
343
368
if urlbase[-1] != '/':
449
474
self.info("%s MediaServer (%s) activated" % (self.backend.name, self.backend))
453
s = self.coherence.ssdp_server
454
uuid = str(self.uuid)
455
host = self.coherence.hostname
456
self.msg('%s register' % self.device_type)
457
# we need to do this after the children are there, since we send notifies
459
'%s::upnp:rootdevice' % uuid,
461
self.coherence.urlbase + uuid[5:] + '/' + 'description-%d.xml' % self.version,
467
self.coherence.urlbase + uuid[5:] + '/' + 'description-%d.xml' % self.version,
470
version = self.version
472
if version == self.version:
477
'%s::urn:schemas-upnp-org:device:%s:%d' % (uuid, self.device_type, version),
478
'urn:schemas-upnp-org:device:%s:%d' % (self.device_type, version),
479
self.coherence.urlbase + uuid[5:] + '/' + 'description-%d.xml' % version,
483
for service in self._services:
485
if hasattr(service,'version'):
486
if service.version < version:
488
elif service.version == version:
491
namespace = service.namespace
493
namespace = 'schemas-upnp-org'
496
'%s::urn:%s:service:%s:%d' % (uuid,namespace,service.id, version),
497
'urn:%s:service:%s:%d' % (namespace,service.id, version),
498
self.coherence.urlbase + uuid[5:] + '/' + 'description-%d.xml' % version,
504
def unregister(self):
505
s = self.coherence.ssdp_server
506
uuid = str(self.uuid)
507
self.coherence.remove_web_resource(uuid[5:])
509
version = self.version
511
s.doByebye('%s::urn:schemas-upnp-org:device:%s:%d' % (uuid, self.device_type, version))
512
for service in self._services:
513
if hasattr(service,'version') and service.version < version:
516
namespace = service.namespace
517
except AttributeError:
518
namespace = 'schemas-upnp-org'
519
s.doByebye('%s::urn:%s:service:%s:%d' % (uuid,namespace,service.id, version))
524
s.doByebye('%s::upnp:rootdevice' % uuid)
b'\\ No newline at end of file'