forked from codesquad-members-2023/issue-tracker
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #276 from codesquad-members-2023-team6/dev
Be: 멀티파일 업로드 API
- Loading branch information
Showing
5 changed files
with
60 additions
and
73 deletions.
There are no files selected for viewing
23 changes: 13 additions & 10 deletions
23
...nd/src/main/java/com/team6/issue_tracker/global/cloud/controller/ImageHostController.java
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 |
---|---|---|
@@ -1,27 +1,30 @@ | ||
package com.team6.issue_tracker.global.cloud.controller; | ||
|
||
import com.team6.issue_tracker.global.cloud.domain.Directory; | ||
import com.team6.issue_tracker.global.cloud.domain.MultifileInfo; | ||
import com.team6.issue_tracker.global.cloud.dto.FileUploadResult; | ||
import com.team6.issue_tracker.global.cloud.service.ImageHostService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestPart; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.io.IOException; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
public class ImageHostController { | ||
|
||
private final ImageHostService imageHostService; | ||
|
||
@Autowired | ||
public ImageHostController(ImageHostService imageHostService) { | ||
this.imageHostService = imageHostService; | ||
} | ||
|
||
@PostMapping("/resource") | ||
public MultifileInfo testUp(MultipartFile file) throws IOException { | ||
return imageHostService.upload(file, Directory.ISSUE); | ||
@Operation( | ||
summary = "이미지 업로드", | ||
tags = "image", | ||
description = "사용자는 이미지를 업로드할 수 있다." | ||
) | ||
@PostMapping(value = "/resource", consumes = "multipart/form-data") | ||
public FileUploadResult testUp(@RequestPart MultipartFile files) throws IOException { | ||
return imageHostService.upload(files, Directory.ISSUE); | ||
} | ||
} |
14 changes: 0 additions & 14 deletions
14
backend/src/main/java/com/team6/issue_tracker/global/cloud/domain/MultifileInfo.java
This file was deleted.
Oops, something went wrong.
23 changes: 23 additions & 0 deletions
23
backend/src/main/java/com/team6/issue_tracker/global/cloud/dto/FileUploadResult.java
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,23 @@ | ||
package com.team6.issue_tracker.global.cloud.dto; | ||
|
||
import com.team6.issue_tracker.global.cloud.domain.Result; | ||
import lombok.*; | ||
|
||
@Getter | ||
@Setter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class FileUploadResult { | ||
private String originKey; | ||
private String uploadKey; | ||
private String path; | ||
private Result result; | ||
|
||
public static FileUploadResult success(String originKey, String uploadKey, String path) { | ||
return new FileUploadResult(originKey, uploadKey, path, Result.SUCCESS); | ||
} | ||
|
||
public static FileUploadResult fail(String originKey) { | ||
return new FileUploadResult(originKey, null , null, Result.FAIL); | ||
} | ||
} |
50 changes: 24 additions & 26 deletions
50
backend/src/main/java/com/team6/issue_tracker/global/cloud/service/ImageHostService.java
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 |
---|---|---|
@@ -1,55 +1,53 @@ | ||
package com.team6.issue_tracker.global.cloud.service; | ||
|
||
import com.amazonaws.services.s3.AmazonS3; | ||
import com.amazonaws.services.s3.model.ObjectMetadata; | ||
import com.amazonaws.services.s3.model.PutObjectRequest; | ||
import com.team6.issue_tracker.global.cloud.domain.Directory; | ||
import com.team6.issue_tracker.global.cloud.domain.MultifileInfo; | ||
import com.team6.issue_tracker.global.cloud.util.MultifileToFileConvertor; | ||
import com.team6.issue_tracker.global.cloud.dto.FileUploadResult; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.UUID; | ||
|
||
@Service | ||
public class ImageHostService { | ||
|
||
private final String bucket; | ||
@Value("${aws.s3.bucket}") | ||
private String bucket; | ||
private final AmazonS3 amazonS3; | ||
|
||
public ImageHostService(@Value("${aws.s3.bucket}") String bucket, AmazonS3 amazonS3) { | ||
this.bucket = bucket; | ||
public ImageHostService(AmazonS3 amazonS3) { | ||
this.amazonS3 = amazonS3; | ||
} | ||
|
||
public MultifileInfo upload(MultipartFile file, Directory directory) throws IOException { | ||
MultifileToFileConvertor.convert(file) | ||
.orElseThrow(() -> new IllegalArgumentException("File convert Failed.")); | ||
return upload(file, directory); | ||
} | ||
public FileUploadResult upload(MultipartFile file, Directory directory) { | ||
|
||
public MultifileInfo upload(File file, Directory directory) { | ||
String key = generateFileName(file, directory.getPrefix()); | ||
putObjectToS3(file, key); | ||
file.delete(); | ||
String originFileName = file.getOriginalFilename(); | ||
String uploadFileName = generateFileName(file, directory.getPrefix()); | ||
String url =""; | ||
try { | ||
url = putObjectToS3(file, uploadFileName); | ||
} catch (IOException e) { | ||
return FileUploadResult.fail(originFileName); | ||
} | ||
|
||
return MultifileInfo.builder() | ||
.key(key) | ||
.path(putObjectToS3(file, key)) | ||
.build(); | ||
return FileUploadResult.success(originFileName, uploadFileName, url); | ||
} | ||
|
||
private String putObjectToS3(File file, String fileName) { | ||
amazonS3.putObject(bucket, fileName, file); | ||
private String putObjectToS3(MultipartFile file, String fileName) throws IOException { | ||
ObjectMetadata objectMetadata = new ObjectMetadata(); | ||
objectMetadata.setContentLength(file.getSize()); | ||
objectMetadata.setContentType(file.getContentType()); | ||
|
||
amazonS3.putObject(new PutObjectRequest(bucket, fileName, file.getInputStream(),objectMetadata)); | ||
return amazonS3.getUrl(bucket, fileName).toString(); | ||
} | ||
|
||
private String generateFileName(File file, String prefix) { | ||
return String.format("%s%s%s", prefix, UUID.randomUUID(), file.getName()); | ||
private String generateFileName(MultipartFile file, String prefix) { | ||
return String.format("%s%s-%s", prefix, UUID.randomUUID(), file.getName()); | ||
} | ||
|
||
public MultifileInfo remove(MultifileInfo info) { | ||
return null; | ||
} | ||
} |
23 changes: 0 additions & 23 deletions
23
...end/src/main/java/com/team6/issue_tracker/global/cloud/util/MultifileToFileConvertor.java
This file was deleted.
Oops, something went wrong.