Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_guess_memoryview(self):
buf = memoryview(bytearray([0xFF, 0xD8, 0xFF, 0x00, 0x08]))
kind = filetype.guess(buf)
self.assertTrue(kind is not None)
self.assertEqual(kind.mime, 'image/jpeg')
self.assertEqual(kind.extension, 'jpg')
def __call__(self, parser, namespace, values, option_string=None):
# Check if the given path is a file
if not os.path.isfile(values):
print('ERROR: {} is not a file.'.format(values))
sys.exit(1)
# Check if the given file is a native terraform plan file
given_file = filetype.guess(values)
if given_file is not None:
terraform_executable = getattr(namespace, 'terraform_file', None)
values = convert_terraform_plan_to_json(os.path.abspath(values), terraform_executable)
# Check if the given file is a json file.
try:
with open(values, 'r', encoding='utf-8') as plan_file:
data = json.load(plan_file)
except json.decoder.JSONDecodeError:
print('ERROR: {} is not a valid JSON file'.format(values))
sys.exit(1)
except UnicodeDecodeError:
print('ERROR: {} is not a valid JSON file.'.format(values))
print(' Did you try to convert the binary plan file to json with '
with urllib.request.urlopen(request) as url, open(
input_file_path, mode="wb"
) as input_file_fh:
input_file_fh.write(url.read())
except ssl.SSLError:
raise Exception(f"Could not download {path}")
except Exception as e:
raise e
else:
input_file_path = path
file_extension = path.replace("?dl=1", "").split(".")[-1]
# Many links from file sharing services are not informative and don't have
# extensions, so we could try to guess.
if file_extension not in ALLOWED_TYPES:
detected = filetype.guess(input_file_path)
if detected is not None:
file_extension = detected.mime.split("/")[-1]
if file_extension not in ALLOWED_TYPES:
print(
f"Could not determine file extension for {anthology_id} at {path}",
file=sys.stderr,
)
with open(input_file_path, "rb") as f:
checksum = compute_hash(f.read())
# Update XML
xml_file = os.path.join(
os.path.dirname(sys.argv[0]), "..", "data", "xml", f"{collection_id}.xml"
)
tree = ET.parse(xml_file)
def main():
kind = filetype.guess('tests/fixtures/sample.jpg')
if kind is None:
print('Cannot guess file type!')
return
print('File extension: %s' % kind.extension)
print('File MIME type: %s' % kind.mime)
def get_filetype(file_path):
file_type = filetype.guess(file_path)
if file_type:
return file_type.mime
else:
return "text/html"
def get_filetype(fpath: str) -> str:
"""
Returns a string with the extension of the library received.
Raises:
UnsupportedFileType: if the file type is not supported by iLEAPP
or cannot be guessed.
Leverages the `filetype` library:
https://github.com/h2non/filetype.py
"""
if not os.path.isdir(fpath):
try:
inferred_filetype = filetype.guess(fpath)
except Exception as e:
raise e
if inferred_filetype is None:
raise UnsupportedFileType(f"Could not detect file type for {fpath}")
extension = inferred_filetype.extension
if extension not in SUPPORTED_EXTENSIONS:
raise UnsupportedFileType(
f"Detected file type {extension} for file {fpath} not supported"
f" by iLEAPP.\nFile types supported are: {SUPPORTED_EXTENSIONS}"
)
return extension
def main():
buf = bytearray([0xFF, 0xD8, 0xFF, 0x00, 0x08])
kind = filetype.guess(buf)
if kind is None:
print('Cannot guess file type!')
return
print('File extension: %s' % kind.extension)
print('File MIME type: %s' % kind.mime)
def check_type(self):
self.media_ext = filetype.guess(self.media_path).extension
def main():
f = open('tests/fixtures/sample.jpg', 'rb')
data = f.read()
kind = filetype.guess(data)
if kind is None:
print('Cannot guess file type!')
return
print('File extension: %s' % kind.extension)
print('File MIME type: %s' % kind.mime)