6
0

GetFile.py 870 B

1234567891011121314151617181920212223242526272829303132333435
  1. from os import path, getcwd
  2. from flask import abort, send_from_directory
  3. from flask.views import View
  4. from pycs.database.Database import Database
  5. class GetFile(View):
  6. """
  7. returns binary file data
  8. """
  9. # pylint: disable=arguments-differ
  10. methods = ['GET']
  11. def __init__(self, db: Database):
  12. # pylint: disable=invalid-name
  13. self.db = db
  14. def dispatch_request(self, file_id: int):
  15. # get file from database
  16. file = self.db.file(file_id)
  17. if file is None:
  18. return abort(404)
  19. # get absolute path
  20. if path.isabs(file.path):
  21. abs_file_path = file.path
  22. else:
  23. abs_file_path = path.join(getcwd(), file.path)
  24. # return data
  25. file_directory, file_name = path.split(abs_file_path)
  26. return send_from_directory(file_directory, file_name)