import cv2 import os import re from PIL import Image from eventlet import tpool from flask import abort from flask import send_from_directory from flask.views import View from pycs.database.Database import Database from pycs.util import file_ops class GetResizedFile(View): """ returns binary file after resizing """ # pylint: disable=arguments-differ methods = ['GET'] def __init__(self, db: Database): # pylint: disable=invalid-name self.db = db def dispatch_request(self, file_id: int, resolution: str): # get file from database file = self.db.file(file_id) if file is None: return abort(404) project = file.project() if not os.path.exists(file.absolute_path): abort(404, "File not found!") # extract desired resolution resolution = re.split(r'[^0-9]', resolution) max_width = int(resolution[0]) max_height = int(resolution[1]) if len(resolution) > 1 else 2 ** 24 # send data file_directory, file_name = tpool.execute(file_ops.resize_file, file, project.root_folder, max_width, max_height) return send_from_directory(file_directory, file_name)