clean_file.py 584 B

12345678910111213141516171819202122
  1. import os
  2. from werkzeug.utils import secure_filename
  3. from . import alphagen as ag
  4. UPLOAD_FOLDER = 'app/static/incoming'
  5. ALLOWED_EXT = {'png', 'jpg', 'jpeg'}
  6. # Checks file for allowed extension
  7. def allowed_file(filename):
  8. return '.' in filename and \
  9. filename.rsplit('.', 1)[1].lower() in ALLOWED_EXT
  10. # And sanitizes
  11. def sanitize(filename):
  12. sfn = secure_filename(filename) # strips any slashes
  13. ssfn, fx = os.path.splitext(sfn) # ensures that internal filenames are not
  14. rsfn = ag.gen_alphanum() # known to users.
  15. ffn = f'{rsfn}{fx}'
  16. return ffn