Skip to content

Commit

Permalink
Merge pull request #31 from kang2453/master
Browse files Browse the repository at this point in the history
fix: update file download response to include content length and handle project_id as a list or string
  • Loading branch information
kang2453 authored Jan 6, 2025
2 parents 0f6d542 + 6e47166 commit 9f89be9
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/spaceone/file_manager/connector/aws_s3_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def download_file(self, resource_group:str, file_id: str) :
raise ERROR_CONNECTOR_CONFIGURATION(backend="AWSS3Connector")

obj = self.client.get_object(Bucket=self.bucket_name, Key=object_name)
return obj["Body"]
return obj


@staticmethod
Expand Down
10 changes: 5 additions & 5 deletions src/spaceone/file_manager/interface/rest/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ async def upload_project_file(self, project_id:str, request: Request, file: Uplo

@router.get("/project/{file_id}")
@exception_handler
async def download_project_file(self, file_id:str, token:str, request: Request):
async def download_project_file(self, file_id:str, token:str, request: Request)-> StreamingResponse:

metadata = {
"token": token,
Expand Down Expand Up @@ -175,15 +175,15 @@ async def download_file(self, metadata, params) -> StreamingResponse:

try:
file_conn_mgr = FileConnectorManager()
file_stream = await run_in_threadpool(file_conn_mgr.download_file, resource_group, file_id)
if not file_stream:
obj = await run_in_threadpool(file_conn_mgr.download_file, resource_group, file_id)
if not obj:
raise ERROR_FILE_DOWNLOAD_FAILED(name=file_info["name"])

except Exception as e:
raise ERROR_FILE_DOWNLOAD_FAILED(name=file_info["name"])

return StreamingResponse(
content=file_stream,
content=obj["Body"],
media_type="application/octet-stream",
headers={"Content-Disposition": f"attachment; filename={file_info['name']}"}
headers={"Content-Disposition": f"attachment; filename={file_info['name']}", "content-length": str(obj["ContentLength"])}
)
8 changes: 4 additions & 4 deletions src/spaceone/file_manager/interface/rest/user_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,15 @@ async def download_file(self, metadata, params) -> StreamingResponse:

try:
file_conn_mgr = FileConnectorManager()
file_stream = await run_in_threadpool(file_conn_mgr.download_file, resource_group, file_id)
if not file_stream:
obj = await run_in_threadpool(file_conn_mgr.download_file, resource_group, file_id)
if not obj:
raise ERROR_FILE_DOWNLOAD_FAILED(name=user_file_info["name"])

except Exception as e:
raise ERROR_FILE_DOWNLOAD_FAILED(name=user_file_info["name"])

return StreamingResponse(
content=file_stream,
content=obj["Body"],
media_type="application/octet-stream",
headers={"Content-Disposition": f"attachment; filename={user_file_info['name']}"}
headers={"Content-Disposition": f"attachment; filename={user_file_info['name']}", "content-length": str(obj["ContentLength"])}
)
3 changes: 2 additions & 1 deletion src/spaceone/file_manager/manager/file_manager.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
from typing import Union
from mongoengine import QuerySet

from spaceone.core.manager import BaseManager
Expand Down Expand Up @@ -43,7 +44,7 @@ def get_file(
file_id: str,
domain_id: str,
workspace_id: str = None,
project_id: str = None,
project_id: Union[list, str] = None,
) -> File:

condition = {
Expand Down
1 change: 1 addition & 0 deletions src/spaceone/file_manager/model/file/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class File(MongoModel):
"change_query_keys": {
"resource_type": "reference.resource_type",
"resource_id": "reference.resource_id",
"user_projects": "project_id",
},
"ordering": ["name"],
"indexes": [
Expand Down
3 changes: 3 additions & 0 deletions src/spaceone/file_manager/model/file/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ class FileDeleteRequest(BaseModel):

class FileGetRequest(BaseModel):
file_id: str
resource_group: ResourceGroup
domain_id: Union[list, str, None] = None
workspace_id: Union[list, str, None] = None
project_id: Union[str, None] = None
user_projects: Union[list, None] = None


class FileSearchQueryRequest(BaseModel):
Expand All @@ -55,6 +57,7 @@ class FileSearchQueryRequest(BaseModel):
domain_id: Union[list, str, None] = None
workspace_id: Union[list, str, None] = None
project_id: Union[str, None] = None
user_projects: Union[list, None] = None


class FileStatQueryRequest(BaseModel):
Expand Down
36 changes: 32 additions & 4 deletions src/spaceone/file_manager/service/file_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ def add(self, params: FileAddRequest) -> Union[FileResponse, dict]:
self.identity_mgr.check_workspace(params.workspace_id, params.domain_id)
params.project_id = "*"
elif resource_group == "PROJECT":
if params.project_id != "*":
if not params.project_id:
params.project_id = "*"
else :
self.identity_mgr.get_project(params.project_id, params.domain_id)


Expand Down Expand Up @@ -168,27 +170,49 @@ def delete(self, params: FileDeleteRequest) -> None:
"WORKSPACE_MEMBER",
],
)
@change_value_by_rule("APPEND", "domain_id", "*")
@change_value_by_rule("APPEND", "workspace_id", "*")

@convert_model
def get(self, params: FileGetRequest) -> Union[FileResponse, dict]:
"""Get file
Args:
params (FileGetRequest): {
'file_id': 'str', # required
'resource_group': 'str',
'workspace_id': 'str', # injected from auth
'domain_id': 'str' # injected from auth
'project_id': 'str' # injected from auth
'user_projects': 'list' # injected from auth
}
Returns:
FileResponse:
"""

resource_group = params.resource_group

if resource_group == "SYSTEM":
params.domain_id = "*"
params.workspace_id = "*"
elif resource_group == "DOMAIN":
params.workspace_id = "*"
elif resource_group == "WORKSPACE" :
self.identity_mgr.check_workspace(params.workspace_id, params.domain_id)
params.project_id = "*"
elif resource_group == "PROJECT":
if not params.project_id:
params.project_id = "*"
else :
self.identity_mgr.get_project(params.project_id, params.domain_id)




file_vo = self.file_mgr.get_file(
params.file_id,
params.domain_id,
params.workspace_id,
params.project_id,
)

return FileResponse(**file_vo.to_dict())
Expand All @@ -204,6 +228,7 @@ def get(self, params: FileGetRequest) -> Union[FileResponse, dict]:
)
@change_value_by_rule("APPEND", "domain_id", "*")
@change_value_by_rule("APPEND", "workspace_id", "*")
@change_value_by_rule("APPEND", "user_projects", "*")
@append_query_filter(
[
"file_id",
Expand All @@ -212,6 +237,7 @@ def get(self, params: FileGetRequest) -> Union[FileResponse, dict]:
"resource_id",
"domain_id",
"workspace_id",
"user_projects",
]
)
@append_keyword_filter(["file_id", "name"])
Expand All @@ -228,6 +254,7 @@ def list(self, params: FileSearchQueryRequest) -> Union[FilesResponse, dict]:
'resource_id': 'str',
'domain_id': 'str', # injected from auth
'workspace_id': 'str', # injected from auth
'user_projects': 'list', # injected from auth
}
Returns:
Expand All @@ -251,7 +278,8 @@ def list(self, params: FileSearchQueryRequest) -> Union[FilesResponse, dict]:
)
@change_value_by_rule("APPEND", "domain_id", "*")
@change_value_by_rule("APPEND", "workspace_id", "*")
@append_query_filter(["domain_id", "workspace_id"])
@change_value_by_rule("APPEND", "user_projects", "*")
@append_query_filter(["domain_id", "workspace_id", "user_projects"])
@append_keyword_filter(["file_id", "name"])
@convert_model
def stat(self, params: FileStatQueryRequest) -> dict:
Expand Down

0 comments on commit 9f89be9

Please sign in to comment.