def guess_content_type(self, fd, query, inode_id): try: if query['hint']: content_type = query['hint'] except KeyError: m = Magic.MagicResolver() type, content_type = m.find_inode_magic(self.case, inode_id) return content_type
def GetContentType(self): """ This should produce the proper mime-type for this image class. The default implementation uses magic to determine the content type. This is sufficiently intelligent for most applications. The only reason you might want to override this is if the extra overhead of displaying the image twice is too prohibitive. """ import pyflag.Magic as Magic magic = Magic.MagicResolver() return magic.estimate_type(self.display(), None, None)
def execute(self): #Find the inode of the file: import pyflag.Magic as Magic m = Magic.MagicResolver() for path in self.args: type, mime = m.find_inode_magic(case=self.environment._CASE, urn=path) yield dict(type=type, mime=mime)
def execute(self): #Find the inode of the file: import pyflag.Magic as Magic m = Magic.MagicResolver() for inode_id in self.args: type, mime, scores = m.find_inode_magic( case=self.environment._CASE, inode_id=inode_id) yield scores yield dict(type=type, mime=mime)
def process(self, data, metadata=None): ScanIfType.process(self,data,metadata) if not self.boring_status and not self.filename: ## We need to find the name of the original uncompressed ## file so we can set a sensible VFS file name. This is ## the algorithm used: ## 1) We try to decompress the first data block from the file to see if the original name is in the header ## 2) Failing this we check if the inodes filename ends with .gz ## 3) Failing that, we call the new file "data" m = Magic.MagicResolver() magic, type_mime = m.find_inode_magic(self.case, inode_id=self.fd.inode_id, data=data[:1024])
def guess(self, fd, result, metadata): """ Uses fd to guess how suitable this filesystem driver is for this image """ if not "magic" in metadata: fd.seek(0) data = fd.read(10240) if data: import pyflag.Magic as Magic magic = Magic.MagicResolver() result.ruler() sig, ct = magic.get_type(data) result.row("Magic identifies this file as: %s" % sig, **{ 'colspan': 50, 'class': 'hilight' }) fd.close() metadata['magic'] = sig else: metadata['magic'] = '' return 10
def display(self, query, result): new_q = result.make_link(query, '') if not query.has_key('limit'): query['limit'] = 0 dbh = self.DBO(query['case']) fsfd = FileSystem.DBFS(query["case"]) ## If this is a directory, only show the stats fd = fsfd.open(inode_id=query['inode_id']) if not fd: return tmp = result.__class__(result) tmp.text(fd.urn) result.heading(tmp) try: m = Magic.MagicResolver() type, mime = m.find_inode_magic(query['case'], fd.inode_id) result.text("Classified as %s by magic" % type) except IOError, e: result.text("Unable to classify file, no blocks: %s" % e)
def __init__(self, fd, size_x): """ fd is the image, size_x is the requested width of the image. The height will be calculated to preserve aspect ratio """ self.size_x = size_x self.fd = fd self.width = 0 self.height = 0 ## Calculate the magic of this file: import pyflag.Magic as Magic magic = Magic.MagicResolver() self.magic, self.content_type = magic.find_inode_magic( fd.case, urn_id=fd.urn_id) ## Now use the magic to dispatch the correct handler: ## Use the content type to access the thumbnail try: method = getattr(self, self.dispatcher[self.content_type]) except KeyError, e: self.Unknown() return
def test01Magic(self): """ Test that common headers are correctly identified """ m = Magic.MagicResolver() for cls in Registry.MAGIC_HANDLERS.classes: print "\nTesting %s" % cls for sample_score, sample in cls.samples: print "Best match %s" % m.get_type(sample,None,None)[0] max_score, scores = m.estimate_type(sample, None, None) print "scores: " for k,v in scores.items(): if v>0: print " %s, %s (%s)" %( k.__class__, k.type_str(), v) self.assertEqual(max_score[1].__class__, cls, "Sample matched %s better than %s" % ( max_score[1].__class__, cls)) self.assertEqual(sample_score, max_score[0], "Unexpected score %s, expected %s" % ( max_score[0], sample_score) )
def process(self, data, metadata=None): ScanIfType.process(self,data,metadata) if not self.boring_status and not self.filename: ## We need to find the name of the original uncompressed ## file so we can set a sensible VFS file name. This is ## the algorithm used: ## 1) We try to decompress the first data block from the file to see if the original name is in the header ## 2) Failing this we check if the inodes filename ends with .gz ## 3) Failing that, we call the new file "data" m = Magic.MagicResolver() magic, type_mime = m.find_inode_magic(self.case, inode_id=self.fd.inode_id, data=data[:1024]) match = re.search(magic,'was "([^"]+)"') if match: self.filename = match.groups(1) return path, inode, inode_id = self.ddfs.lookup(inode=self.inode) original_filename = os.path.basename(path) if original_filename.endswith(".gz"): self.filename=original_filename[:-3] return self.filename="Uncompressed"
def add_type_info(self, inode_id): m = Magic.MagicResolver() m.find_inode_magic(case = self.fd.case, inode_id = inode_id)
def process(self, data, metadata=None): if self.type_str==None: m = Magic.MagicResolver() self.type_str, self.type_mime = m.cache_type(self.case, self.fd.inode_id, data[:1024]) metadata['mime'] = self.type_mime metadata['type'] = self.type_str
"select inode_id, scanner_cache from vfs where inode_id=%r limit 1", fd.inode_id) row = dbh.fetch() try: scanners_run = row['scanner_cache'].split(',') except: scanners_run = [] ## Force the scanners to run anyway if force: scanners_run = [] fd.inode_id = row['inode_id'] ## The new scanning framework is much simpler - we just call the ## scan() method on each factory. m = Magic.MagicResolver() type, mime, scores = m.find_inode_magic(case, fd.inode_id) for c in get_factories(scanners): if c.__class__.__name__ not in scanners_run: fd.seek(0) try: c.scan(fd, scanners=scanners, type=type, mime=mime, cookie=cookie, scores=scores) except Exception, e: print e #continue