-
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.
- Loading branch information
1 parent
e05bad9
commit 6f1fc39
Showing
16 changed files
with
315 additions
and
80 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
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
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
77 changes: 77 additions & 0 deletions
77
core/core-infra-s3/src/main/java/com/sponus/coreinfras3/S3Service.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,77 @@ | ||
package com.sponus.coreinfras3; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import com.amazonaws.services.s3.AmazonS3; | ||
import com.amazonaws.services.s3.model.PutObjectRequest; | ||
import com.sponus.coreinfras3.convert.webp.WebpConvertService; | ||
import com.sponus.coreinfras3.util.FileUtils; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
@Service | ||
@RequiredArgsConstructor | ||
public class S3Service { | ||
|
||
private final AmazonS3 amazonS3; | ||
|
||
private final S3Config s3Config; | ||
|
||
private final WebpConvertService webpConvertService; | ||
|
||
public String uploadImage(MultipartFile multipartFile) { | ||
File file = FileUtils.convertToFile(multipartFile); | ||
return uploadImage(file); | ||
} | ||
|
||
public String uploadImage(File file) { | ||
File webpFile = webpConvertService.convertToWebP(file); | ||
return uploadFile(webpFile); | ||
} | ||
|
||
public String uploadFile(MultipartFile multipartFile) { | ||
File file = FileUtils.convertToFile(multipartFile); | ||
return uploadFile(file); | ||
} | ||
|
||
public String uploadFile(File file) { | ||
String fileName = getFileNamePrefix() + file.getName(); | ||
|
||
amazonS3.putObject(new PutObjectRequest(s3Config.getBucket(), s3Config.getFolder() + fileName, file)); | ||
try { | ||
Files.delete(file.toPath()); | ||
} catch (IOException e) { | ||
throw new IllegalArgumentException("Failed to delete file", e); | ||
} | ||
|
||
return amazonS3.getUrl(s3Config.getBucket(), s3Config.getFolder() + fileName).toString(); | ||
} | ||
|
||
public String deleteFile(String image) { | ||
try { | ||
amazonS3.deleteObject(s3Config.getBucket(), s3Config.getFolder() + image); | ||
} catch (Exception e) { | ||
log.error("error at AmazonS3Manager deleteFile : {}", (Object)e.getStackTrace()); | ||
} | ||
return "삭제 성공"; | ||
} | ||
|
||
public List<String> uploadFiles(List<MultipartFile> files) { | ||
return files.stream() | ||
.map(this::uploadFile) | ||
.toList(); | ||
} | ||
|
||
private String getFileNamePrefix() { | ||
return UUID.randomUUID().toString().substring(0, 5) + "-"; | ||
} | ||
} |
58 changes: 0 additions & 58 deletions
58
core/core-infra-s3/src/main/java/com/sponus/coreinfras3/S3Util.java
This file was deleted.
Oops, something went wrong.
13 changes: 13 additions & 0 deletions
13
core/core-infra-s3/src/main/java/com/sponus/coreinfras3/convert/ImageFormat.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,13 @@ | ||
package com.sponus.coreinfras3.convert; | ||
|
||
public enum ImageFormat { | ||
JPEG, JPG, PNG, GIF, WEBP; | ||
|
||
public static ImageFormat of(String format) { | ||
try { | ||
return ImageFormat.valueOf(format.toUpperCase()); | ||
} catch (IllegalArgumentException e) { | ||
throw new IllegalArgumentException("Unsupported image format: " + format); | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
core/core-infra-s3/src/main/java/com/sponus/coreinfras3/convert/webp/Jpg2WebpStrategy.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,37 @@ | ||
package com.sponus.coreinfras3.convert.webp; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
|
||
import org.springframework.stereotype.Service; | ||
|
||
import com.sksamuel.scrimage.ImmutableImage; | ||
import com.sksamuel.scrimage.webp.WebpWriter; | ||
import com.sponus.coreinfras3.convert.ImageFormat; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
@Service | ||
public class Jpg2WebpStrategy implements WebpConvertStrategy { | ||
|
||
@Override | ||
public boolean identify(ImageFormat imageFormat) { | ||
return imageFormat == ImageFormat.JPG || imageFormat == ImageFormat.JPEG; | ||
} | ||
|
||
@Override | ||
public File convert(String fileName, File file, WebpCompressionParam param) { | ||
try { | ||
File webpFile = ImmutableImage.loader() | ||
.fromFile(file) | ||
.output(WebpWriter.DEFAULT, new File(fileName + ".webp")); | ||
Files.delete(file.toPath()); | ||
return webpFile; | ||
} catch (IOException e) { | ||
log.error("JPG -> WebP conversion failed, cancelling conversion. {}", e.getMessage()); | ||
return file; | ||
} | ||
} | ||
} |
Oops, something went wrong.