fix(file and app):

1. Handle the encoding issue when downloading Markdown files;
2. Experience the sharing of memory configuration
This commit is contained in:
Timebomb2018
2026-03-25 17:54:27 +08:00
parent def7367e33
commit caab58dd2f
7 changed files with 57 additions and 30 deletions

View File

@@ -109,17 +109,13 @@ class StorageBackend(ABC):
pass
@abstractmethod
async def get_url(self, file_key: str, expires: int = 3600) -> str:
"""
Get an access URL for the file.
Args:
file_key: Unique identifier for the file in the storage system.
expires: URL validity period in seconds (default: 1 hour).
Returns:
URL for accessing the file.
"""
async def get_url(
self,
file_key: str,
expires: int = 3600,
file_name: Optional[str] = None
) -> str:
"""Get an access URL for the file."""
pass
async def get_permanent_url(self, file_key: str) -> Optional[str]:

View File

@@ -210,7 +210,12 @@ class LocalStorage(StorageBackend):
cause=e,
)
async def get_url(self, file_key: str, expires: int = 3600) -> str:
async def get_url(
self,
file_key: str,
expires: int = 3600,
file_name: Optional[str] = None
) -> str:
"""
Get an access URL for the file.
@@ -220,6 +225,7 @@ class LocalStorage(StorageBackend):
Args:
file_key: Unique identifier for the file in the storage system.
expires: URL validity period in seconds (not used for local storage).
file_name: If set, adds Content-Disposition: attachment to force download.
Returns:
A relative URL path for accessing the file.

View File

@@ -7,6 +7,7 @@ Storage Service (OSS) using the oss2 SDK.
import io
import logging
import urllib.parse
from typing import AsyncIterator, Optional
import oss2
@@ -242,24 +243,33 @@ class OSSStorage(StorageBackend):
logger.error(f"Failed to check file existence in OSS {file_key}: {e}")
return False
async def get_url(self, file_key: str, expires: int = 3600) -> str:
async def get_url(
self,
file_key: str,
expires: int = 3600,
file_name: Optional[str] = None,
) -> str:
"""
Get a presigned URL for accessing the file.
Args:
file_key: Unique identifier for the file in the storage system.
expires: URL validity period in seconds (default: 1 hour).
file_name: If set, adds Content-Disposition: attachment to force download.
Returns:
A presigned URL for accessing the file.
"""
try:
url = self.bucket.sign_url("GET", file_key, expires)
params = {}
if file_name:
filename_encoded = urllib.parse.quote(file_name.encode("utf-8"))
params["response-content-disposition"] = f"attachment; filename*=UTF-8''{filename_encoded}"
url = self.bucket.sign_url("GET", file_key, expires, params=params if params else None)
logger.debug(f"Generated presigned URL for {file_key}, expires in {expires}s")
return url
except Exception as e:
logger.error(f"Failed to generate presigned URL for {file_key}: {e}")
# Return a basic URL format as fallback
return f"https://{self.bucket_name}.{self.endpoint.replace('https://', '').replace('http://', '')}/{file_key}"
async def get_permanent_url(self, file_key: str) -> str:

View File

@@ -6,6 +6,7 @@ using the boto3 SDK.
"""
import io
import urllib.parse
import logging
from typing import AsyncIterator, Optional
@@ -352,31 +353,37 @@ class S3Storage(StorageBackend):
logger.error(f"Failed to check file existence in S3 {file_key}: {e}")
return False
async def get_url(self, file_key: str, expires: int = 3600) -> str:
async def get_url(
self,
file_key: str,
expires: int = 3600,
file_name: Optional[str] = None,
) -> str:
"""
Get a presigned URL for accessing the file.
Args:
file_key: Unique identifier for the file in the storage system.
expires: URL validity period in seconds (default: 1 hour).
file_name: If set, adds Content-Disposition: attachment to force download.
Returns:
A presigned URL for accessing the file.
"""
try:
params = {"Bucket": self.bucket_name, "Key": file_key}
if file_name:
filename_encoded = urllib.parse.quote(file_name.encode("utf-8"))
params["ResponseContentDisposition"] = f"attachment; filename*=UTF-8''{filename_encoded}"
url = self.client.generate_presigned_url(
"get_object",
Params={
"Bucket": self.bucket_name,
"Key": file_key,
},
Params=params,
ExpiresIn=expires,
)
logger.debug(f"Generated presigned URL for {file_key}, expires in {expires}s")
return url
except Exception as e:
logger.error(f"Failed to generate presigned URL for {file_key}: {e}")
# Return a basic URL format as fallback
return f"https://{self.bucket_name}.s3.{self.region}.amazonaws.com/{file_key}"
async def get_permanent_url(self, file_key: str) -> str: