1+ from bson import ObjectId
2+ import boto3
3+ from botocore .exceptions import ClientError
4+ from src .config .S3Config import s3_client
5+ from src .config .mongodb import get_mongo_client
6+
7+ class FileRepository :
8+ def __init__ (self ):
9+ client = get_mongo_client ()
10+ self .db = client ["xrpedia-data" ]
11+ self .files_collection = self .db ["files" ]
12+
13+ def get_file_info (self , file_id : str ):
14+ return self .files_collection .find_one (
15+ {"_id" : ObjectId (file_id )},
16+ {"s3_key" : 1 , "s3_bucket" : 1 }
17+ )
18+
19+ def get_presigned_url (self , s3_bucket : str , s3_key : str ):
20+ try :
21+ s3_client .head_object (Bucket = s3_bucket , Key = s3_key )
22+ return s3_client .generate_presigned_url (
23+ "get_object" ,
24+ Params = {"Bucket" : s3_bucket , "Key" : s3_key },
25+ ExpiresIn = 3600 ,
26+ )
27+ except ClientError as e :
28+ # 파일 존재 X : 404 반환
29+ if e .response ["Error" ]["Code" ] == "404" :
30+ return None
31+ else :
32+ raise e
33+
34+ # MongoDB에서 해당 파일의 다운로드 횟수 +1
35+ def increment_download_count (self , file_id : str ):
36+ self .files_collection .update_one (
37+ {"_id" : ObjectId (file_id )},
38+ {"$inc" : {"download_count" : 1 }}
39+ )
0 commit comments