Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

remote_files.py 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import urllib.parse
  2. import httpx
  3. from flask_restx import marshal_with, reqparse
  4. import services
  5. from controllers.common import helpers
  6. from controllers.common.errors import (
  7. FileTooLargeError,
  8. RemoteFileUploadError,
  9. UnsupportedFileTypeError,
  10. )
  11. from controllers.web import web_ns
  12. from controllers.web.wraps import WebApiResource
  13. from core.file import helpers as file_helpers
  14. from core.helper import ssrf_proxy
  15. from extensions.ext_database import db
  16. from fields.file_fields import build_file_with_signed_url_model, build_remote_file_info_model
  17. from services.file_service import FileService
  18. @web_ns.route("/remote-files/<path:url>")
  19. class RemoteFileInfoApi(WebApiResource):
  20. @web_ns.doc("get_remote_file_info")
  21. @web_ns.doc(description="Get information about a remote file")
  22. @web_ns.doc(
  23. responses={
  24. 200: "Remote file information retrieved successfully",
  25. 400: "Bad request - invalid URL",
  26. 404: "Remote file not found",
  27. 500: "Failed to fetch remote file",
  28. }
  29. )
  30. @marshal_with(build_remote_file_info_model(web_ns))
  31. def get(self, app_model, end_user, url):
  32. """Get information about a remote file.
  33. Retrieves basic information about a file located at a remote URL,
  34. including content type and content length.
  35. Args:
  36. app_model: The associated application model
  37. end_user: The end user making the request
  38. url: URL-encoded path to the remote file
  39. Returns:
  40. dict: Remote file information including type and length
  41. Raises:
  42. HTTPException: If the remote file cannot be accessed
  43. """
  44. decoded_url = urllib.parse.unquote(url)
  45. resp = ssrf_proxy.head(decoded_url)
  46. if resp.status_code != httpx.codes.OK:
  47. # failed back to get method
  48. resp = ssrf_proxy.get(decoded_url, timeout=3)
  49. resp.raise_for_status()
  50. return {
  51. "file_type": resp.headers.get("Content-Type", "application/octet-stream"),
  52. "file_length": int(resp.headers.get("Content-Length", -1)),
  53. }
  54. @web_ns.route("/remote-files/upload")
  55. class RemoteFileUploadApi(WebApiResource):
  56. @web_ns.doc("upload_remote_file")
  57. @web_ns.doc(description="Upload a file from a remote URL")
  58. @web_ns.doc(
  59. responses={
  60. 201: "Remote file uploaded successfully",
  61. 400: "Bad request - invalid URL or parameters",
  62. 413: "File too large",
  63. 415: "Unsupported file type",
  64. 500: "Failed to fetch remote file",
  65. }
  66. )
  67. @marshal_with(build_file_with_signed_url_model(web_ns))
  68. def post(self, app_model, end_user):
  69. """Upload a file from a remote URL.
  70. Downloads a file from the provided remote URL and uploads it
  71. to the platform storage for use in web applications.
  72. Args:
  73. app_model: The associated application model
  74. end_user: The end user making the request
  75. JSON Parameters:
  76. url: The remote URL to download the file from (required)
  77. Returns:
  78. dict: File information including ID, signed URL, and metadata
  79. int: HTTP status code 201 for success
  80. Raises:
  81. RemoteFileUploadError: Failed to fetch file from remote URL
  82. FileTooLargeError: File exceeds size limit
  83. UnsupportedFileTypeError: File type not supported
  84. """
  85. parser = reqparse.RequestParser()
  86. parser.add_argument("url", type=str, required=True, help="URL is required")
  87. args = parser.parse_args()
  88. url = args["url"]
  89. try:
  90. resp = ssrf_proxy.head(url=url)
  91. if resp.status_code != httpx.codes.OK:
  92. resp = ssrf_proxy.get(url=url, timeout=3, follow_redirects=True)
  93. if resp.status_code != httpx.codes.OK:
  94. raise RemoteFileUploadError(f"Failed to fetch file from {url}: {resp.text}")
  95. except httpx.RequestError as e:
  96. raise RemoteFileUploadError(f"Failed to fetch file from {url}: {str(e)}")
  97. file_info = helpers.guess_file_info_from_response(resp)
  98. if not FileService.is_file_size_within_limit(extension=file_info.extension, file_size=file_info.size):
  99. raise FileTooLargeError
  100. content = resp.content if resp.request.method == "GET" else ssrf_proxy.get(url).content
  101. try:
  102. upload_file = FileService(db.engine).upload_file(
  103. filename=file_info.filename,
  104. content=content,
  105. mimetype=file_info.mimetype,
  106. user=end_user,
  107. source_url=url,
  108. )
  109. except services.errors.file.FileTooLargeError as file_too_large_error:
  110. raise FileTooLargeError(file_too_large_error.description)
  111. except services.errors.file.UnsupportedFileTypeError:
  112. raise UnsupportedFileTypeError
  113. return {
  114. "id": upload_file.id,
  115. "name": upload_file.name,
  116. "size": upload_file.size,
  117. "extension": upload_file.extension,
  118. "url": file_helpers.get_signed_file_url(upload_file_id=upload_file.id),
  119. "mime_type": upload_file.mime_type,
  120. "created_by": upload_file.created_by,
  121. "created_at": upload_file.created_at,
  122. }, 201