def listenTwisted(self, extInit=lambda : None): from twisted.internet import reactor root = tResource.Resource() tStatic.loadMimeTypes() # load from /etc/mime.types # setup static file paths map(lambda x: root.putChild(x[0],tStatic.File(x[1])), self.staticPaths) # setup exportable interface print "publish",self.soapPath root.putChild(self.soapPath, self._makeWrapper(self.funcExports, "soap", tSoap.SOAPPublisher)) if self.xmlPath: print "publish",self.xmlPath root.putChild(self.xmlPath, self._makeWrapper(self.funcExports, "xmlrpc", tXmlrpc.XMLRPC)) map(lambda x: root.putChild(x[0],x[1]), self.customChildren) # init listening reactor.listenTCP(self.soapPort, tServer.Site(root)) log.debug("Starting SWAMP interface at: %s"% self.url) print "Starting SWAMP interface at: %s"% self.url extInit() subproc.usingTwisted = True reactor.run() pass
def loadMimeTypes(): # Add our own mime types to the ones parsed from /etc/mime.types d = static.loadMimeTypes() d['.flv'] = 'video/x-flv' d['.mp4'] = 'video/mp4' d['.webm'] = 'video/webm' return d
def loadMimeTypes(): # Add our own mime types to the ones parsed from /etc/mime.types d = static.loadMimeTypes() d[".flv"] = "video/x-flv" d[".mp4"] = "video/mp4" d[".webm"] = "video/webm" return d
def loadMimeTypes(): # Add our own mime types to the ones parsed from /etc/mime.types d = static.loadMimeTypes() d['.flv'] = 'video/x-flv' d['.mp4'] = 'video/mp4' d['.webm'] = 'video/webm' d['.ts'] = 'video/MP2T' d['.m3u8'] = 'application/vnd.apple.mpegurl' return d
def loadCompatibleMimeTypes(): # Read from Python's built-in mimetypes, but don't load any mimetypes # from disk. contentTypes = static.loadMimeTypes(mimetype_locations=()) contentTypes.update({ # Use text/javascript instead of application/javascript for # compatibility reasons. '.js': 'text/javascript', '.ico': 'image/x-icon', '.log': 'text/plain', '.rst': 'text/plain', # Office 2007 formats # http://technet.microsoft.com/en-us/library/ee309278%28office.12%29.aspx '.docx': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', '.docm': 'application/vnd.ms-word.document.macroEnabled.12', '.dotx': 'application/vnd.openxmlformats-officedocument.wordprocessingml.template', '.dotm': 'application/vnd.ms-word.template.macroEnabled.12', '.xlsx': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', '.xlsm': 'application/vnd.ms-excel.sheet.macroEnabled.12', '.xltx': 'application/vnd.openxmlformats-officedocument.spreadsheetml.template', '.xltm': 'application/vnd.ms-excel.template.macroEnabled.12', '.xlsb': 'application/vnd.ms-excel.sheet.binary.macroEnabled.12', '.xlam': 'application/vnd.ms-excel.addin.macroEnabled.12', '.pptx': 'application/vnd.openxmlformats-officedocument.presentationml.presentation', '.pptm': 'application/vnd.ms-powerpoint.presentation.macroEnabled.12', '.ppsx': 'application/vnd.openxmlformats-officedocument.presentationml.slideshow', '.ppsm': 'application/vnd.ms-powerpoint.slideshow.macroEnabled.12', '.potx': 'application/vnd.openxmlformats-officedocument.presentationml.template', '.potm': 'application/vnd.ms-powerpoint.template.macroEnabled.12', '.ppam': 'application/vnd.ms-powerpoint.addin.macroEnabled.12', '.sldx': 'application/vnd.openxmlformats-officedocument.presentationml.slide', '.sldm': 'application/vnd.ms-powerpoint.slide.macroEnabled.12', '.one': 'application/onenote', '.onetoc2': 'application/onenote', '.onetmp': 'application/onenote', '.onepkg': 'application/onenote', '.thmx': 'application/vnd.ms-officetheme', }) return contentTypes
# $Id: //depot/python/pymeds/pymeds-0.5/FileDIDL.py#1 $ # # Convert file information into a DIDL class. Dynamicly generate a new class # from a base class and the DIDL class to be determined. # __all__ = [ 'mimetoclass', 'buildClassMT', 'getClassMT', ] import os.path import weakref from DIDLLite import VideoItem, AudioItem, TextItem, ImageItem from twisted.python import log from twisted.web import static mimedict = static.loadMimeTypes() classdict = weakref.WeakValueDictionary() mimetoclass = { 'application/ogg': AudioItem, 'video': VideoItem, 'audio': AudioItem, 'text': TextItem, 'image': ImageItem, } def getClassMT(name, mimetype = None, fp = None): '''Return a tuple of the DIDLLite class and mimetype responsible for the named/mimetyped/fpd file.''' if mimetype is None: fn, ext = os.path.splitext(name) ext = ext.lower()
from twisted.plugin import IPlugin from twisted.web import static, server, resource from twisted.python import randbytes from sdfs.interfaces import IURIHandler, ICrontab from sdfs.exceptions import FileOrFolderNotFoundException from sdfs.resource import PluginResource from sdfs.site import predict_storage_path from sdfs.fs import TEMP_FOLDER from sdfs.utils import get_free_space RETRIEVE = 0 # download a file STORE = 1 # save files to a given folder URL_LIFETIME = timedelta(days=1) MIMETYPES = static.loadMimeTypes() class FilelikeObjectResource(static.File): isLeaf = True contentType = None fileObject = None encoding = 'bytes' def __init__(self, fileObject, size, contentType='bytes'): self.contentType = contentType self.fileObject = fileObject self.fileSize = size resource.Resource.__init__(self) def _setContentHeaders(self, request, size=None): if size is None:
class ZipArchiveResource(resource.Resource): """ Twisted Web resource """ log = make_logger() contentTypes = loadMimeTypes() # FIXME: https://github.com/crossbario/crossbar/issues/633 contentEncodings = {'.gz': 'gzip', '.bz2': 'bzip2', '.jgz': 'gzip'} def __init__(self, worker, config, path, archive_file): resource.Resource.__init__(self) self._worker = worker self._config = config self._path = path self._archive_file = archive_file self._origin = config.get('origin', None) self._cache = config.get('cache', False) self._default_object = config.get('default_object', None) self._object_prefix = config.get('object_prefix', None) if 'mime_types' in config: self.contentTypes.update(config['mime_types']) # now open ZIP archive from local file .. if os.path.exists(self._archive_file): self._archive = zipfile.ZipFile(self._archive_file) else: self._archive = None # setup map: filename -> cached content (bytes from file) if self._archive: self._zipfiles = {key: None for key in set(self._archive.namelist())} else: self._zipfiles = {} # setup fallback option self._default_file = self._config.get('options', {}).get('default_file') if self._default_file: self._default_file = self._default_file.encode('utf-8') self.log.info('ZipArchiveResource: {zlen} files in ZIP archive', zlen=len(self._zipfiles)) self.log.debug('ZipArchiveResource files: {filelist}', filelist=sorted(self._zipfiles.keys())) def getChild(self, path, request, retry=True): self.log.debug( 'ZipFileResource.getChild(path={path}, request={request}, prepath={prepath}, postpath={postpath})', path=path, prepath=request.prepath, postpath=request.postpath, request=request) search_path = b'/'.join([path] + request.postpath).decode('utf8') if (search_path == '' or search_path.endswith('/')) and self._default_object: search_path += self._default_object if self._object_prefix: search_path = os.path.join(self._object_prefix, search_path) self.log.debug('ZipArchiveResource.getChild - effective search path: "{}"'.format(search_path)) if search_path in self._zipfiles: # check cache data = self._zipfiles[search_path] # get data if not cached if not data: if self._archive: # open file within ZIP archive data = self._archive.open(search_path).read() if self._cache: self._zipfiles[search_path] = data self.log.debug('contents for file {search_path} from archive {archive_file} cached in memory', search_path=search_path, archive_file=self._archive_file) else: self.log.debug('contents for file {search_path} from archive {archive_file} read from file', search_path=search_path, archive_file=self._archive_file) else: self.log.debug('cache archive not loaded') return resource.NoResource() else: self.log.debug( 'cache hit: contents for file {search_path} from archive {archive_file} cached in memory', search_path=search_path, archive_file=self._archive_file) # file size file_size = len(data) fd = io.BytesIO(data) # guess MIME type from file extension _, ext = os.path.splitext(search_path) content_type = self.contentTypes.get(ext, None) # create and return resource that returns the file contents res = ZipFileResource(fd, file_size, content_type) return res else: if self._default_file and retry: return self.getChild(self._default_file, request, False) else: return resource.NoResource()
from RedirecToCurrentStream import RedirecToCurrentStreamResource from Plugins.Extensions.WebInterface.WebSocket.DreamboxWebSocketServer import webSocketServer from Tools.Directories import resolveFilename, SCOPE_MEDIA from External.__init__ import importExternalModules externalChildren = [] """ .htc Files for IE Fixes need a certain Content-Type """ import mimetypes mimetypes.add_type('text/x-component', '.htc') mimetypes.add_type('text/cache-manifest', '.appcache') mimetypes.add_type('video/MP2T', '.ts') static.File.contentTypes = static.loadMimeTypes() if hasattr(static.File, 'render_GET'): class File(static.File): def render_POST(self, request): return self.render_GET(request) else: File = static.File def addExternalChild(child): externalChildren.append(child) def getToplevel(session):
from PlayService import ServiceplayerResource from Uploader import UploadResource from ServiceListSave import ServiceList from RedirecToCurrentStream import RedirecToCurrentStreamResource from Tools.Directories import resolveFilename, SCOPE_MEDIA from External.__init__ import importExternalModules externalChildren = [] """ .htc Files for IE Fixes need a certain Content-Type """ import mimetypes mimetypes.add_type('text/x-component', '.htc') mimetypes.add_type('text/cache-manifest', '.appcache') static.File.contentTypes = static.loadMimeTypes() if hasattr(static.File, 'render_GET'): class File(static.File): def render_POST(self, request): return self.render_GET(request) else: File = static.File def addExternalChild(child): externalChildren.append(child) def getToplevel(session): root = File(util.sibpath(__file__, "web-data/tpl/default")) root.putChild("web", ScreenPage(session, util.sibpath(__file__, "web"), True) ) # "/web/*"
class FileSystemResource(Resource, filepath.FilePath): """ A file system resource. """ implements(IFileSystemResource) content_types = static.loadMimeTypes() content_encodings = {".gz": "gzip", ".bz2": "bzip2"} type = None def __init__(self, path, default_type="text/html", registry=None, processors={ '.epy': PythonScript, '.rpy': ResourceScript }, **kwargs): Resource.__init__(self, **kwargs) filepath.FilePath.__init__(self, path) # folder or file? self.restat() if self.isdir(): self.category = 'folder' self.is_leaf = False self.folderish = True else: self.category = 'file' self.is_leaf = True self.folderish = False # content type self.default_type = default_type # a registry for cached file based scripts self.registry = registry or static.Registry() # allowed processors self.processors = processors def getMetadata(self): self.restat() s = self.statinfo return { "size": s.st_size, "uid": s.st_uid, "gid": s.st_gid, "permissions": s.st_mode, "atime": s.st_atime, "mtime": s.st_mtime, "nlink": s.st_nlink } def getChild(self, id, request): # refresh file meta information self.restat() # are we a directory if not self.isdir(): raise ForbiddenError("Item %s is not a valid path" % self.path) # filepath needs to be a Unicode object id = id.decode('utf-8') # create new FilePath object fpath = self.child(id) # are we a valid FilePath if not fpath.exists(): raise NotFoundError("Item %s does not exists" % id) # any processors active ? proc = self.processors.get(fpath.splitext()[1].lower()) if proc: request.setHeader('content-type', 'text/html; charset=UTF-8') return proc(fpath.path, self.registry) return self._clone(fpath.path) def _clone(self, path): return self.__class__(path, default_type=self.default_type, registry=self.registry, processors=self.processors, hidden=self.hidden, public=self.public) def render_GET(self, request): #@UnusedVariable """ Returns either the content of the folder or the file object. """ if not self.exists(): raise NotFoundError("Item %s does not exists." % self.path) if self.isdir(): # return a dictionary of L{FileSystemResource} objects ids = sorted(self.listdir()) children = {} for id in ids: # IDs retrieved from listdir() are Unicode object safe_id = id.encode('UTF-8') children[safe_id] = self._clone(self.child(id).path) # add dynamic children children.update(self.children) return children elif self.isfile(): # return this L{FileSystemResource} object return self msg = "I don't know how to handle item %s." % self.path raise InternalServerError(msg)
from twisted.python.compat import intToBytes, networkString from twisted.internet import abstract, defer, task from twisted.internet.interfaces import IPushProducer from twisted.python import log, randbytes from twisted.web import http, resource, server, static from zope.interface import implementer from ..httpserver import ROOT_RESOURCE from ..plugin import OutputBase from ..txiobuffer import TwistedIOBuffer logger = logging.getLogger(__name__) URL_LIFETIME = timedelta(days=1) MIMETYPES = static.loadMimeTypes() class HttpOutput(OutputBase): plugin_name = 'http' def __init__(self, url_prefix='http'): self.filelist = {} self.url_prefix = url_prefix.strip('/') def start(self): self.cleanup_old_urls_loop = task.LoopingCall(self.cleanup_old_urls) self.cleanup_old_urls_loop.start(30 * 60) self.resource = FileServeResource() self.resource.filelist = self.filelist