clean_file.py 622 B

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