12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- import os
- import re
- 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.FileOperations import crop_file
- class GetCroppedFile(View):
- """
- return the image crop defined by the result.
- """
- # 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, crop_box: 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 crop
- resolution = re.split(r'[^0-9]', resolution)
- max_width = int(resolution[0])
- max_height = int(resolution[1]) if len(resolution) > 1 else 2 ** 24
- crop_box = re.split(r'[^0-9.]', crop_box)
- crop_x = float(crop_box[0])
- crop_y = float(crop_box[1]) if len(crop_box) > 1 else 0
- crop_w = float(crop_box[2]) if len(crop_box) > 2 else 1 - crop_x
- crop_h = float(crop_box[3]) if len(crop_box) > 3 else 1 - crop_y
- # crop file
- file_directory, file_name = tpool.execute(crop_file, file, project.root_folder,
- crop_x, crop_y, crop_w, crop_h,
- max_width, max_height)
- # send to client
- return send_from_directory(file_directory, file_name)
|