2024-08-15 09:17:36 +08:00
|
|
|
#
|
2026-03-24 19:24:41 +08:00
|
|
|
# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
|
2024-08-15 09:17:36 +08:00
|
|
|
#
|
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
|
#
|
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
#
|
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
|
# limitations under the License
|
|
|
|
|
#
|
|
|
|
|
import re
|
2026-03-20 20:32:00 +08:00
|
|
|
|
|
|
|
|
from quart import make_response, request
|
|
|
|
|
|
2025-11-18 17:05:16 +08:00
|
|
|
from api.apps import current_user, login_required
|
2026-04-21 18:55:30 +08:00
|
|
|
from api.db import FileType
|
2026-04-27 20:35:00 +08:00
|
|
|
from api.db.services.document_service import DocumentService
|
2024-08-15 09:17:36 +08:00
|
|
|
from api.db.services.file2document_service import File2DocumentService
|
2024-12-12 16:38:03 +08:00
|
|
|
from api.utils.api_utils import (
|
|
|
|
|
get_data_error_result,
|
2025-05-19 14:54:06 +08:00
|
|
|
server_error_response,
|
2024-12-12 16:38:03 +08:00
|
|
|
)
|
2026-04-27 18:54:49 +08:00
|
|
|
from api.utils.web_utils import CONTENT_TYPE_MAP, apply_safe_file_response_headers
|
2025-11-06 09:36:38 +08:00
|
|
|
from common import settings
|
2026-04-27 10:18:16 +08:00
|
|
|
from common.misc_utils import thread_pool_exec
|
2024-08-15 09:17:36 +08:00
|
|
|
|
|
|
|
|
|
2025-05-19 14:54:06 +08:00
|
|
|
@manager.route("/get/<doc_id>", methods=["GET"]) # noqa: F821
|
2026-02-09 19:52:52 +08:00
|
|
|
@login_required
|
2025-11-18 17:05:16 +08:00
|
|
|
async def get(doc_id):
|
2024-08-15 09:17:36 +08:00
|
|
|
try:
|
|
|
|
|
e, doc = DocumentService.get_by_id(doc_id)
|
|
|
|
|
if not e:
|
2024-11-05 11:02:31 +08:00
|
|
|
return get_data_error_result(message="Document not found!")
|
2024-08-15 09:17:36 +08:00
|
|
|
|
2024-09-19 19:19:27 +08:00
|
|
|
b, n = File2DocumentService.get_storage_address(doc_id=doc_id)
|
2026-01-20 13:29:37 +08:00
|
|
|
data = await thread_pool_exec(settings.STORAGE_IMPL.get, b, n)
|
2025-12-03 14:19:53 +08:00
|
|
|
response = await make_response(data)
|
2024-08-15 09:17:36 +08:00
|
|
|
|
2025-07-11 18:47:19 +08:00
|
|
|
ext = re.search(r"\.([^.]+)$", doc.name.lower())
|
|
|
|
|
ext = ext.group(1) if ext else None
|
2026-02-25 09:46:48 +08:00
|
|
|
content_type = None
|
2024-08-15 09:17:36 +08:00
|
|
|
if ext:
|
2026-02-25 09:46:48 +08:00
|
|
|
fallback_prefix = "image" if doc.type == FileType.VISUAL.value else "application"
|
|
|
|
|
content_type = CONTENT_TYPE_MAP.get(ext, f"{fallback_prefix}/{ext}")
|
|
|
|
|
apply_safe_file_response_headers(response, content_type, ext)
|
2024-08-15 09:17:36 +08:00
|
|
|
return response
|
|
|
|
|
except Exception as e:
|
|
|
|
|
return server_error_response(e)
|
|
|
|
|
|
|
|
|
|
|
2025-11-14 19:52:11 +08:00
|
|
|
@manager.route("/download/<attachment_id>", methods=["GET"]) # noqa: F821
|
|
|
|
|
@login_required
|
2025-11-18 17:05:16 +08:00
|
|
|
async def download_attachment(attachment_id):
|
2025-11-14 19:52:11 +08:00
|
|
|
try:
|
|
|
|
|
ext = request.args.get("ext", "markdown")
|
2026-01-20 13:29:37 +08:00
|
|
|
data = await thread_pool_exec(settings.STORAGE_IMPL.get, current_user.id, attachment_id)
|
2025-11-18 17:05:16 +08:00
|
|
|
response = await make_response(data)
|
2026-02-25 09:46:48 +08:00
|
|
|
content_type = CONTENT_TYPE_MAP.get(ext, f"application/{ext}")
|
|
|
|
|
apply_safe_file_response_headers(response, content_type, ext)
|
2025-11-14 19:52:11 +08:00
|
|
|
|
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
return server_error_response(e)
|
|
|
|
|
|