def test_raw_read_file(self): import os.path from zope.component import provideAdapter from zope.publisher.browser import TestRequest from plone.resource.file import FilesystemFile from plone.resource.file import rawReadFile from zope.filerepresentation.interfaces import IRawReadFile provideAdapter(rawReadFile) name = 'test.html' path = os.path.join(os.path.dirname(__file__), 'resources', 'demo', 'foo', name) request = TestRequest() f = FilesystemFile(None, request, path, name) rf = IRawReadFile(f) self.assertTrue(isinstance(rf, file)) self.assertEqual(rf.read(), 'asdf') rf.close()
def manage_FTPget(self, REQUEST=None, RESPONSE=None): """Return the body of the content item in an FTP or WebDAV response. This adapts self to IRawReadFile(), which is then returned as an iterator. The adapter should provide IStreamIterator. """ reader = IRawReadFile(self, None) if reader is None: return '' request = REQUEST is not None and REQUEST or self.REQUEST response = RESPONSE is not None and RESPONSE or request.response mimeType = reader.mimeType encoding = reader.encoding if mimeType is not None: if encoding is not None: response.setHeader('Content-Type', '%s; charset="%s"' % (mimeType, encoding,)) else: response.setHeader('Content-Type', mimeType) size = reader.size() if size is not None: response.setHeader('Content-Length', str(size)) # if the reader is an iterator that the publisher can handle, return # it as-is. Otherwise, read the full contents if ((IInterface.providedBy(IStreamIterator) and IStreamIterator.providedBy(reader)) or (not IInterface.providedBy(IStreamIterator) and IStreamIterator.isImplementedBy(reader)) ): return reader else: return reader.read()
def content_type(self): """Return the content type (MIME type) of the tiem """ readFile = IRawReadFile(self, None) if readFile is None: return None return readFile.mimeType
def get_template(self, tile_id): tiles_directory = self.get_tile_directory() tile_folder = tiles_directory[tile_id] fi = tile_folder['template.html'] if fi.__class__.__name__ == "FilesystemFile": data = IRawReadFile(fi).read() else: data = str(fi.data) return ZopePageTemplate(tile_id, text=data)
def write_customization(self, filename): fi = self.jbot_directory[filename] filepath = self.get_filepath(filename) with open(filepath, 'wb') as tmpfi: if fi.__class__.__name__ == "FilesystemFile": data = IRawReadFile(fi).read() else: data = str(fi.data) tmpfi.write(data) return filepath
def manage_FTPget(self, REQUEST=None, RESPONSE=None): """Return the body of the content item in an FTP or WebDAV response. This adapts self to IRawReadFile(), which is then returned as an iterator. The adapter should provide IStreamIterator. """ reader = IRawReadFile(self, None) if reader is None: return '' request = REQUEST is not None and REQUEST or self.REQUEST response = RESPONSE is not None and RESPONSE or request.response mimeType = reader.mimeType encoding = reader.encoding if mimeType is not None: if encoding is not None: response.setHeader('Content-Type', '%s; charset="%s"' % ( mimeType, encoding, )) else: response.setHeader('Content-Type', mimeType) size = reader.size() if size is not None: response.setHeader('Content-Length', str(size)) # if the reader is an iterator that the publisher can handle, return # it as-is. Otherwise, read the full contents if ((IInterface.providedBy(IStreamIterator) and IStreamIterator.providedBy(reader)) or (not IInterface.providedBy(IStreamIterator) and IStreamIterator.isImplementedBy(reader))): return reader else: return reader.read()
def getFileFromDirectory(self, directory, filename): # noqa fi = directory[filename] path = self._get_fs_path() filepath = os.path.join(path, filename) if fi.__class__.__name__ == "FilesystemFile": last_modified = fi.lastModifiedTimestamp else: last_modified = fi._p_mtime if not os.path.exists(filepath) or \ DateTime(last_modified) > DateTime(os.stat(filepath).st_mtime): tmpfi = open(filepath, 'wb') if fi.__class__.__name__ == "FilesystemFile": data = IRawReadFile(fi).read() else: data = str(fi.data) tmpfi.write(data) tmpfi.close() return filepath
def content_type(self): # Return the content type (MIME type) of the item. readFile = IRawReadFile(self, None) if readFile is None: return None return readFile.mimeType
def read_file(self, fi): if fi.__class__.__name__ == "FilesystemFile": data = IRawReadFile(fi).read() else: data = str(fi.data) return data