def classify(self, data, mimetype=None, filename=None): """Classify works as follows: 1) you tell me the rfc-2046 name and I give you an IMimetype object 2) the filename includes an extension from which we can guess the mimetype 3) we can optionally introspect the data 4) default to self.defaultMimetype if no data was provided else to application/octet-stream of no filename was provided, else to text/plain Return an IMimetype object or None """ mt = None if mimetype: mt = self.lookup(mimetype) if mt: mt = mt[0] elif filename: mt = self.lookupExtension(filename) if mt is None: mt = self.globFilename(filename) if data and not mt: for c in self._classifiers(): if c.classify(data): mt = c break if not mt: mstr = magic.guessMime(data) if mstr: _mt = self.lookup(mstr) if len(_mt) > 0: mt = _mt[0] if not mt: if not data: mtlist = self.lookup(self.defaultMimetype) elif filename: mtlist = self.lookup('application/octet-stream') else: failed = 'text/x-unknown-content-type' filename = filename or '' data = data or '' if six.PY3: data = data.encode() ct, enc = guess_content_type(filename, data, None) if ct == failed: ct = 'text/plain' mtlist = self.lookup(ct) if len(mtlist) > 0: mt = mtlist[0] else: return None # Remove acquisition wrappers return aq_base(mt)
def classify(self, data, mimetype=None, filename=None): """Classify works as follows: 1) you tell me the rfc-2046 name and I give you an IMimetype object 2) the filename includes an extension from which we can guess the mimetype 3) we can optionally introspect the data 4) default to self.defaultMimetype if no data was provided else to application/octet-stream of no filename was provided, else to text/plain Return an IMimetype object or None """ mt = None if mimetype: mt = self.lookup(mimetype) if mt: mt = mt[0] elif filename: mt = self.lookupExtension(filename) if mt is None: mt = self.globFilename(filename) if data and not mt: for c in self._classifiers(): if c.classify(data): mt = c break if not mt: mstr = magic.guessMime(data) if mstr: _mt = self.lookup(mstr) if len(_mt) > 0: mt = _mt[0] if not mt: if not data: mtlist = self.lookup(self.defaultMimetype) elif filename: mtlist = self.lookup('application/octet-stream') else: failed = 'text/x-unknown-content-type' filename = filename or '' data = data or '' ct, enc = guess_content_type(filename, data, None) if ct == failed: ct = 'text/plain' mtlist = self.lookup(ct) if len(mtlist) > 0: mt = mtlist[0] else: return None # Remove acquisition wrappers return aq_base(mt)
def portrait(self): """Provides a single location to pull the user's portrait from.""" member_portrait = self.viewedmember().getPortrait() if member_portrait: data = member_portrait.data else: file = open(self.context.restrictedTraverse(self.defaultPortraitURL).context.path, 'rb') data = file.read() file.close() content_type = magic.guessMime(data) self.response.setHeader('Content-Type', content_type) return data
def update_to_25(context): return "" site = context.getSite() catalog = site.portal_catalog query = {'portal_type': [ 'eea.climateadapt.aceproject', 'eea.climateadapt.adaptationoption', 'eea.climateadapt.casestudy', 'eea.climateadapt.guidancedocument', 'eea.climateadapt.indicator', 'eea.climateadapt.informationportal', 'eea.climateadapt.mapgraphdataset', 'eea.climateadapt.organisation', 'eea.climateadapt.publicationreport', 'eea.climateadapt.researchproject', 'eea.climateadapt.tool' ]} results = catalog.searchResults(**query) extension = { 'application/pdf': '.pdf', 'application/zip': '.zip', 'image/jpeg': '.jpg', } for b in results: items = b.getObject().contentValues() for item in items: if item.portal_type == 'File': filex = item.file filetype = guessMime(filex._data) if filetype is not None: if filex.filename.find(extension[filetype]) == -1: filex.contentType = filetype filex.filename = filex.filename.replace( '.', extension[filetype]) filex._p_changed = True logger.info("Fixing file: %s", filex.filename) logger.info("URL: %s", item.absolute_url()) else: logger.info('Not Fixed:') logger.info("Type: %s", filetype) logger.info("URL: %s", item.absolute_url()) logger.info("Item: %s", item)
def update_to_25(context): return "" site = context.getSite() catalog = site.portal_catalog query = { 'portal_type': [ 'eea.climateadapt.aceproject', 'eea.climateadapt.adaptationoption', 'eea.climateadapt.casestudy', 'eea.climateadapt.guidancedocument', 'eea.climateadapt.indicator', 'eea.climateadapt.informationportal', 'eea.climateadapt.mapgraphdataset', 'eea.climateadapt.organisation', 'eea.climateadapt.publicationreport', 'eea.climateadapt.researchproject', 'eea.climateadapt.tool' ] } results = catalog.searchResults(**query) extension = { 'application/pdf': '.pdf', 'application/zip': '.zip', 'image/jpeg': '.jpg', } for b in results: items = b.getObject().contentValues() for item in items: if item.portal_type == 'File': filex = item.file filetype = guessMime(filex._data) if filetype is not None: if filex.filename.find(extension[filetype]) == -1: filex.contentType = filetype filex.filename = filex.filename.replace( '.', extension[filetype]) filex._p_changed = True logger.info("Fixing file: %s", filex.filename) logger.info("URL: %s", item.absolute_url()) else: logger.info('Not Fixed:') logger.info("Type: %s", filetype) logger.info("URL: %s", item.absolute_url()) logger.info("Item: %s", item)
def test_guessMime(self): for filename, expected in samplefiles: file = open(input_file_path(filename)) data = file.read() file.close() # use method direct got = guessMime(data) self.failUnlessEqual(got, expected) # use mtr-tool got_from_tool = self.registry.classify(data) self.failUnlessEqual(got_from_tool, expected) # now cut it to the first 8k if greater if len(data) > 8192: data = data[:8192] got_cutted = self.registry.classify(data) self.failUnlessEqual(got_cutted, expected)
def test_guessMime(self): for filename, expected in samplefiles: file = open(input_file_path(filename), 'rb') data = file.read() file.close() # use method direct got = guessMime(data) self.assertEqual(got, expected) # use mtr-tool got_from_tool = self.registry.classify(data) self.assertTrue(isinstance(got_from_tool, MimeTypeItem)) self.assertEqual(str(got_from_tool), expected) # now cut it to the first 8k if greater if len(data) > 8192: data = data[:8192] got_cutted = self.registry.classify(data) self.assertTrue(isinstance(got_from_tool, MimeTypeItem)) self.assertEqual(str(got_cutted), expected)
def _portrait_thumb(self, thumbnail_property, default_thumb='defaultPortraitThumbURL'): """Provides a single location to pull the user's portrait from. Same as above, but returns the thumbnail.""" member_portrait_thumb = getattr(self.viewedmember().aq_inner, thumbnail_property, None) if member_portrait_thumb is not None: data = str(member_portrait_thumb.data) modified = member_portrait_thumb.bobobase_modification_time() content_type = member_portrait_thumb.content_type else: default_thumb = getattr(self, default_thumb) path = self.context.restrictedTraverse(default_thumb).context.path file = open(path, 'rb') modified = DateTime.DateTime(os.path.getmtime(path)) data = file.read() content_type = magic.guessMime(data) file.close() self.response.setHeader('Content-Type', content_type) modified = modified.toZone('GMT') self.response.setHeader('Last-Modified', modified.rfc822()) # DateTime can be added to a number of days (not seconds!) self.response.setHeader('Expires', (DateTime.DateTime() + 1).rfc822()) return data