260311_01_ENH: 파일 저장 서비스 구현 및 컨택 시나리오 파일 첨부 수정
This commit is contained in:
@@ -4,11 +4,13 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
import org.springframework.boot.ApplicationRunner;
|
import org.springframework.boot.ApplicationRunner;
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
|
||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
|
@ConfigurationPropertiesScan
|
||||||
public class ContactServerApplication {
|
public class ContactServerApplication {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import lombok.RequiredArgsConstructor;
|
|||||||
@Getter(onMethod_ = @JsonValue)
|
@Getter(onMethod_ = @JsonValue)
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public enum SurveyType implements CodeEnum<Short> {
|
public enum SurveyType implements CodeEnum<Short> {
|
||||||
|
|
||||||
NONE((short) 0), // 없음
|
NONE((short) 0), // 없음
|
||||||
KON((short) 1), // KON
|
KON((short) 1), // KON
|
||||||
KIWI((short) 2), // KIWI
|
KIWI((short) 2), // KIWI
|
||||||
|
|||||||
@@ -28,7 +28,10 @@ public enum SystemEventType implements CodeEnum<Short> {
|
|||||||
UPDATE_ROLE((short) 412), // 권한 수정
|
UPDATE_ROLE((short) 412), // 권한 수정
|
||||||
DELETE_ROLE((short) 413), // 권한 삭제
|
DELETE_ROLE((short) 413), // 권한 삭제
|
||||||
|
|
||||||
UPSERT_LIST_HEADER((short) 1011),
|
UPSERT_LIST_HEADER((short) 1011), //리스트 헤더 수정
|
||||||
|
UPLOAD_LIST((short) 1021), //리스트 업로드
|
||||||
|
UPDATE_LIST((short) 1022), //리스트 수정
|
||||||
|
DELETE_LIST((short) 1023), //리스트 삭제
|
||||||
|
|
||||||
DUMMY((short) 9999);
|
DUMMY((short) 9999);
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package com.kri.contact.server.config;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@ConfigurationProperties(prefix = "kri.file")
|
||||||
|
public class FileStorageProperties {
|
||||||
|
private String listBaseDir;
|
||||||
|
private String listItemBaseDir;
|
||||||
|
private String contactSenarioBaseDir;
|
||||||
|
}
|
||||||
@@ -10,7 +10,7 @@ import com.kri.contact.server.dto.project.ProjectMainTemplateUpdateRequestDto;
|
|||||||
import com.kri.contact.server.dto.project.ProjectOwnerResponseDto;
|
import com.kri.contact.server.dto.project.ProjectOwnerResponseDto;
|
||||||
import com.kri.contact.server.dto.project.ProjectSearchRequestDto;
|
import com.kri.contact.server.dto.project.ProjectSearchRequestDto;
|
||||||
import com.kri.contact.server.dto.project.ProjectUpdateRequestDto;
|
import com.kri.contact.server.dto.project.ProjectUpdateRequestDto;
|
||||||
import com.kri.contact.server.service.FileService;
|
import com.kri.contact.server.service.file.ContactScenarioFileService;
|
||||||
import com.kri.contact.server.service.ProjectService;
|
import com.kri.contact.server.service.ProjectService;
|
||||||
import com.kri.contact.server.service.SystemEventLogService;
|
import com.kri.contact.server.service.SystemEventLogService;
|
||||||
import io.swagger.v3.oas.annotations.Operation;
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
@@ -39,7 +39,7 @@ public class ProjectController {
|
|||||||
|
|
||||||
private final SystemEventLogService systemEventLogService;
|
private final SystemEventLogService systemEventLogService;
|
||||||
private final ProjectService projectService;
|
private final ProjectService projectService;
|
||||||
private final FileService fileService;
|
private final ContactScenarioFileService contactScenarioFileService;
|
||||||
|
|
||||||
@Operation(summary = "프로젝트 조회")
|
@Operation(summary = "프로젝트 조회")
|
||||||
@GetMapping("/{prjctIdx}")
|
@GetMapping("/{prjctIdx}")
|
||||||
@@ -101,7 +101,7 @@ public class ProjectController {
|
|||||||
@PutMapping("/senario-image/{prjctCd}")
|
@PutMapping("/senario-image/{prjctCd}")
|
||||||
public String uploadContactSenarioImage(@PathVariable String prjctCd,
|
public String uploadContactSenarioImage(@PathVariable String prjctCd,
|
||||||
@RequestParam("file") MultipartFile file) {
|
@RequestParam("file") MultipartFile file) {
|
||||||
return fileService.storeTempFile(prjctCd, file);
|
return contactScenarioFileService.storeTempFile(prjctCd, file);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Operation(summary = "프로젝트 수정")
|
@Operation(summary = "프로젝트 수정")
|
||||||
@@ -113,7 +113,7 @@ public class ProjectController {
|
|||||||
) {
|
) {
|
||||||
final LocalDateTime now = DateTimeUtil.nowKstLocalDateTime();
|
final LocalDateTime now = DateTimeUtil.nowKstLocalDateTime();
|
||||||
|
|
||||||
String newHtml = fileService.syncOnSave(
|
String newHtml = contactScenarioFileService.syncOnSave(
|
||||||
req.getProjectInfo().getPrjctCd(),
|
req.getProjectInfo().getPrjctCd(),
|
||||||
req.getProjectInfo().getContactSenario()
|
req.getProjectInfo().getContactSenario()
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -13,12 +13,16 @@ public class ProjectSearchRequestDto {
|
|||||||
|
|
||||||
@Schema(description = "검색 텍스트")
|
@Schema(description = "검색 텍스트")
|
||||||
private String prjctText; // like 대상
|
private String prjctText; // like 대상
|
||||||
|
|
||||||
@Schema(description = "프로젝트 상태 리스트")
|
@Schema(description = "프로젝트 상태 리스트")
|
||||||
private List<Short> prjctStatusList; // in (0,1,10)
|
private List<Short> prjctStatusList; // in (0,1,10)
|
||||||
|
|
||||||
@Schema(description = "담당 부서 리스트")
|
@Schema(description = "담당 부서 리스트")
|
||||||
private List<String> ownerDeptList; // in ('S101','P101')
|
private List<String> ownerDeptList; // in ('S101','P101')
|
||||||
|
|
||||||
@Schema(description = "담당 실사 리스트")
|
@Schema(description = "담당 실사 리스트")
|
||||||
private List<String> ownerFwList; // in ('jspark','jhchae')
|
private List<String> ownerFwList; // in ('jspark','jhchae')
|
||||||
|
|
||||||
|
@Schema(description = "페이지네이션 정보")
|
||||||
private PageRequestDto pageRequest;
|
private PageRequestDto pageRequest;
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.kri.contact.server.scheduler;
|
package com.kri.contact.server.scheduler;
|
||||||
|
|
||||||
|
import com.kri.contact.server.service.file.ContactScenarioFileService;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.DirectoryStream;
|
import java.nio.file.DirectoryStream;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
@@ -17,14 +18,14 @@ import org.springframework.stereotype.Component;
|
|||||||
@Component
|
@Component
|
||||||
public class TempFileCleanupScheduler {
|
public class TempFileCleanupScheduler {
|
||||||
|
|
||||||
private final Path uploadBaseDir;
|
private final ContactScenarioFileService contactScenarioFileService;
|
||||||
private final Duration expireDuration;
|
private final Duration expireDuration;
|
||||||
|
|
||||||
public TempFileCleanupScheduler(
|
public TempFileCleanupScheduler(
|
||||||
@Value("${kri.contact-senario.upload-base-dir}") String uploadBaseDirStr,
|
ContactScenarioFileService contactScenarioFileService,
|
||||||
@Value("${kri.contact-senario.temp-expire-hours:24}") long expireHours
|
@Value("${kri.contact-senario.temp-expire-hours:24}") long expireHours
|
||||||
) {
|
) {
|
||||||
this.uploadBaseDir = Paths.get(uploadBaseDirStr);
|
this.contactScenarioFileService = contactScenarioFileService;
|
||||||
this.expireDuration = Duration.ofHours(expireHours);
|
this.expireDuration = Duration.ofHours(expireHours);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -33,14 +34,14 @@ public class TempFileCleanupScheduler {
|
|||||||
*/
|
*/
|
||||||
@Scheduled(cron = "${kri.contact-senario.temp-cleanup-cron:0 0 0 * * *}")
|
@Scheduled(cron = "${kri.contact-senario.temp-cleanup-cron:0 0 0 * * *}")
|
||||||
public void cleanupOldTempFiles() {
|
public void cleanupOldTempFiles() {
|
||||||
|
Path uploadBaseDir = contactScenarioFileService.getUploadBaseDir();
|
||||||
|
|
||||||
if (!Files.isDirectory(uploadBaseDir)) {
|
if (!Files.isDirectory(uploadBaseDir)) {
|
||||||
log.warn("[TempCleanup] uploadBaseDir({}) 가 디렉토리가 아닙니다.", uploadBaseDir);
|
log.warn("[TempCleanup] uploadBaseDir({}) 가 디렉토리가 아닙니다.", uploadBaseDir);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Instant now = Instant.now();
|
Instant threshold = Instant.now().minus(expireDuration);
|
||||||
Instant threshold = now.minus(expireDuration);
|
|
||||||
|
|
||||||
log.info("[TempCleanup] 시작 - baseDir={}, threshold={}", uploadBaseDir, threshold);
|
log.info("[TempCleanup] 시작 - baseDir={}, threshold={}", uploadBaseDir, threshold);
|
||||||
|
|
||||||
try (DirectoryStream<Path> projectDirs = Files.newDirectoryStream(uploadBaseDir)) {
|
try (DirectoryStream<Path> projectDirs = Files.newDirectoryStream(uploadBaseDir)) {
|
||||||
@@ -69,16 +70,13 @@ public class TempFileCleanupScheduler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
BasicFileAttributes attrs =
|
BasicFileAttributes attrs = Files.readAttributes(file, BasicFileAttributes.class);
|
||||||
Files.readAttributes(file, BasicFileAttributes.class);
|
|
||||||
|
|
||||||
// 생성 시간(지원 안 하면 마지막 수정 시간)을 기준으로 비교
|
|
||||||
Instant created = attrs.creationTime() != null
|
Instant created = attrs.creationTime() != null
|
||||||
? attrs.creationTime().toInstant()
|
? attrs.creationTime().toInstant()
|
||||||
: attrs.lastModifiedTime().toInstant();
|
: attrs.lastModifiedTime().toInstant();
|
||||||
|
|
||||||
if (created.isBefore(threshold)) {
|
if (created.isBefore(threshold)) {
|
||||||
Files.deleteIfExists(file);
|
contactScenarioFileService.deleteTempPath(file);
|
||||||
log.debug("[TempCleanup] 삭제: {} (created={})", file, created);
|
log.debug("[TempCleanup] 삭제: {} (created={})", file, created);
|
||||||
}
|
}
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
|
|||||||
@@ -1,180 +0,0 @@
|
|||||||
package com.kri.contact.server.service;
|
|
||||||
|
|
||||||
import com.kri.common.file.FileUtil;
|
|
||||||
import com.kri.common.util.CommonUtil;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.nio.file.DirectoryStream;
|
|
||||||
import java.nio.file.Files;
|
|
||||||
import java.nio.file.Path;
|
|
||||||
import java.nio.file.Paths;
|
|
||||||
import java.nio.file.StandardCopyOption;
|
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.Set;
|
|
||||||
import org.jsoup.Jsoup;
|
|
||||||
import org.jsoup.nodes.Document;
|
|
||||||
import org.jsoup.nodes.Element;
|
|
||||||
import org.jsoup.select.Elements;
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
|
||||||
|
|
||||||
@Service
|
|
||||||
public class FileService {
|
|
||||||
|
|
||||||
private static final Logger log = LoggerFactory.getLogger(FileService.class);
|
|
||||||
private final Path uploadBaseDir; // ex) /data/upload-file/contact-senario
|
|
||||||
private final String publicBaseUrlPrefix; // ex) /upload-file/contact-senario
|
|
||||||
|
|
||||||
public FileService(
|
|
||||||
@Value("${kri.contact-senario.upload-base-dir}") String uploadBaseDir,
|
|
||||||
@Value("${kri.contact-senario.public-base-url}") String publicBaseUrlPrefix
|
|
||||||
) {
|
|
||||||
this.uploadBaseDir = Paths.get(uploadBaseDir).toAbsolutePath().normalize();
|
|
||||||
this.publicBaseUrlPrefix = publicBaseUrlPrefix.endsWith("/")
|
|
||||||
? publicBaseUrlPrefix.substring(0, publicBaseUrlPrefix.length() - 1)
|
|
||||||
: publicBaseUrlPrefix;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Temp에 파일 저장 후, public URL 반환
|
|
||||||
*/
|
|
||||||
public String storeTempFile(String prjctCd, MultipartFile file) {
|
|
||||||
try {
|
|
||||||
String original = Optional.ofNullable(file.getOriginalFilename()).orElse("file");
|
|
||||||
String ext = "";
|
|
||||||
|
|
||||||
int dot = original.lastIndexOf('.');
|
|
||||||
if (dot > 0 && dot < original.length() - 1) {
|
|
||||||
ext = original.substring(dot);
|
|
||||||
}
|
|
||||||
|
|
||||||
String newName = CommonUtil.uuid32() + ext;
|
|
||||||
|
|
||||||
Path tempDir = getTempDir(prjctCd);
|
|
||||||
Files.createDirectories(tempDir);
|
|
||||||
|
|
||||||
Path target = tempDir.resolve(newName);
|
|
||||||
file.transferTo(target.toFile());
|
|
||||||
|
|
||||||
// 브라우저에서 사용할 URL
|
|
||||||
return publicBaseUrlPrefix + "/" + prjctCd + "/Temp/" + newName;
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new RuntimeException("파일 저장 실패", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 컨택 시나리오 저장 시: 1) HTML 내 Temp 이미지 -> Asset으로 이동 & src 수정 2) Asset 디렉토리에서 더 이상 사용되지 않는 파일 삭제 3)
|
|
||||||
* 정리된 HTML 반환
|
|
||||||
*/
|
|
||||||
public String syncOnSave(String prjctCd, String rawHtml) {
|
|
||||||
if (rawHtml == null) {
|
|
||||||
rawHtml = "";
|
|
||||||
}
|
|
||||||
|
|
||||||
String returnHtml = rawHtml;
|
|
||||||
try {
|
|
||||||
|
|
||||||
// body fragment로 파싱 (KriEditor가 body 없이 <p>...</p> 형태 저장하므로)
|
|
||||||
Document doc = Jsoup.parseBodyFragment(rawHtml);
|
|
||||||
Elements imgs = doc.select("img[src]");
|
|
||||||
|
|
||||||
Set<String> assetFileNamesToKeep = new HashSet<>();
|
|
||||||
|
|
||||||
for (Element img : imgs) {
|
|
||||||
String src = img.attr("src");
|
|
||||||
if (src == null || src.isBlank()) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (!src.contains("/" + prjctCd + "/")) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (src.contains("/" + prjctCd + "/Temp/")) {
|
|
||||||
// Temp → Asset 이동
|
|
||||||
String fileName = FileUtil.extractFileNameFromPath(src);
|
|
||||||
moveTempToAsset(prjctCd, fileName);
|
|
||||||
|
|
||||||
String assetSrc = src.replace("/Temp/", "/Asset/");
|
|
||||||
img.attr("src", assetSrc);
|
|
||||||
|
|
||||||
assetFileNamesToKeep.add(fileName);
|
|
||||||
} else if (src.contains("/" + prjctCd + "/Asset/")) {
|
|
||||||
String fileName = FileUtil.extractFileNameFromPath(src);
|
|
||||||
assetFileNamesToKeep.add(fileName);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Asset 디렉토리 정리 (본문에 없는 파일 삭제)
|
|
||||||
cleanupAssetDir(prjctCd, assetFileNamesToKeep);
|
|
||||||
|
|
||||||
// body 안의 HTML만 반환 (불필요한 <html><body> wrapper 제거)
|
|
||||||
returnHtml = doc.body().html();
|
|
||||||
} catch (IOException e) {
|
|
||||||
log.warn("temp 파일 이동 실패: {}", rawHtml, e);
|
|
||||||
}
|
|
||||||
|
|
||||||
return returnHtml;
|
|
||||||
}
|
|
||||||
|
|
||||||
/***************************private************************************/
|
|
||||||
private String normalizeBaseUrl(String url) {
|
|
||||||
if (url.endsWith("/")) {
|
|
||||||
return url.substring(0, url.length() - 1);
|
|
||||||
}
|
|
||||||
return url;
|
|
||||||
}
|
|
||||||
|
|
||||||
private Path getTempDir(String prjctCd) {
|
|
||||||
return uploadBaseDir.resolve(prjctCd).resolve("Temp");
|
|
||||||
}
|
|
||||||
|
|
||||||
private Path getAssetDir(String prjctCd) {
|
|
||||||
return uploadBaseDir.resolve(prjctCd).resolve("Asset");
|
|
||||||
}
|
|
||||||
|
|
||||||
private void moveTempToAsset(String prjctCd, String fileName) throws IOException {
|
|
||||||
if (fileName == null || fileName.isBlank()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Path tempDir = getTempDir(prjctCd);
|
|
||||||
Path assetDir = getAssetDir(prjctCd);
|
|
||||||
Files.createDirectories(assetDir);
|
|
||||||
|
|
||||||
Path source = tempDir.resolve(fileName).normalize();
|
|
||||||
Path target = assetDir.resolve(fileName).normalize();
|
|
||||||
|
|
||||||
if (!source.startsWith(tempDir) || !target.startsWith(assetDir)) {
|
|
||||||
// 안전 체크
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (!Files.exists(source)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Files.move(source, target, StandardCopyOption.REPLACE_EXISTING);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void cleanupAssetDir(String prjctCd, Set<String> keepFileNames) throws IOException {
|
|
||||||
Path assetDir = getAssetDir(prjctCd);
|
|
||||||
if (!Files.exists(assetDir)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try (DirectoryStream<Path> stream = Files.newDirectoryStream(assetDir)) {
|
|
||||||
for (Path path : stream) {
|
|
||||||
if (!Files.isRegularFile(path)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
String fileName = path.getFileName().toString();
|
|
||||||
if (!keepFileNames.contains(fileName)) {
|
|
||||||
Files.deleteIfExists(path);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,151 @@
|
|||||||
|
package com.kri.contact.server.service.file;
|
||||||
|
|
||||||
|
import com.kri.common.file.FileUtil;
|
||||||
|
import com.kri.contact.server.config.FileStorageProperties;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.DirectoryStream;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.Set;
|
||||||
|
import lombok.Getter;
|
||||||
|
import org.jsoup.Jsoup;
|
||||||
|
import org.jsoup.nodes.Document;
|
||||||
|
import org.jsoup.nodes.Element;
|
||||||
|
import org.jsoup.select.Elements;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class ContactScenarioFileService extends AbstractFileService {
|
||||||
|
|
||||||
|
private static final Logger log = LoggerFactory.getLogger(ContactScenarioFileService.class);
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
private final Path uploadBaseDir;
|
||||||
|
private final String publicBaseUrlPrefix;
|
||||||
|
|
||||||
|
public ContactScenarioFileService(
|
||||||
|
FileStorageProperties fileStorageProperties,
|
||||||
|
@Value("${kri.contact-senario.public-base-url}") String publicBaseUrlPrefix
|
||||||
|
) {
|
||||||
|
this.uploadBaseDir = Paths.get(fileStorageProperties.getContactSenarioBaseDir())
|
||||||
|
.toAbsolutePath()
|
||||||
|
.normalize();
|
||||||
|
this.publicBaseUrlPrefix = normalizeBaseUrl(publicBaseUrlPrefix);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Path getBaseDir() {
|
||||||
|
return uploadBaseDir;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String buildFileName(MultipartFile file) {
|
||||||
|
String original = Optional.ofNullable(file.getOriginalFilename()).orElse("file");
|
||||||
|
int dot = original.lastIndexOf('.');
|
||||||
|
String ext = (dot > 0 && dot < original.length() - 1) ? original.substring(dot) : "";
|
||||||
|
return "scenario_" + System.currentTimeMillis() + "_" + java.util.UUID.randomUUID().toString()
|
||||||
|
.replace("-", "") + ext;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Temp에 파일 저장 후 public URL 반환
|
||||||
|
*/
|
||||||
|
public String storeTempFile(String prjctCd, MultipartFile file) {
|
||||||
|
try {
|
||||||
|
String fileName = saveFile(getTempDir(prjctCd), file);
|
||||||
|
return publicBaseUrlPrefix + "/" + prjctCd + "/Temp/" + fileName;
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException("파일 저장 실패", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 저장 시 Temp -> Asset 이동, html src 치환, 미사용 asset 삭제
|
||||||
|
*/
|
||||||
|
public String syncOnSave(String prjctCd, String rawHtml) {
|
||||||
|
if (rawHtml == null) {
|
||||||
|
rawHtml = "";
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
Document doc = Jsoup.parseBodyFragment(rawHtml);
|
||||||
|
Elements imgs = doc.select("img[src]");
|
||||||
|
Set<String> assetFileNamesToKeep = new HashSet<>();
|
||||||
|
|
||||||
|
for (Element img : imgs) {
|
||||||
|
String src = img.attr("src");
|
||||||
|
if (src.isBlank()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (!src.contains("/" + prjctCd + "/")) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (src.contains("/" + prjctCd + "/Temp/")) {
|
||||||
|
String fileName = FileUtil.extractFileNameFromPath(src);
|
||||||
|
moveTempToAsset(prjctCd, fileName);
|
||||||
|
img.attr("src", src.replace("/Temp/", "/Asset/"));
|
||||||
|
assetFileNamesToKeep.add(fileName);
|
||||||
|
} else if (src.contains("/" + prjctCd + "/Asset/")) {
|
||||||
|
String fileName = FileUtil.extractFileNameFromPath(src);
|
||||||
|
assetFileNamesToKeep.add(fileName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cleanupAssetDir(prjctCd, assetFileNamesToKeep);
|
||||||
|
return doc.body().html();
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.warn("contact scenario asset sync 실패. prjctCd={}, rawHtml={}", prjctCd, rawHtml, e);
|
||||||
|
return rawHtml;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void deleteTempPath(Path file) throws IOException {
|
||||||
|
Files.deleteIfExists(file.toAbsolutePath().normalize());
|
||||||
|
}
|
||||||
|
|
||||||
|
public Path getTempDir(String prjctCd) {
|
||||||
|
return getBaseDir().resolve(prjctCd).resolve("Temp").toAbsolutePath().normalize();
|
||||||
|
}
|
||||||
|
|
||||||
|
private Path getAssetDir(String prjctCd) {
|
||||||
|
return getBaseDir().resolve(prjctCd).resolve("Asset").toAbsolutePath().normalize();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void moveTempToAsset(String prjctCd, String fileName) throws IOException {
|
||||||
|
moveFile(getTempDir(prjctCd), getAssetDir(prjctCd), fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void cleanupAssetDir(String prjctCd, Set<String> keepFileNames) throws IOException {
|
||||||
|
Path assetDir = getAssetDir(prjctCd);
|
||||||
|
if (!Files.exists(assetDir)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try (DirectoryStream<Path> stream = Files.newDirectoryStream(assetDir)) {
|
||||||
|
for (Path path : stream) {
|
||||||
|
if (!Files.isRegularFile(path)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
String fileName = path.getFileName().toString();
|
||||||
|
if (!keepFileNames.contains(fileName)) {
|
||||||
|
Files.deleteIfExists(path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String normalizeBaseUrl(String url) {
|
||||||
|
if (url == null || url.isBlank()) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
return url.endsWith("/") ? url.substring(0, url.length() - 1) : url;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -49,8 +49,12 @@ kri:
|
|||||||
version: "v1.0.0"
|
version: "v1.0.0"
|
||||||
base-package: "com.kri.contact.server"
|
base-package: "com.kri.contact.server"
|
||||||
|
|
||||||
|
file:
|
||||||
|
list-base-dir: ./upload-file/list
|
||||||
|
list-item-base-dir: ./upload-file/list-item
|
||||||
|
contact-senario-base-dir: ./upload-file/contact-senario
|
||||||
|
|
||||||
contact-senario:
|
contact-senario:
|
||||||
upload-base-dir: ./upload-file/contact-senario
|
|
||||||
public-base-url: /contact-senario
|
public-base-url: /contact-senario
|
||||||
temp-expire-hours: 24
|
temp-expire-hours: 24
|
||||||
temp-cleanup-cron: "0 0 0 * * *" # 매일 00:00(초 분 시 일 월 요일)
|
temp-cleanup-cron: "0 0 0 * * *" # 매일 00:00(초 분 시 일 월 요일)
|
||||||
|
|||||||
Reference in New Issue
Block a user