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_extension_buffer_invalid(self):
buf = bytearray([0xFF, 0x00, 0x00, 0x00, 0x00])
ext = filetype.guess_extension(buf)
self.assertTrue(ext is None)
def test_guess_extension_file_path(self):
ext = filetype.guess_extension(FIXTURES + '/sample.jpg')
self.assertTrue(ext is not None)
self.assertEqual(ext, 'jpg')
def test_guess_extension_buffer(self):
buf = bytearray([0xFF, 0xD8, 0xFF, 0x00, 0x08])
ext = filetype.guess_extension(buf)
self.assertTrue(ext is not None)
self.assertEqual(ext, 'jpg')
def test_guess_extension_memoryview(self):
buf = memoryview(bytearray([0xFF, 0xD8, 0xFF, 0x00, 0x08]))
ext = filetype.guess_extension(buf)
self.assertTrue(ext is not None)
self.assertEqual(ext, 'jpg')
def upload_image():
_clear_imagemagick_temp_files()
if "file" not in request.files:
return jsonify(error="File is missing!"), 400
file = request.files["file"]
random_string = _get_random_filename()
tmp_filepath = os.path.join("/tmp/", random_string)
file.save(tmp_filepath)
output_type = settings.OUTPUT_TYPE or filetype.guess_extension(tmp_filepath)
error = None
output_filename = os.path.basename(tmp_filepath) + f".{output_type}"
output_path = os.path.join(settings.IMAGES_DIR, output_filename)
try:
with Image(filename=tmp_filepath) as img:
img.strip()
if output_type not in ["gif"]:
with img.sequence[0] as first_frame:
with Image(image=first_frame) as first_frame_img:
with first_frame_img.convert(output_type) as converted:
converted.save(filename=output_path)
else:
with img.convert(output_type) as converted:
converted.save(filename=output_path)