From aa33852879882a029e6d59aeebf8f852207fec7c Mon Sep 17 00:00:00 2001 From: John-Mark Gurney Date: Sun, 2 Jul 2006 13:06:46 -0800 Subject: [PATCH] if an Object has a content attribute, install it into the webserver so that data can be accessed.. This will give the ability for custom objects to add their data instead of depending upon the FS... the url may need to be set after object creation, instead of having it in Object, as we don't generate the name, the cd does... slight layering violation, but it works.. [git-p4: depot-paths = "//depot/": change = 801] --- ContentDirectory.py | 8 ++++++++ DIDLLite.py | 6 +++++- FSStorage.py | 16 ++++++---------- pymediaserv | 5 ++++- 4 files changed, 23 insertions(+), 12 deletions(-) diff --git a/ContentDirectory.py b/ContentDirectory.py index 697afaa..925f17d 100644 --- a/ContentDirectory.py +++ b/ContentDirectory.py @@ -32,6 +32,8 @@ from urllib import quote class ContentDirectoryControl(UPnPPublisher, dict): """This class implements the CDS actions over SOAP.""" + urlbase = property(lambda x: x._urlbase) + def getnextID(self): ret = str(self.nextID) self.nextID += 1 @@ -43,9 +45,12 @@ class ContentDirectoryControl(UPnPPublisher, dict): return ret def addItem(self, parent, klass, *args, **kwargs): + '''Takes an optional arg, content, which is a twisted.web.resource.Resource that this item provides.''' assert isinstance(self[parent], Container) nid = self.getnextID() i = klass(self, nid, parent, *args, **kwargs) + if hasattr(i, 'content'): + self.webbase.putChild(nid, i.content) self.children[parent].append(i) self[i.id] = i return i.id @@ -66,6 +71,9 @@ class ContentDirectoryControl(UPnPPublisher, dict): def __init__(self, title, *args, **kwargs): super(ContentDirectoryControl, self).__init__(*args) + self.webbase = kwargs['webbase'] + self._urlbase = kwargs['urlbase'] + del kwargs['webbase'], kwargs['urlbase'] fakeparent = '-1' self.nextID = 0 self.children = { fakeparent: []} diff --git a/DIDLLite.py b/DIDLLite.py index 314dacf..2be120c 100644 --- a/DIDLLite.py +++ b/DIDLLite.py @@ -35,9 +35,10 @@ class Object: creator = None res = None writeStatus = None + content = property(lambda x: x._content) def __init__(self, cd, id, parentID, title, restricted = False, - creator = None): + creator = None, **kwargs): self.cd = cd self.id = id @@ -50,6 +51,9 @@ class Object: else: self.restricted = '0' + if kwargs.has_key('content'): + self._content = kwargs['content'] + def checkUpdate(self): return self diff --git a/FSStorage.py b/FSStorage.py index 419157f..207e668 100644 --- a/FSStorage.py +++ b/FSStorage.py @@ -52,12 +52,11 @@ class FSItem(FSObject, Item): def __init__(self, *args, **kwargs): FSObject.__init__(self, kwargs['path']) del kwargs['path'] - urlbase = kwargs['urlbase'] - del kwargs['urlbase'] mimetype = kwargs['mimetype'] del kwargs['mimetype'] + kwargs['content'] = static.File(self.FSpath) Item.__init__(self, *args, **kwargs) - self.url = '%s%s' % (urlbase, self.FSpath) + self.url = '%s/%s' % (self.cd.urlbase, self.id) self.mimetype = mimetype def doUpdate(self): @@ -85,7 +84,7 @@ mimetoklass = { 'image': FSImageItem, } -def dofileadd(cd, parent, urlbase, path, name): +def dofileadd(cd, parent, path, name): fn, ext = os.path.splitext(name) ext = ext.lower() try: @@ -103,21 +102,18 @@ def dofileadd(cd, parent, urlbase, path, name): log.msg('no item for mt: %s' % mt) return - return cd.addItem(parent, klass, name, urlbase = urlbase, + return cd.addItem(parent, klass, name, path = os.path.join(path, name), mimetype = mt) class FSDirectory(FSObject, StorageFolder): def __init__(self, *args, **kwargs): path = kwargs['path'] del kwargs['path'] - urlbase = kwargs['urlbase'] - del kwargs['urlbase'] StorageFolder.__init__(self, *args, **kwargs) FSObject.__init__(self, path) # mapping from path to objectID self.pathObjmap = {} - self.urlbase = urlbase def doUpdate(self): # We need to rescan this dir, and see if our children has @@ -137,10 +133,10 @@ class FSDirectory(FSObject, StorageFolder): # new object if os.path.isdir(fname): # new dir - nf = self.cd.addContainer(self.id, i, klass = FSDirectory, path = fname, urlbase = self.urlbase) + nf = self.cd.addContainer(self.id, i, klass = FSDirectory, path = fname) elif os.path.isfile(fname): # new file - nf = dofileadd(self.cd, self.id, self.urlbase, self.FSpath, i) + nf = dofileadd(self.cd, self.id, self.FSpath, i) else: nf = None log.msg('skipping: %s' % fname) diff --git a/pymediaserv b/pymediaserv index c3c275c..14fe97c 100755 --- a/pymediaserv +++ b/pymediaserv @@ -65,11 +65,13 @@ class RootDevice(static.Data): static.Data.__init__(self, d, 'text/xml') root = WebServer() -cds = ContentDirectoryServer('My Media Server', klass = FSDirectory, path = 'media', urlbase = urlbase) # This sets up the root to be the media dir so we don't have to enumerate the directory +content = resource.Resource() +cds = ContentDirectoryServer('My Media Server', klass = FSDirectory, path = 'media', urlbase = os.path.join(urlbase, 'content'), webbase = content) # This sets up the root to be the media dir so we don't have to enumerate the directory root.putChild('ContentDirectory', cds) cds = cds.control root.putChild('ConnectionManager', ConnectionManagerServer()) root.putChild('root-device.xml', RootDevice()) +root.putChild('content', content) # Area of server to serve media files from @@ -83,6 +85,7 @@ medianode.contentTypes.update( { '.ts': 'video/mpeg', # we may want this instead of mp2t #'.mp4': 'video/mp4', '.mp4': 'video/mpeg', + '.dat': 'video/mpeg', # VCD tracks '.ogm': 'application/ogg', '.vob': 'video/mpeg', #'.m4a': 'audio/mp4', # D-Link can't seem to play AAC files.