-
Notifications
You must be signed in to change notification settings - Fork 5
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
7dac0bb
commit 41d29a6
Showing
15 changed files
with
400 additions
and
186 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
83 changes: 83 additions & 0 deletions
83
backend/pium/src/main/java/com/official/pium/util/PhotoLocalManager.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,83 @@ | ||
package com.official.pium.util; | ||
|
||
import com.official.pium.service.PhotoManager; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
@Component | ||
public class PhotoLocalManager implements PhotoManager { | ||
|
||
private static final String SLASH = "/"; | ||
|
||
@Value("${local.image.web}") | ||
private String webPath; | ||
|
||
@Value("${local.image.root}") | ||
private String localPath; | ||
|
||
@Override | ||
public String upload(MultipartFile multipartFile, String workingDirectory) { | ||
if (multipartFile == null || multipartFile.isEmpty()) { | ||
throw new IllegalArgumentException("이미지 파일이 존재하지 않습니다. multipartFile: " + null); | ||
} | ||
return uploadPhoto(multipartFile, workingDirectory); | ||
} | ||
|
||
private String uploadPhoto(MultipartFile multipartFile, String workingDirectory) { | ||
try { | ||
String fileName = PhotoNameGenerator.of(multipartFile.getOriginalFilename()); | ||
File uploadDirectory = loadDirectory(getLocalDirectoryPath(workingDirectory)); | ||
File uploadPath = new File(uploadDirectory, fileName); | ||
uploadFileInLocal(multipartFile, uploadPath); | ||
|
||
return webPath + SLASH + uploadDirectory + SLASH + fileName; | ||
} catch (Exception e) { | ||
throw new IllegalStateException("파일 업로드를 실패했습니다."); | ||
} | ||
} | ||
|
||
private String getLocalDirectoryPath(String workingDirectory) { | ||
return localPath + SLASH + workingDirectory; | ||
} | ||
|
||
private File loadDirectory(String directoryLocation) { | ||
File directory = new File(directoryLocation); | ||
if (!directory.exists()) { | ||
directory.mkdirs(); | ||
} | ||
return directory; | ||
} | ||
|
||
private void uploadFileInLocal(MultipartFile multipartFile, File uploadPath) { | ||
try { | ||
multipartFile.transferTo(uploadPath); | ||
} catch (IOException e) { | ||
throw new IllegalStateException("파일 변환이 실패했습니다."); | ||
} | ||
} | ||
|
||
@Override | ||
public void delete(String originalImageUrl, String workingDirectory) { | ||
String deletePath = getFileLocalPath(originalImageUrl); | ||
File file = new File(deletePath); | ||
deleteFile(file); | ||
} | ||
|
||
private String getFileLocalPath(String fullPath) { | ||
int urlIndex = fullPath.lastIndexOf(webPath); | ||
if (urlIndex == -1) { | ||
throw new IllegalArgumentException("잘못된 파일 경로입니다."); | ||
} | ||
int urlNextIndex = urlIndex + webPath.length(); | ||
return localPath + fullPath.substring(urlNextIndex); | ||
} | ||
|
||
private void deleteFile(File file) { | ||
if (file.exists()) { | ||
file.delete(); | ||
} | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
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,85 @@ | ||
spring: | ||
flyway: | ||
enabled: false | ||
sql: | ||
init: | ||
schema-locations: classpath:sql/schema.sql | ||
data-locations: classpath:sql/data.sql | ||
mode: always | ||
mvc: | ||
hiddenmethod: | ||
filter: | ||
enabled: true | ||
jpa: | ||
hibernate: | ||
ddl-auto: validate | ||
properties: | ||
hibernate: | ||
format_sql: true | ||
show-sql: true | ||
datasource: | ||
username: sa | ||
url: jdbc:h2:mem:test;MODE=MYSQL | ||
management: | ||
endpoint: | ||
health: | ||
enabled: true | ||
endpoints: | ||
enabled-by-default: false | ||
fcm: | ||
key: | ||
path: test/ | ||
scope: https://www.googleapis.com/auth/firebase.messaging | ||
api: | ||
url: https://fcm.googleapis.com/v1/projects/project-id/messages:send | ||
petPlant: | ||
image: | ||
directory: test | ||
dictPlant: | ||
image: | ||
directory: /dict | ||
server: | ||
servlet: | ||
session: | ||
cookie: | ||
secure: true | ||
same-site: none | ||
logging: | ||
level: | ||
org: | ||
hibernate: | ||
orm: | ||
jdbc: | ||
bind: trace | ||
slack: | ||
webhook-url: https://WEB_HOOK_URL.com | ||
admin: | ||
secondPassword: 123 | ||
password: 123 | ||
account: asdf | ||
aws: | ||
s3: | ||
root: https://test.image.storage | ||
directory: test | ||
folder: test/ | ||
bucket: test/ | ||
|
||
local: | ||
image: | ||
root: src/test/resources | ||
web: https://static.pium.life | ||
auth: | ||
kakao: | ||
member-info-request-uri: https://kapi.kakao.com/v2/user/me | ||
unlink-uri: https://kapi.kakao.com/v1/user/unlink | ||
redirect-uri: http://localhost:8282/authorization | ||
client-id: REST_API_KEY | ||
token-request-uri: https://kauth.kakao.com/oauth/token | ||
admin-id: ADMIN_KEY | ||
registration: | ||
image: | ||
directory: registration | ||
view: | ||
image: | ||
favicion: https://static.pium.life/prod/favicon.ico | ||
home: https://static.pium.life/prod/home.png |
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
39 changes: 39 additions & 0 deletions
39
backend/pium/src/test/java/com/official/pium/config/ImageCleaner.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,39 @@ | ||
package com.official.pium.config; | ||
|
||
import org.springframework.stereotype.Component; | ||
|
||
import java.io.File; | ||
|
||
@Component | ||
public class ImageCleaner { | ||
|
||
private static final String TEST_FILE_ROOT_PATH = "src/test/resources/"; | ||
|
||
public static void cleanUp() { | ||
deleteFolder(TEST_FILE_ROOT_PATH); | ||
} | ||
|
||
private static void deleteFolder(String path) { | ||
File folder = new File(path); | ||
if (folder.exists()) { | ||
deleteFileRecursive(folder); | ||
} | ||
} | ||
|
||
private static void deleteFileRecursive(File file) { | ||
if (file.isDirectory()) { | ||
File[] files = file.listFiles(); | ||
if (files == null) { | ||
return; | ||
} | ||
|
||
for (File child : files) { | ||
deleteFileRecursive(child); | ||
} | ||
} | ||
if (file.getName().contains("application.yml")) { | ||
return; | ||
} | ||
file.delete(); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
backend/pium/src/test/java/com/official/pium/config/ImageCleanerExtension.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,12 @@ | ||
package com.official.pium.config; | ||
|
||
import org.junit.jupiter.api.extension.AfterEachCallback; | ||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
|
||
public class ImageCleanerExtension implements AfterEachCallback { | ||
|
||
@Override | ||
public void afterEach(ExtensionContext context) throws Exception { | ||
ImageCleaner.cleanUp(); | ||
} | ||
} |
Oops, something went wrong.