Skip to content

Commit

Permalink
[DDING-15] presignedUrl contentType 지정 로직 추가 (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
5uhwann authored Oct 15, 2024
1 parent 3e10adc commit cc1e8b1
Show file tree
Hide file tree
Showing 20 changed files with 307 additions and 108 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,9 @@ public class FileMetaData extends BaseEntity {
@Column(nullable = false)
private FileCategory fileCategory;

@Column(nullable = false)
private String fileName;

@Builder
public FileMetaData(FileCategory fileCategory, UUID fileId, String fileName) {
private FileMetaData(FileCategory fileCategory, UUID fileId) {
this.fileId = fileId;
this.fileCategory = fileCategory;
this.fileName = fileName;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package ddingdong.ddingdongBE.domain.filemetadata.service;

import ddingdong.ddingdongBE.domain.filemetadata.entity.FileMetaData;
import java.util.UUID;
import ddingdong.ddingdongBE.domain.filemetadata.service.dto.CreateFileMetaDataCommand;

public interface FacadeFileMetaDataService {

FileMetaData getFileUrlWithMetaData(UUID fileId);
void create(CreateFileMetaDataCommand command);

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package ddingdong.ddingdongBE.domain.filemetadata.service;

import ddingdong.ddingdongBE.domain.filemetadata.entity.FileMetaData;
import ddingdong.ddingdongBE.domain.filemetadata.service.dto.CreateFileMetaDataCommand;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand All @@ -14,8 +14,14 @@ public class FacadeFileMetaDataServiceImpl implements FacadeFileMetaDataService
private final FileMetaDataService fileMetaDataService;

@Override
public FileMetaData getFileUrlWithMetaData(UUID fileId) {
return fileMetaDataService.getByFileId(fileId);
public void create(CreateFileMetaDataCommand command) {
String fileId = extractFilename(command.key());
fileMetaDataService.create(command.toEntity(UUID.fromString(fileId)));
}

private String extractFilename(String key) {
String[] splitKey = key.split("/");
return splitKey[splitKey.length - 1];
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public class GeneralFileMetaDataService implements FileMetaDataService {

private final FileMetaDataRepository fileMetaDataRepository;

@Transactional
@Override
@Transactional
public void create(FileMetaData fileMetaData) {
fileMetaDataRepository.save(fileMetaData);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,16 @@
import ddingdong.ddingdongBE.domain.filemetadata.entity.FileCategory;
import ddingdong.ddingdongBE.domain.filemetadata.entity.FileMetaData;
import java.util.UUID;
import lombok.Builder;

@Builder
public record FileMetaDataCommand(
UUID fileId,
String fileName
public record CreateFileMetaDataCommand(
String key,
FileCategory fileCategory
) {

public FileMetaData toEntity(FileCategory fileCategory) {
public FileMetaData toEntity(UUID fileId) {
return FileMetaData.builder()
.fileCategory(fileCategory)
.fileId(fileId)
.fileName(fileName)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class AwsS3FileStore implements FileStore {
@Value("${cloud.aws.region.static}")
private String region;

@Value("${spring.s3.bucket}")
@Value("${spring.s3.input-bucket}")
private String bucketName;

@Override
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/ddingdong/ddingdongBE/file/api/S3FileAPi.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ddingdong.ddingdongBE.file.api;

import ddingdong.ddingdongBE.auth.PrincipalDetails;
import ddingdong.ddingdongBE.common.exception.ErrorResponse;
import ddingdong.ddingdongBE.file.controller.dto.response.UploadUrlResponse;
import io.swagger.v3.oas.annotations.Operation;
Expand All @@ -12,6 +13,7 @@
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
Expand Down Expand Up @@ -53,6 +55,7 @@ public interface S3FileAPi {
@ResponseStatus(HttpStatus.OK)
@SecurityRequirement(name = "AccessToken")
@GetMapping("/upload-url")
UploadUrlResponse getUploadUrl(@RequestParam("fileName") String fileName);
UploadUrlResponse getPreSignedUrl(@AuthenticationPrincipal PrincipalDetails principalDetails,
@RequestParam("fileName") String fileName);

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package ddingdong.ddingdongBE.file.controller;

import ddingdong.ddingdongBE.auth.PrincipalDetails;
import ddingdong.ddingdongBE.domain.user.entity.User;
import ddingdong.ddingdongBE.file.api.S3FileAPi;
import ddingdong.ddingdongBE.file.controller.dto.response.UploadUrlResponse;
import ddingdong.ddingdongBE.file.service.S3FileService;
import ddingdong.ddingdongBE.file.service.dto.command.GeneratePreSignedUrlRequestCommand;
import ddingdong.ddingdongBE.file.service.dto.query.GeneratePreSignedUrlRequestQuery;
import java.net.URL;
import java.time.LocalDateTime;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RestController;

Expand All @@ -13,7 +19,13 @@ public class S3FileController implements S3FileAPi {
private final S3FileService s3FileService;

@Override
public UploadUrlResponse getUploadUrl(String fileName) {
return s3FileService.generatePreSignedUrl(fileName);
public UploadUrlResponse getPreSignedUrl(PrincipalDetails principalDetails, String fileName) {
User user = principalDetails.getUser();
LocalDateTime now = LocalDateTime.now();
GeneratePreSignedUrlRequestQuery query =
s3FileService.generatePresignedUrlRequest(
new GeneratePreSignedUrlRequestCommand(now, user.getAuthId(), fileName));
URL presingedUrl = s3FileService.getPresignedUrl(query.generatePresignedUrlRequest());
return UploadUrlResponse.of(query, presingedUrl);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package ddingdong.ddingdongBE.file.controller.dto.response;

import ddingdong.ddingdongBE.file.service.dto.query.GeneratePreSignedUrlRequestQuery;
import io.swagger.v3.oas.annotations.media.Schema;
import java.net.URL;
import lombok.Builder;

@Schema(
Expand All @@ -12,14 +14,18 @@ public record UploadUrlResponse(

@Schema(description = "presignedUrl", example = "https://test-bucket.s3.amazonaws.com/test/jpg/image.jpg")
String uploadUrl,
@Schema(description = "업로드 파일 식별자(UUID)", example = "9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d")
String fileId
@Schema(description = "업로드 key(경로)", example = "local/file/2024-01-01/cow/UUID")
String key,
@Schema(description = "contentType(presignedUrl 업로드 요청 시 사용)", example = "image/png")
String contentType

) {

public static UploadUrlResponse of(String uploadUrl, String fileId) {
public static UploadUrlResponse of(GeneratePreSignedUrlRequestQuery query, URL uploadUrl) {
return UploadUrlResponse.builder()
.uploadUrl(uploadUrl)
.fileId(fileId)
.uploadUrl(uploadUrl.toString())
.key(query.key())
.contentType(query.contentType())
.build();
}

Expand Down
56 changes: 56 additions & 0 deletions src/main/java/ddingdong/ddingdongBE/file/service/ContentType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package ddingdong.ddingdongBE.file.service;

import java.util.Arrays;
import java.util.List;
import lombok.Getter;

@Getter
public enum ContentType {
JPEG("image/jpeg", false, "jpg", "jpeg", "jpe", "jif", "jfif", "jfi"),
PNG("image/png", false, "png"),
GIF("image/gif", false, "gif"),
WEBP("image/webp", false, "webp"),
TIFF("image/tiff", false, "tiff", "tif"),
BMP("image/bmp", false, "bmp"),
SVG("image/svg+xml", false, "svg", "svgz"),
ICO("image/x-icon", false, "ico"),
HEIC("image/heic", false, "heic"),
HEIF("image/heif", false, "heif"),
RAW("image/x-raw", false, "raw", "arw", "cr2", "nrw", "k25"),
PSD("image/vnd.adobe.photoshop", false, "psd"),
PDF("application/pdf", false, "pdf"),
MSWORD("application/msword", false, "doc"),
DOCX("application/vnd.openxmlformats-officedocument.wordprocessingml.document", false, "docx"),
EXCEL("application/vnd.ms-excel", false, "xls"),
XLSX("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", false, "xlsx"),
TEXT("text/plain", false, "txt"),
HTML("text/html", false, "html"),
MP4("video/mp4", true, "mp4"),
WEBM("video/webm", true, "webm"),
MOV("video/quicktime", true, "mov"),
OCTET_STREAM("application/octet-stream", false);

private final String mimeType;
private final boolean isVideo;
private final List<String> extensions;

ContentType(String mimeType, boolean isVideo, String... extensions) {
this.mimeType = mimeType;
this.isVideo = isVideo;
this.extensions = Arrays.asList(extensions);
}

public static ContentType fromExtension(String extension) {
String lowerExtension = extension.toLowerCase();
for (ContentType contentType : values()) {
if (contentType.extensions.contains(lowerExtension)) {
return contentType;
}
}
return OCTET_STREAM;
}

public boolean isVideo() {
return this.isVideo;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

//TODO: 리팩토링 후 제거
@Service
@RequiredArgsConstructor
public class FileService {
Expand Down
Loading

0 comments on commit cc1e8b1

Please sign in to comment.