FAISP/src/main/java/com/dbnt/faisp/config/BaseService.java

87 lines
2.5 KiB
Java

package com.dbnt.faisp.config;
import lombok.RequiredArgsConstructor;
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;
@Service
@RequiredArgsConstructor
public class BaseService {
@Value("${spring.servlet.multipart.location}")
protected String locationPath;
@Value("${file.dir.publicBoard}")
protected String publicBoardPath;
@Value("${file.dir.faRpt}")
protected String faRptPath;
@Value("${file.dir.vulnerable}")
protected String vulnerablePath;
@Value("${file.dir.part}")
protected String partPath;
@Value("${file.dir.equip}")
protected String equipPath;
@Value("${file.dir.sailor}")
protected String sailorPath;
@Value("${file.dir.affair}")
protected String affairPath;
@Value("${file.dir.affair.plan}")
protected String affairPlanPath;
@Value("${file.dir.affair.result}")
protected String affairResultPath;
@Value("${file.dir.editor}")
protected String editorPath;
@Value("${file.dir.sri}")
protected String sriPath;
@Value("${file.dir.ciw}")
protected String ciwPath;
@Value("${file.dir.cia.safty}")
protected String ciaSaftyPath;
@Value("${file.dir.cia.company}")
protected String ciaCompanyPath;
@Value("${file.dir.cia.foreigner}")
protected String ciaForeignerPath;
@Value("${file.dir.cia.edu}")
protected String ciaEduPath;
@Value("${file.dir.activityCase}")
protected String activityCasePath;
@Value("${file.dir.majorStatus}")
protected String majorStatusPath;
protected String calculationSize(double fileSize){
String[] units = {"bytes", "KB", "MB", "GB", "TB", "PB"};
double unitSelector = Math.floor(Math.log(fileSize)/Math.log(1024));
if(fileSize>0){
return Math.round((fileSize/Math.pow(1024, unitSelector))*100)/100d+" "+units[(int)unitSelector];
}else{
return "";
}
}
protected void deleteStoredFile(File deleteFile){
deleteFile.delete();
}
protected void saveFile(MultipartFile file, File saveFile){
if(file.getSize()!=0){ // 저장될 파일 확인
if(!saveFile.exists()){ // 저장될 경로 확인
if(saveFile.getParentFile().mkdirs()){
try{
saveFile.createNewFile();
}catch (IOException e){
e.printStackTrace();
}
}
}
try {
file.transferTo(saveFile);
}catch (IllegalStateException | IOException e){
e.printStackTrace();
}
}
}
}