def ensure_correct_content_type(self, brain): """Make sure that the contentType of a mail's message is what would be returned by `get_contenttype`. """ if self.has_expected_content_type(brain): return mail = brain.getObject() message = getattr(mail, 'message', None) if not message: return # can't be paranoid enough sometimes ... if not INamedBlobFile.providedBy(message): return if not getattr(message, 'filename', None): return content_type = get_contenttype(filename=message.filename) if not content_type: return message.contentType = content_type # we're actually interested in updating metadata only, but since we # can't just chose a cheap index to achieve that. mail.reindexObject(idxs=['id'])
def add(self, attributes): conn = self.manager.connection if conn is None: return try: info = IPrimaryFieldInfo(self.context, None) except TypeError: info = None if info is not None and INamedBlobFile.providedBy(info.value): blob = info.value._blob else: return super(DexterityItemIndexHandler, self).add(attributes) if attributes and 'SearchableText' not in attributes: return super(DexterityItemIndexHandler, self).add(attributes) schema = self.manager.schema if not schema: logger.warning( 'Unable to fetch schema, skipping indexing of %r', self.context) return if attributes is None: attributes = schema.fields.keys() attributes.remove('SearchableText') data = self.get_data(attributes) conn.extract(blob, data)
def get_file_tuple(self, named_file, path_prefix): path = u'{0}/{1}'.format(safe_unicode(path_prefix), safe_unicode(named_file.filename)) if HAVE_BLOBS and INamedBlobFile.providedBy(named_file): return (path, named_file.open()) else: stream_data = StringIO(named_file.data) return (path, stream_data)
def serve_file(self): file = self._getFile() self.set_headers(file) if not INamedBlobFile.providedBy(file): return super(Download, self).__call__() request_range = handleRequestRange(self.context, file.getSize(), self.request, self.request.response) return BlobStreamIterator(file._blob, **request_range)
def test_namedblobfile_setting(self): data = 'datattatatata' filename = FILENAME_TESTSTRING + u".xlsx" content_type = 'application/vnd.ms-excel' fileSetter = self.getFileSetter(ISchemaWithNamedBlobFile) fileSetter.set(data, filename.encode('utf-8'), content_type) field = fileSetter.context.field self.assertTrue(INamedBlobFile.providedBy(field)) self.assertEqual(field.filename, filename) self.assertEqual(field.contentType, content_type) self.assertEqual(field.data, data)
def get_file(blob): zodb_blob = None if HAS_NAMEDFILE and INamedBlobFile.providedBy(blob) and hasattr(blob, '_blob'): # HACK: uses internal knowledge but INamedBlobFile provides no way method # to get to the underlying blob zodb_blob = blob._blob elif IBlob.providedBy(blob): zodb_blob = blob if zodb_blob is None: return False else: file_path = zodb_blob.committed() return file_path
def test_file_content_is_migrated(self): from plone.app.contenttypes.migration.migration import FileMigrator from plone.namedfile.interfaces import INamedBlobFile from Products.ATContentTypes.content.file import ATFile at_file = self.createATCTobject(ATFile, 'file') field = at_file.getField('file') field.set(at_file, 'dummydata') field.setFilename(at_file, 'dummyfile.txt') field.setContentType(at_file, 'text/dummy') migrator = self.get_migrator(at_file, FileMigrator) migrator.migrate() new_file = self.portal['file'] self.assertTrue(INamedBlobFile.providedBy(new_file.file)) self.assertEqual(new_file.file.filename, 'dummyfile.txt') self.assertEqual(new_file.file.contentType, 'text/dummy') self.assertEqual(new_file.file.data, 'dummydata')
def test_blob_file_content_is_migrated(self): from plone.app.contenttypes.migration.migration import BlobFileMigrator from plone.namedfile.interfaces import INamedBlobFile self.portal.invokeFactory('File', 'file') at_file = self.portal['file'] at_file.setFile('dummydata', mimetype="text/dummy", filename='dummyfile.txt') applyProfile(self.portal, 'plone.app.contenttypes:default') migrator = self.get_migrator(at_file, BlobFileMigrator) migrator.migrate() dx_file = self.portal['file'] self.assertTrue(INamedBlobFile.providedBy(dx_file.file)) self.assertEqual(dx_file.file.filename, 'dummyfile.txt') self.assertEqual(dx_file.file.contentType, 'text/dummy') self.assertEqual(dx_file.file.data, 'dummydata')
def _getFields(self, ignore_empty=True): """ Get an iterable of the file-like fields for context """ result = [] unwrapped = self.context.aq_base if hasattr(unwrapped, 'Schema'): schema = unwrapped.Schema() for field in schema.fields(): if field.type in ['file', 'blob']: result.append({ 'name': field.getName(), 'context_uid': self.context.UID(), 'mimetype': field.getContentType(self.context), 'size': field.get_size(self.context), 'filename': field.getFilename(self.context), }) else: try: from plone.dexterity.interfaces import IDexterityFTI from plone.namedfile.interfaces import INamedBlobFile except ImportError: return result # could be dexterity schema = getUtility(IDexterityFTI, name=unwrapped.portal_type).lookupSchema() for name in (schema or []): field = getattr(unwrapped, name) if not INamedBlobFile.providedBy(field): continue result.append({ 'name': name, 'context_uid': self.context.UID(), 'mimetype': field.contentType, 'size': field.size, 'filename': field.filename, }) if ignore_empty: result = [x for x in result if x['size'] > 0] return result
def testInterface(self): self.assertTrue(INamedBlobImage.implementedBy(NamedBlobImage)) self.assertTrue(verifyClass(INamedBlobImage, NamedBlobImage)) self.assertTrue(INamedBlobFile.implementedBy(NamedBlobImage)) self.assertTrue(INamedBlobImage.implementedBy(NamedBlobImage)) self.assertTrue(verifyClass(INamedBlobFile, NamedBlobImage))
def testInterface(self): self.failUnless(INamedBlobImage.implementedBy(NamedBlobImage)) self.failUnless(verifyClass(INamedBlobImage, NamedBlobImage)) self.failUnless(INamedBlobFile.implementedBy(NamedBlobImage)) self.failUnless(INamedBlobImage.implementedBy(NamedBlobImage)) self.failUnless(verifyClass(INamedBlobFile, NamedBlobImage))