clean_file.py 663 B

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