GetResizedFile.py 1.2 KB

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