GetResizedFile.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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.Database import Database
  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 __init__(self, db: Database):
  16. # pylint: disable=invalid-name
  17. self.db = db
  18. def dispatch_request(self, file_id: int, resolution: str):
  19. # get file from database
  20. file = self.db.file(file_id)
  21. if file is None:
  22. return abort(404)
  23. project = file.project()
  24. if not os.path.exists(file.absolute_path):
  25. abort(404, "File not found!")
  26. # extract desired resolution
  27. resolution = re.split(r'[^0-9]', resolution)
  28. max_width = int(resolution[0])
  29. max_height = int(resolution[1]) if len(resolution) > 1 else 2 ** 24
  30. # send data
  31. file_directory, file_name = tpool.execute(resize_file, file,
  32. project.root_folder, max_width, max_height)
  33. return send_from_directory(file_directory, file_name)