6
0

GetResizedFile.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import os
  2. import re
  3. from eventlet import tpool
  4. from flask import abort
  5. from flask import send_from_directory
  6. from flask.views import View
  7. from pycs.database.File import File
  8. from pycs.util.FileOperations import resize_file
  9. class GetResizedFile(View):
  10. """
  11. returns binary file after resizing
  12. """
  13. # pylint: disable=arguments-differ
  14. methods = ['GET']
  15. def dispatch_request(self, file_id: int, resolution: str):
  16. # get file from database
  17. file = File.get_or_404(file_id)
  18. project = file.project
  19. if not os.path.exists(file.absolute_path):
  20. abort(404, "File not found!")
  21. # extract desired resolution
  22. resolution = re.split(r'[^0-9]', resolution)
  23. max_width = int(resolution[0])
  24. max_height = int(resolution[1]) if len(resolution) > 1 else 2 ** 24
  25. # resize file
  26. file_directory, file_name = tpool.execute(resize_file, file, project.root_folder,
  27. max_width, max_height)
  28. # send to client
  29. return send_from_directory(file_directory, file_name)