def test_RestrictedNamedBlobFile(self): path = "%s/batchimport/toprocess/outgoing-mail/Accusé de réception.odt" % imiodmsmail.__path__[ 0] odtfile = file(path, 'rb') odtblob = NamedBlobFile(data=odtfile.read(), filename=u'file.odt') odtfile.close() path = "%s/configure.zcml" % imiodmsmail.__path__[0] otherfile = file(path, 'rb') otherblob = NamedBlobFile(data=otherfile.read(), filename=u'file.txt') otherfile.close() registry = getUtility(IRegistry) # check content type self.assertEqual(get_contenttype(odtblob), 'application/vnd.oasis.opendocument.text') self.assertEqual(get_contenttype(otherblob), 'text/plain') field = RestrictedNamedBlobFile() # with om context and good file field.context = get_object(oid='reponse1', ptype='dmsoutgoingmail')['1'] field._validate(odtblob) # with bad file self.assertRaises(Invalid, field._validate, otherblob) # bad file, validation deactivated registry[ 'imio.dms.mail.browser.settings.IImioDmsMailConfig.omail_odt_mainfile'] = False field._validate(otherblob)
def validateAudioType(value): if value is not None: mimetype = get_contenttype(value) if mimetype.split('/')[0] != 'audio': # If opus file permit it... if value.filename.split('.')[-1:][0] != 'opus' and get_contenttype(value) != 'application/octet-stream': raise InvalidAudioFile(mimetype)
def test_get_contenttype(self): self.assertEqual(get_contenttype(NamedImage(getFile('image.gif').read(), contentType='image/gif')), 'image/gif') self.assertEqual(get_contenttype(NamedImage(getFile('image.gif').read(), filename=u'image.gif')), 'image/gif') self.assertEqual(get_contenttype( NamedImage(getFile('notimage.doc').read(), filename=u'notimage.doc')), 'application/msword')
def test_get_contenttype(self): self.assertEqual( get_contenttype( NamedImage(getFile('image.gif').read(), contentType='image/gif')), 'image/gif') self.assertEqual( get_contenttype( NamedImage(getFile('image.gif').read(), filename=u'image.gif')), 'image/gif') self.assertEqual( get_contenttype( NamedImage(getFile('notimage.doc').read(), filename=u'notimage.doc')), 'application/msword')
def extractPackageContent(treeRoot, zip_blob): """ Extract package content into ZOB Tree """ zipfile = ZipFile(zip_blob.open('r')) parent_dict = {} for path in sorted(zipfile.namelist()): if path.endswith('/'): # create directory path = path[:-1] foldername = path.split(os.sep)[-1] parent_folder_name = '/'.join(path.split(os.sep)[:-1]) parent = parent_dict[ parent_folder_name] if parent_folder_name in parent_dict else treeRoot parent.insert(foldername, OOBTree()) parent_dict[path] = parent[foldername] else: # create file filename = path.split(os.sep)[-1] parent_folder_name = '/'.join(path.split(os.sep)[:-1]) parent = parent_dict[ parent_folder_name] if parent_folder_name in parent_dict else treeRoot data = zipfile.read(path) blob = BlobWrapper(get_contenttype(filename=filename)) file_obj = blob.getBlob().open('w') file_obj.write(data) file_obj.close() blob.setFilename(filename) parent.insert(filename, blob)
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 __init__(self, data='', contentType='', filename=None): if (filename is not None and contentType in ('', 'application/octet-stream')): contentType = get_contenttype(filename=filename) self.data = data self.contentType = contentType self.filename = filename
def facebook_metatags(self): mimetype = get_contenttype(self.context.image) for t in self.tags: if t.get('property') == 'og:image': t['content'] = self.image_url elif t.get('property') == 'og:image:type': t['content'] = mimetype
def __call__(self): """ Partially reproduced from plone.formwidget.namedfile.widget.Download. Leverages the existing BlobWrapper functionality to stream the media blobs to the client, allowing ranges and partial content. """ if self.context.ignoreContext: raise NotFound( "Cannot get the data file from a widget with no context") if self.context.form is not None: content = aq_inner(self.context.form.getContent()) else: content = aq_inner(self.context.context) field = aq_inner(self.context.field) dm = getMultiAdapter(( content, field, ), IDataManager) file_ = dm.get() if file_ is None: raise NotFound(self, self.request) content_type = get_contenttype(file_) blob_wrapper = BlobWrapper(content_type) blob_wrapper.setBlob(file_) return blob_wrapper.index_html(self.request)
def mimetype(self): "mimetype" tool = api.portal.get_tool("mimetypes_registry") mt = get_contenttype(self.context.file) types = tool.lookup(mt) if len(types) == 1: return types[0] else: return None
def __init__(self, data='', contentType='', filename=None): if ( filename is not None and contentType in ('', 'application/octet-stream') ): contentType = get_contenttype(filename=filename) self.data = data self.contentType = contentType self.filename = filename
def __init__(self, data='', contentType='', filename=None): if filename is not None and contentType in ('', 'application/octet-stream'): contentType = get_contenttype(filename=filename) self.contentType = contentType self._blob = Blob() f = self._blob.open('w') f.write('') f.close() self._setData(data) self.filename = filename
def _validate(self, value): super(RestrictedNamedBlobFile, self)._validate(value) if value is not None: registry = getUtility(IRegistry) # TODO add an option to permit pdf file if registry['imio.dms.mail.browser.settings.IImioDmsMailConfig.omail_odt_mainfile']: if (self.context.portal_type == 'dmsommainfile' and self.context.file.contentType != 'application/vnd.oasis.opendocument.text'): # we are editing the dmsmainfile, there was previously another type, we keep it ! # It's necessary to permit edition of a pdf scanned file return mimetype = get_contenttype(value) if mimetype != 'application/vnd.oasis.opendocument.text': raise Invalid(_('You can only upload ".odt" file (Libre Office format)'))
def set_attachment_content_disposition(request, filename, file=None): """ Set the content disposition on the request for the given browser """ if not filename: return if file: contenttype = get_contenttype(file) request.response.setHeader("Content-Type", contenttype) request.response.setHeader("Content-Length", file.getSize()) user_agent = request.get('HTTP_USER_AGENT', '') if 'MSIE' in user_agent: filename = quote(filename) request.response.setHeader( "Content-disposition", 'attachment; filename=%s' % filename) else: request.response.setHeader( "Content-disposition", 'attachment; filename="%s"' % filename)
def set_attachment_content_disposition(request, filename, file=None): """ Set the content disposition on the request for the given browser """ if not filename: return if file: contenttype = get_contenttype(file) request.response.setHeader("Content-Type", contenttype) request.response.setHeader("Content-Length", file.getSize()) user_agent = request.get('HTTP_USER_AGENT', '') if 'MSIE' in user_agent: filename = quote(filename) request.response.setHeader("Content-disposition", 'attachment; filename=%s' % filename) else: request.response.setHeader("Content-disposition", 'attachment; filename="%s"' % filename)
def __call__(self): if self.value is None: return mimetype = get_contenttype(self.value) if mimetype.split('/')[0] != 'image': raise InvalidImageFile(mimetype, self.field.__name__)
def __init__(self, data='', contentType='', filename=None): if filename is not None and contentType in ('', 'application/octet-stream'): contentType = get_contenttype(filename=filename) super(NamedBlobFile, self).__init__(data, contentType) self.filename = filename
def test_get_contenttype(self): self.assertEqual( get_contenttype( NamedImage(getFile('image.svg'), contentType='image/svg+xml')), 'image/svg+xml')
def validate_image_field(field, value): if value is not None: mimetype = get_contenttype(value) if mimetype.split('/')[0] != 'image': raise InvalidImageFile(mimetype, field.__name__)
def validateFileType(value): if value is not None: mimetype = get_contenttype(value) if mimetype != 'application/pdf': raise InvalidAnnexFile(mimetype)
def __init__(self, data="", contentType="", filename=None): if filename is not None and contentType in ("", "application/octet-stream"): contentType = get_contenttype(filename=filename) self.data = data self.contentType = contentType self.filename = filename