Skip to content

Commit

Permalink
refactor: materials/views.py 수정 - 수동 merge로 생성된 코드 중복 삭제
Browse files Browse the repository at this point in the history
  • Loading branch information
devnproyj22 committed Oct 7, 2024
1 parent 9ea107c commit 805b315
Showing 1 changed file with 0 additions and 124 deletions.
124 changes: 0 additions & 124 deletions materials/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,75 +16,6 @@
from .serializers import ImageSerializer, VideoSerializer, WatchHistorySerializer


class ImageCreateView(generics.CreateAPIView):
# POST 요청: 이미지 파일을 업로드합니다.

queryset = Image.objects.all()
serializer_class = ImageSerializer
permission_classes = []
parser_classes = (MultiPartParser, FormParser)

def create(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
if serializer.is_valid():
file = request.FILES.get("file")
if not file:
return Response(
{"error": "No file provided"}, status=status.HTTP_400_BAD_REQUEST
)

# 이미지 파일 검증
try:
img = PILImage.open(file)
img.verify()
except:
return Response(
{"error": "Invalid image file"}, status=status.HTTP_400_BAD_REQUEST
)

# S3 클라이언트 설정
s3_client = boto3.client(
"s3",
aws_access_key_id=settings.AWS_ACCESS_KEY_ID,
aws_secret_access_key=settings.AWS_SECRET_ACCESS_KEY,
region_name=settings.AWS_S3_REGION_NAME,
)

try:
# 이미지 최적화 (선택사항)
img = PILImage.open(file)
buffer = io.BytesIO()
img.save(buffer, format="JPEG", quality=85)
buffer.seek(0)

# S3에 파일 업로드
file_name = f"images/{file.name}"
s3_client.upload_fileobj(
buffer,
settings.AWS_STORAGE_BUCKET_NAME,
file_name,
ExtraArgs={"ContentType": "image/jpeg"},
)

# 업로드된 파일의 URL 생성
file_url = f"https://{settings.AWS_S3_CUSTOM_DOMAIN}/{file_name}"

# 이미지 객체 생성 및 저장
image = serializer.save(file=file_url)

return Response(
self.get_serializer(image).data, status=status.HTTP_201_CREATED
)
except ClientError as e:
return Response(
{"error": str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR
)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)


from .serializers import ImageSerializer, VideoSerializer, WatchHistorySerializer


class ImageCreateView(generics.CreateAPIView):
# POST 요청: 이미지 파일을 업로드합니다.

Expand Down Expand Up @@ -157,7 +88,6 @@ class ImageListCreateView(generics.ListCreateAPIView):
queryset = Image.objects.all()
serializer_class = ImageSerializer
permission_classes = []
permission_classes = []

def perform_create(self, serializer):
serializer.save(course_id=self.request.data.get("course_id"))
Expand All @@ -171,7 +101,6 @@ class ImageRetrieveUpdateDestroyView(generics.RetrieveUpdateDestroyAPIView):
queryset = Image.objects.all()
serializer_class = ImageSerializer
permission_classes = []
permission_classes = []

def check_object_permissions(self, request, obj):
if not request.user.is_staff and obj.course.tutor != request.user:
Expand Down Expand Up @@ -230,64 +159,12 @@ def create(self, request, *args, **kwargs):
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)


class VideoCreateView(generics.CreateAPIView):
# POST 요청: 영상 파일을 업로드합니다.

queryset = Video.objects.all()
serializer_class = VideoSerializer
permission_classes = []
parser_classes = (MultiPartParser, FormParser)

def create(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
if serializer.is_valid():
file = request.FILES.get("file")
if not file:
return Response(
{"error": "No file provided"}, status=status.HTTP_400_BAD_REQUEST
)

# S3 클라이언트 설정
s3_client = boto3.client(
"s3",
aws_access_key_id=settings.AWS_ACCESS_KEY_ID,
aws_secret_access_key=settings.AWS_SECRET_ACCESS_KEY,
region_name=settings.AWS_S3_REGION_NAME,
)

try:
# S3에 파일 업로드
file_name = f"videos/{file.name}"
s3_client.upload_fileobj(
file,
settings.AWS_STORAGE_BUCKET_NAME,
file_name,
ExtraArgs={"ContentType": file.content_type},
)

# 업로드된 파일의 URL 생성
file_url = f"https://{settings.AWS_S3_CUSTOM_DOMAIN}/{file_name}"

# 비디오 객체 생성 및 저장
video = serializer.save(file=file_url)

return Response(
self.get_serializer(video).data, status=status.HTTP_201_CREATED
)
except ClientError as e:
return Response(
{"error": str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR
)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)


class VideoListCreateView(generics.ListCreateAPIView):
# GET 요청: 영상 파일 목록을 조회합니다.

queryset = Video.objects.all()
serializer_class = VideoSerializer
permission_classes = []
permission_classes = []

def perform_create(self, serializer):
serializer.save(topic_id=self.request.data.get("topic_id"))
Expand All @@ -301,7 +178,6 @@ class VideoRetrieveUpdateDestroyView(generics.RetrieveUpdateDestroyAPIView):
queryset = Video.objects.all()
serializer_class = VideoSerializer
permission_classes = []
permission_classes = []

def check_object_permissions(self, request, obj):
if request.method in ["PUT", "PATCH", "DELETE"]:
Expand Down

0 comments on commit 805b315

Please sign in to comment.