Skip to content
This repository has been archived by the owner on Apr 5, 2022. It is now read-only.

Commit

Permalink
Fix Bandit B108: hardcoded_tmp_directory
Browse files Browse the repository at this point in the history
  • Loading branch information
BBaoVanC committed Jan 25, 2021
1 parent e0325f6 commit 5252417
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions imgupload.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import datetime
from pathlib import Path
from PIL import Image
import tempfile
import encoding

import settings # app settings (such as allowed extensions)
Expand Down Expand Up @@ -97,15 +98,16 @@ def upload():
print("Requested filename already exists!")
return jsonify({'status': 'error', 'error': 'FILENAME_TAKEN'}), status.HTTP_409_CONFLICT

f.save(f"/tmp/{fname}") # save the image temporarily (before removing EXIF)
with tempfile.TemporaryFile() as tmpf:
f.save(tmpf) # save the image temporarily (before removing EXIF)

image = Image.open(f"/tmp/{fname}")
data = list(image.getdata())
stripped = Image.new(image.mode, image.size)
stripped.putdata(data)
stripped.save(os.path.join(settings.UPLOAD_FOLDER, fname)) # save the image without EXIF
image = Image.open(tmpf)
data = list(image.getdata())
stripped = Image.new(image.mode, image.size)
stripped.putdata(data)
stripped.save(os.path.join(settings.UPLOAD_FOLDER, fname)) # save the image without EXIF

print(f"Saved to {fname}")
print(f"Saved to {fname}")

url = settings.ROOTURL + fname # construct the url to the image

Expand Down

0 comments on commit 5252417

Please sign in to comment.