Python magic.from_buffer() Examples
The following are 30
code examples of magic.from_buffer().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
magic
, or try the search function
.
Example #1
Source File: telegram.py From fishroom with GNU General Public License v3.0 | 8 votes |
def upload_audio(self, file_id, mime): if not self.file_store: return None, "No file store available" filedata = self.download_file(file_id) if filedata is None: return None, "teleboto Faild to download file" if mime is None: mime = magic.from_buffer(filedata, mime=True).decode('utf-8') ext = mimetypes.guess_extension(mime) if ext is None: raise Exception("Failed to guess ext from mime: %s" % mime) filename = "voice" + ext url = self.file_store.upload_file(filedata, filename, filetype="audio") if url is None: return None, "Failed to upload Document" return url, None
Example #2
Source File: static.py From mac-a-mal-cuckoo with MIT License | 6 votes |
def _get_filetype(self, data): """Gets filetype, uses libmagic if available. @param data: data to be analyzed. @return: file type or None. """ if not HAVE_MAGIC: return None try: ms = magic.open(magic.MAGIC_NONE) ms.load() file_type = ms.buffer(data) except: try: file_type = magic.from_buffer(data) except Exception: return None finally: try: ms.close() except: pass return file_type
Example #3
Source File: pArch.py From IntraArchiveDeduplicator with BSD 3-Clause "New" or "Revised" License | 6 votes |
def iterHashes(self): ''' Iterate over all the files in the archive, and yield 2-tuples containing the internal-path and the internal-item-data as a dict ''' items = list(self.getFileList()) items.sort() for item in items: if item not in self.hashedFiles: fp = self.open(item) cont = fp.read() ret = hf.getHashDict(item, cont) ret['cont'] = cont ret['type'] = fix_mime(magic.from_buffer(cont, mime=True)) self.hashedFiles[item] = ret yield item, self.hashedFiles[item]
Example #4
Source File: url.py From yeti with Apache License 2.0 | 6 votes |
def do_import(self, results, url): response = requests.get(url, proxies=yeti_config.proxy) content_type = magic.from_buffer(response.content, mime=True) if content_type == "text/html": import_html(results, response.content) self.save_as_pdf(results, url) else: target = AttachedFile.from_content( StringIO(response.content), url, content_type) results.investigation.update(import_document=target) try: method = ImportMethod.objects.get(acts_on=content_type) method.do_import(results, target.filepath) except: raise ValueError( "unsupported file type: '{}'".format(content_type))
Example #5
Source File: utils.py From CIRTKit with MIT License | 6 votes |
def get_type(data): try: ms = magic.open(magic.MAGIC_NONE) ms.load() file_type = ms.buffer(data) except: try: file_type = magic.from_buffer(data) except: return '' finally: try: ms.close() except: pass return file_type
Example #6
Source File: static.py From CuckooSploit with GNU General Public License v3.0 | 6 votes |
def _get_filetype(self, data): """Gets filetype, uses libmagic if available. @param data: data to be analyzed. @return: file type or None. """ if not HAVE_MAGIC: return None try: ms = magic.open(magic.MAGIC_NONE) ms.load() file_type = ms.buffer(data) except: try: file_type = magic.from_buffer(data) except Exception: return None finally: try: ms.close() except: pass return file_type
Example #7
Source File: diaphora_index_batch.py From maltindex with GNU General Public License v2.0 | 6 votes |
def run(self, directory): for root, dirs, files in os.walk(directory, followlinks=True): for name in files: filename = os.path.join(root, name) try: file_type = magic.from_buffer(open(filename).read(1024)) except: log("Error reading file %s: %s" % (filename, str(sys.exc_info()[1]))) continue if is_executable(file_type): md5_hash = md5(open(filename, "rb").read()).hexdigest() if not self.is_file_indexed(md5_hash): self.do_run(filename, file_type) else: log("File already indexed %s" % name) #-------------------------------------------------------------------------------
Example #8
Source File: pescanner.py From CapTipper with GNU General Public License v3.0 | 6 votes |
def get_filetype(data): """There are two versions of python-magic floating around, and annoyingly, the interface changed between versions, so we try one method and if it fails, then we try the other. NOTE: you may need to alter the magic_file for your system to point to the magic file.""" if sys.modules.has_key('magic'): try: ms = magic.open(magic.MAGIC_NONE) ms.load() return ms.buffer(data) except: try: return magic.from_buffer(data) except magic.MagicException: magic_custom = magic.Magic(magic_file='C:\windows\system32\magic') return magic_custom.from_buffer(data) return ''
Example #9
Source File: comment_parser.py From comment_parser with MIT License | 6 votes |
def extract_comments_from_str(code, mime=None): """Extracts and returns comments from the given source string. Args: code: String containing code to extract comments from. mime: Optional MIME type for code (str). Note some MIME types accepted don't comply with RFC2045. If not given, an attempt to deduce the MIME type will occur. Returns: Python list of parsers.common.Comment in the order that they appear in the source code. Raises: UnsupportedError: If code is of an unsupported MIME type. """ if not mime: mime = magic.from_buffer(code, mime=True) if isinstance(mime, bytes): mime = mime.decode('utf-8') if mime not in MIME_MAP: raise UnsupportedError('Unsupported MIME type %s' % mime) try: parser = MIME_MAP[mime] return parser.extract_comments(code) except common.Error as e: raise ParseError(str(e))
Example #10
Source File: utils.py From django-content-gallery with BSD 3-Clause "New" or "Revised" License | 5 votes |
def create_in_memory_image(image, name, size): """ Resizes the image and saves it as InMemoryUploadedFile object Returns the InMemoryUploadedFile object with the image data """ output = io.BytesIO() # create an io object # resize the image and save it to the io object image_resize(image, output, size) # get MIME type of the image mime = magic.from_buffer(output.getvalue(), mime=True) # create InMemoryUploadedFile using data from the io return uploadedfile.InMemoryUploadedFile(output, 'ImageField', name, mime, sys.getsizeof(output), None)
Example #11
Source File: matrix.py From mautrix-telegram with GNU Affero General Public License v3.0 | 5 votes |
def handle_matrix_avatar(self, sender: 'u.User', url: ContentURI, event_id: EventID ) -> None: if self.peer_type not in ("chat", "channel"): # Invalid peer type return elif self.avatar_url == url: return self.avatar_url = url file = await self.main_intent.download_media(url) mime = magic.from_buffer(file, mime=True) ext = sane_mimetypes.guess_extension(mime) uploaded = await sender.client.upload_file(file, file_name=f"avatar{ext}") photo = InputChatUploadedPhoto(file=uploaded) if self.peer_type == "chat": response = await sender.client(EditChatPhotoRequest(chat_id=self.tgid, photo=photo)) else: channel = await self.get_input_entity(sender) response = await sender.client(EditPhotoRequest(channel=channel, photo=photo)) self.dedup.register_outgoing_actions(response) for update in response.updates: is_photo_update = (isinstance(update, UpdateNewMessage) and isinstance(update.message, MessageService) and isinstance(update.message.action, MessageActionChatEditPhoto)) if is_photo_update: loc, size = self._get_largest_photo_size(update.message.action.photo) self.photo_id = f"{size.location.volume_id}-{size.location.local_id}" self.save() break await self._send_delivery_receipt(event_id) await self.update_bridge_info()
Example #12
Source File: Decoded.py From AIL-framework with GNU Affero General Public License v3.0 | 5 votes |
def get_file_mimetype(bytes_content): return magic.from_buffer(bytes_content, mime=True)
Example #13
Source File: mitm-colored-ads.py From ad-versarial with MIT License | 5 votes |
def _mime_sniff(content): return magic.from_buffer(content)
Example #14
Source File: InfoExtractor.py From codex-backend with MIT License | 5 votes |
def MIME_TYPE(data, mime=True): try: return magic.from_buffer(data, mime=mime) except magic.MagicException: return "none/none"
Example #15
Source File: Paste.py From AIL-framework with GNU Affero General Public License v3.0 | 5 votes |
def __init__(self, p_path): config_loader = ConfigLoader.ConfigLoader() self.cache = config_loader.get_redis_conn("Redis_Queues") self.store = config_loader.get_redis_conn("Redis_Data_Merging") self.store_metadata = config_loader.get_redis_conn("ARDB_Metadata") self.PASTES_FOLDER = os.path.join(os.environ['AIL_HOME'], config_loader.get_config_str("Directories", "pastes")) if self.PASTES_FOLDER not in p_path: self.p_rel_path = p_path self.p_path = os.path.join(self.PASTES_FOLDER, p_path) else: self.p_path = p_path self.p_rel_path = p_path.replace(self.PASTES_FOLDER+'/', '', 1) self.p_name = os.path.basename(self.p_path) self.p_size = round(os.path.getsize(self.p_path)/1024.0, 2) self.p_mime = magic.from_buffer("test", mime=True) self.p_mime = magic.from_buffer(self.get_p_content(), mime=True) # Assuming that the paste will alway be in a day folder which is itself # in a month folder which is itself in a year folder. # /year/month/day/paste.gz var = self.p_path.split('/') self.p_date = Date(var[-4], var[-3], var[-2]) self.p_date_path = os.path.join(var[-4], var[-3], var[-2], self.p_name) self.p_source = var[-5] self.supposed_url = 'https://{}/{}'.format(self.p_source.replace('_pro', ''), var[-1].split('.gz')[0]) self.p_encoding = None self.p_hash_kind = {} self.p_hash = {} self.p_langage = None self.p_nb_lines = None self.p_max_length_line = None self.array_line_above_threshold = None self.p_duplicate = None self.p_tags = None
Example #16
Source File: pescanner.py From codex-backend with MIT License | 5 votes |
def get_filetype(data): """There are two versions of python-magic floating around, and annoyingly, the interface changed between versions, so we try one method and if it fails, then we try the other""" if sys.modules.has_key('magic'): try: ms = magic.open(magic.MAGIC_NONE) ms.load() return ms.buffer(data) except: return magic.from_buffer(data)
Example #17
Source File: forms.py From sample-platform with ISC License | 5 votes |
def validate_file(form, field) -> None: """ Validate sample being uploaded. :param form: form data :type form: UploadForm :param field: field to validate :type field: form field :raises ValidationError: when extension is not allowed :raises ValidationError: when mimetype is not allowed :raises ValidationError: when extension not provided and not supported """ # File cannot end with a forbidden extension filename, file_extension = os.path.splitext(field.data.filename) if len(file_extension) > 0: forbidden_ext = ForbiddenExtension.query.filter(ForbiddenExtension.extension == file_extension[1:]).first() if forbidden_ext is not None: raise ValidationError('Extension not allowed') mimetype = magic.from_buffer(field.data.read(1024), mime=True) # File Pointer returns to beginning field.data.seek(0, 0) # Check for permitted mimetype forbidden_mime = ForbiddenMimeType.query.filter(ForbiddenMimeType.mimetype == mimetype).first() if forbidden_mime is not None: raise ValidationError('File MimeType not allowed') extension = mimetypes.guess_extension(mimetype) if extension is not None: forbidden_real = ForbiddenExtension.query.filter(ForbiddenExtension.extension == extension[1:]).first() if forbidden_real is not None: raise ValidationError('Extension not allowed')
Example #18
Source File: pArch.py From IntraArchiveDeduplicator with BSD 3-Clause "New" or "Revised" License | 5 votes |
def getHashInfo(self, intPath): ''' Get the info-dict for internal item `intPath` ''' if intPath not in self.hashedFiles: fp = self.open(intPath) cont = fp.read() ret = hf.getHashDict(intPath, cont) ret['cont'] = cont ret['type'] = fix_mime(magic.from_buffer(cont, mime=True)) self.hashedFiles[intPath] = ret return self.hashedFiles[intPath]
Example #19
Source File: validators.py From django-cloudinary-storage with MIT License | 5 votes |
def validate_video(value): if os.name == 'nt': magic_object = magic.Magic(magic_file='magic', mime=True) mime = magic_object.from_buffer(value.file.read(1024)) else: mime = magic.from_buffer(value.file.read(1024), mime=True) value.file.seek(0) if not mime.startswith('video/'): raise ValidationError(_(app_settings.INVALID_VIDEO_ERROR_MESSAGE))
Example #20
Source File: geodata_download_importer.py From actinia_core with GNU General Public License v3.0 | 5 votes |
def _check_urls(self): """Check the urls for access and supported mimetypes. If all files are already in the download cache, then nothing needs to be downloaded and checked. """ for url in self.url_list: # Send a resource update if self.send_resource_update is not None: self.send_resource_update(message="Checking access to URL: %s" % url) # Check if thr URL exists by investigating the HTTP header resp = requests.head(url) if self.message_logger: self.message_logger.info("%i %s %s" % (resp.status_code, resp.text, resp.headers)) if resp.status_code != 200: raise AsyncProcessError("The URL <%s> can not be accessed." % url) # Download 256 bytes from the url and check its mimetype response = urlopen(url) mime_type = magic.from_buffer(response.read(256), mime=True).lower() if self.message_logger: self.message_logger.info(mime_type) if mime_type not in SUPPORTED_MIMETYPES: raise AsyncProcessError("Mimetype <%s> of url <%s> is not supported. " "Supported mimetypes are: %s" % (mime_type, url, ",".join(SUPPORTED_MIMETYPES))) self.detected_mime_types.append(mime_type)
Example #21
Source File: utils.py From allura with Apache License 2.0 | 5 votes |
def is_text_file(file): msg = magic.from_buffer(file[:1024]) if ("text" in msg) or ("empty" in msg): return True return False
Example #22
Source File: settings.py From helfertool with GNU Affero General Public License v3.0 | 5 votes |
def clean_latex_template(self): file = self.cleaned_data['latex_template'] # check mimetype with libmagic filemime = magic.from_buffer(file.read(1024), mime=True) if filemime != 'text/x-tex': raise ValidationError(_("File does not contain LaTeX code.")) # seek to begin (may be necessary for further use?) file.seek(0) return file
Example #23
Source File: utils.py From helfertool with GNU Affero General Public License v3.0 | 5 votes |
def is_image(file): filemime = magic.from_buffer(file.read(), mime=True) file.seek(0) return filemime.startswith('image/')
Example #24
Source File: test_binary_service.py From FACT_core with GNU General Public License v3.0 | 5 votes |
def test_get_repacked_binary_and_file_name(self): tar, file_name = self.binary_service.get_repacked_binary_and_file_name(TEST_FW.uid) self.assertEqual(file_name, '{}.tar.gz'.format(TEST_FW.file_name), 'file_name not correct') file_type = magic.from_buffer(tar, mime=False) assert 'gzip compressed data' in file_type, 'Result is not an tar.gz file'
Example #25
Source File: test_tar_repack.py From FACT_core with GNU General Public License v3.0 | 5 votes |
def test_tar_repack(self): file_path = os.path.join(get_test_data_dir(), 'container/test.zip') result = self.repack_service.tar_repack(file_path) file_type = magic.from_buffer(result, mime=False) assert 'gzip compressed data' in file_type, 'Result is not an tar.gz file'
Example #26
Source File: submission.py From ijust_server with GNU General Public License v3.0 | 5 votes |
def validate_file(self): data = self.code.data.read(16) self.code.data.seek(0) #if not magic.from_buffer(data, mime=True) in self.allowed_extensions: # return False #return True return magic.from_buffer(data, mime=True).startswith('text/')
Example #27
Source File: problem.py From ijust_server with GNU General Public License v3.0 | 5 votes |
def validate_file(self): data = self.body.data.read(16) self.body.data.seek(0) if not magic.from_buffer(data, mime=True) in self.allowed_extensions: return False return True
Example #28
Source File: problem.py From ijust_server with GNU General Public License v3.0 | 5 votes |
def validate_file(self): data = self.testcase.data.read(16) self.testcase.data.seek(0) if not magic.from_buffer(data, mime=True) in self.allowed_extensions: return False return True
Example #29
Source File: portal.py From mautrix-facebook with GNU Affero General Public License v3.0 | 5 votes |
def _handle_matrix_media(self, sender: 'u.User', message: MediaMessageEventContent) -> Optional[str]: if message.file and decrypt_attachment: data = await self.main_intent.download_media(message.file.url) data = decrypt_attachment(data, message.file.key.key, message.file.hashes.get("sha256"), message.file.iv) elif message.url: data = await self.main_intent.download_media(message.url) else: return None mime = message.info.mimetype or magic.from_buffer(data, mime=True) files = await sender.client.upload([(message.body, data, mime)]) return await self.thread_for(sender).send_files(files)
Example #30
Source File: utils.py From Servo with BSD 2-Clause "Simplified" License | 5 votes |
def file_type(buf): import magic return magic.from_buffer(buf, mime=True)