Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

file.py 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. from flask import request
  2. from flask_login import current_user
  3. from flask_restful import Resource, marshal_with
  4. from werkzeug.exceptions import Forbidden
  5. import services
  6. from configs import dify_config
  7. from controllers.console import api
  8. from controllers.console.datasets.error import (
  9. FileTooLargeError,
  10. NoFileUploadedError,
  11. TooManyFilesError,
  12. UnsupportedFileTypeError,
  13. )
  14. from controllers.console.setup import setup_required
  15. from controllers.console.wraps import account_initialization_required, cloud_edition_billing_resource_check
  16. from fields.file_fields import file_fields, upload_config_fields
  17. from libs.login import login_required
  18. from services.file_service import ALLOWED_EXTENSIONS, UNSTRUCTURED_ALLOWED_EXTENSIONS, FileService
  19. PREVIEW_WORDS_LIMIT = 3000
  20. class FileApi(Resource):
  21. @setup_required
  22. @login_required
  23. @account_initialization_required
  24. @marshal_with(upload_config_fields)
  25. def get(self):
  26. file_size_limit = dify_config.UPLOAD_FILE_SIZE_LIMIT
  27. batch_count_limit = dify_config.UPLOAD_FILE_BATCH_LIMIT
  28. image_file_size_limit = dify_config.UPLOAD_IMAGE_FILE_SIZE_LIMIT
  29. return {
  30. "file_size_limit": file_size_limit,
  31. "batch_count_limit": batch_count_limit,
  32. "image_file_size_limit": image_file_size_limit,
  33. }, 200
  34. @setup_required
  35. @login_required
  36. @account_initialization_required
  37. @marshal_with(file_fields)
  38. @cloud_edition_billing_resource_check("documents")
  39. def post(self):
  40. if current_user.is_dataset_editor:
  41. raise Forbidden()
  42. # get file from request
  43. file = request.files["file"]
  44. # check file
  45. if "file" not in request.files:
  46. raise NoFileUploadedError()
  47. if len(request.files) > 1:
  48. raise TooManyFilesError()
  49. try:
  50. upload_file = FileService.upload_file(file, current_user)
  51. except services.errors.file.FileTooLargeError as file_too_large_error:
  52. raise FileTooLargeError(file_too_large_error.description)
  53. except services.errors.file.UnsupportedFileTypeError:
  54. raise UnsupportedFileTypeError()
  55. return upload_file, 201
  56. class FilePreviewApi(Resource):
  57. @setup_required
  58. @login_required
  59. @account_initialization_required
  60. def get(self, file_id):
  61. file_id = str(file_id)
  62. text = FileService.get_file_preview(file_id)
  63. return {"content": text}
  64. class FileSupportTypeApi(Resource):
  65. @setup_required
  66. @login_required
  67. @account_initialization_required
  68. def get(self):
  69. etl_type = dify_config.ETL_TYPE
  70. allowed_extensions = UNSTRUCTURED_ALLOWED_EXTENSIONS if etl_type == "Unstructured" else ALLOWED_EXTENSIONS
  71. return {"allowed_extensions": allowed_extensions}
  72. api.add_resource(FileApi, "/files/upload")
  73. api.add_resource(FilePreviewApi, "/files/<uuid:file_id>/preview")
  74. api.add_resource(FileSupportTypeApi, "/files/support-type")