This repository has been archived by the owner on Jun 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
12 changed files
with
192 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import datetime | ||
|
||
from fastapi import UploadFile | ||
from pydantic import BaseModel, field_validator | ||
|
||
|
||
class File(BaseModel): | ||
id: int | ||
file_path: str | ||
type: str | ||
original_name: str | ||
create: datetime.datetime | ||
|
||
|
||
class FileCreate(BaseModel): | ||
file_path: str | ||
type: str | ||
original_name: str | ||
|
||
@field_validator('file_path', 'type', 'original_name') | ||
def not_empty(cls, v): | ||
if not v or not v.strip(): | ||
raise ValueError('빈 값은 허용되지 않습니다.') | ||
return v | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import hashlib | ||
from datetime import datetime | ||
|
||
import aiofiles | ||
from fastapi import UploadFile | ||
from sqlalchemy.orm import Session | ||
|
||
from core.models.models import File | ||
|
||
|
||
FILE_LOCATION = f"./static/img/recipe/" | ||
|
||
|
||
def create_hash_name(file_name: str): | ||
return hashlib.sha256( | ||
f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}{file_name}".encode() | ||
).hexdigest() | ||
|
||
|
||
def get_file(db: Session, file_id: int): | ||
return db.query(File).filter(File.id == file_id).first() | ||
|
||
|
||
def get_file_by_original_name(db: Session, original_name: str): | ||
return db.query(File).filter(File.original_name == original_name).first() | ||
|
||
|
||
async def create_file(db: Session, file: UploadFile): | ||
file_name = create_hash_name(file.filename) | ||
|
||
# 파일 경로가 없다면 생성 | ||
import os | ||
|
||
if not os.path.exists(os.path.dirname(FILE_LOCATION)): | ||
os.makedirs(FILE_LOCATION) | ||
|
||
file_location = os.path.join(FILE_LOCATION, file_name) | ||
async with aiofiles.open(file_location, 'wb+') as f: | ||
while content := await file.read(1024): | ||
await f.write(content) | ||
|
||
# 파일 권한 변경 | ||
os.chmod(file_location, 0o444) | ||
|
||
db_file = File( | ||
file_path=file_location, | ||
type=file.content_type, | ||
original_name=file.filename, | ||
) | ||
|
||
db.add(db_file) | ||
db.commit() | ||
|
||
return get_file_by_original_name(db=db, original_name=file.filename).id | ||
|
||
|
||
def delete_file(db: Session, file_id: int): | ||
db.query(File).filter(File.id == file_id).delete() | ||
db.commit() | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import urllib.parse | ||
|
||
from fastapi import APIRouter, Depends, HTTPException, UploadFile, File | ||
from fastapi.responses import StreamingResponse | ||
from sqlalchemy.orm import Session | ||
from starlette import status | ||
|
||
import internal.admin.admin_crud as admin_crud | ||
from core.database import get_db | ||
from core.models import models | ||
from internal.file import file_crud | ||
|
||
router = APIRouter( | ||
prefix="/api/file", | ||
tags=["File"] | ||
) | ||
|
||
|
||
# File | ||
# Create | ||
@router.post("/", status_code=status.HTTP_201_CREATED) | ||
async def create_file(file: UploadFile = File(None), db: Session = Depends(get_db)): | ||
await file_crud.create_file(db=db, file=file) | ||
|
||
|
||
# Read | ||
@router.get("/{file_id}") | ||
async def get_file(file_id: int, db: Session = Depends(get_db)): | ||
db_file = file_crud.get_file(db=db, file_id=file_id) | ||
|
||
def iter_file(): | ||
with open(db_file.file_path, mode="rb") as f: | ||
yield from f | ||
|
||
return StreamingResponse(iter_file(), media_type=db_file.type, | ||
headers={ | ||
"Content-Disposition": f"attachment; filename={urllib.parse.quote(db_file.original_name, encoding='utf-8')}"} | ||
) | ||
|
||
@router.delete("/{file_id}", status_code=status.HTTP_204_NO_CONTENT) | ||
async def delete_file(file_id: int, db: Session = Depends(get_db)): | ||
file_crud.delete_file(db=db, file_id=file_id) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters