package foundation.io; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import org.apache.commons.io.FileUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import foundation.io.object.FileItemAgent; import foundation.server.config.Configer; import foundation.token.IOnlineUser; import foundation.token.IUser; import foundation.util.Util; public class FileRepository { protected static Logger logger; public static String Default_Template = "empty.xlsx"; static { logger = LogManager.getLogger(FileRepository.class); } //获取目录--模板存放目录 public static File getTemplatePath() { String path = Configer.getParam("PathRepository") + "template/"; File file = new File(path); if (!file.exists()) { file.mkdirs(); } return file; } //获取目录--用户临时目录 public static File getUserTempPath(IUser user) { String path = Configer.getParam("PathRepository") + "temp/" + user.getCode() + "/" + Util.newDateTimeStr("yyyy/MM") + "/"; File file = new File(path); if (!file.exists()) { file.mkdirs(); } return file; } public static String urlToPath(String fileURL) { String path = Configer.getParam("PathRepository") + fileURL; return path; } public static String pathToURL(File file) { String root = new File(Configer.getParam("PathRepository")).toString(); String path = file.toString(); path = path.substring(root.length() + 1); return path; } //获取目录--上载文件目录 public static File getUploadPath(IOnlineUser user) throws IOException { String userName = user.getName(); String path = Configer.getParam("PathRepository") + "upload/" + userName + Util.newDateTimeStr("/yyyy/MM"); File file = new File(path); if (!file.exists()) { FileUtils.forceMkdir(file); } return file; } //获取文件--模板文件 public static File getTemplateFile(String templateName) { File template = new File(getTemplatePath(), templateName); return template; } //创建文件--上载临时文件 public static File createUploadRepositoryFile(IOnlineUser user, String fileName) throws Exception { if (Util.isEmpty(fileName)) { throw new Exception("can not create upload repository file: file name is empty"); } //1. path File path = getUploadPath(user); //2. file name fileName = addTimeStamp(fileName); File file = new File(path, fileName); return file; } //创建文件--数据导入临时文件 public static File createImportTempFile(IUser user, FileItemAgent fileForm) throws Exception { //1. get user temporary path File tempPath = getUserTempPath(user); //2. get file String fileName = fileForm.getName(); fileName = addTimeStamp(fileName); File tempFile = new File(tempPath, fileName); //3. save to file OutputStream outputStream = new FileOutputStream(tempFile); InputStream inputStream = fileForm.getInputStream(); try { byte buf[] = new byte[1024]; int length = 0; while((length = inputStream.read(buf)) > 0){ outputStream.write(buf, 0, length); } } finally { try { outputStream.flush(); outputStream.close(); } catch (Exception e) { } try { inputStream.close(); } catch (Exception e) { } } return tempFile; } //创建文件--下载临时文件 public static File createExportTempFile(IUser user, String templateName, String fileName) throws Exception { //1. get template if (Util.isEmpty(templateName)) { templateName = Default_Template; } File template = new File(getTemplatePath(), clearVariants(templateName)); if (!template.exists()) { throw new Exception("template file not exists: " + template); } //2. get temporary path File tempPath = getUserTempPath(user); //3. get temporary file fileName = fillVariants(fileName); fileName = addSuffix(fileName, templateName); File tempFile = new File(tempPath, fileName); if (tempFile.exists()) { tempFile.delete(); } //4. copy template to FileUtils.copyFile(template, tempFile); return tempFile; } //创建文件--模板注入临时文件 public static File createInjectTempFile(IUser user, String templateName) throws Exception { //1. get user temporary path File tempPath = getUserTempPath(user); //2. get template templateName = addTimeStamp(templateName); File tempFile = new File(tempPath, templateName); if (tempFile.exists()) { tempFile.delete(); } return tempFile; } private static String clearVariants(String fileName) { //KA医院管理-@{YYYYMMdd} if (fileName == null) { return null; } fileName = fileName.trim(); int begin = fileName.indexOf("@{"); if (begin <= 0) { return fileName; } int end = fileName.indexOf("}"); if (end <= 0) { end = fileName.length(); } String result = fileName.substring(0, begin) + fileName.substring(end + 1); return result; } public static String addTimeStamp(String fileName) { //KA医院管理-20230102-101234180 int pos = fileName.lastIndexOf("."); if (pos < 0) { return fileName; } String timeStamp = Util.newDateTimeStr("-yyyyMMdd-kkmmssSSS"); String result = fileName.substring(0, pos) + timeStamp + fileName.substring(pos); return result; } public static String fillVariants(String fileName) { //KA医院管理-@{YYYYMMdd} if (fileName == null) { return null; } fileName = fileName.trim(); int begin = fileName.indexOf("@{"); if (begin <= 0) { return fileName; } int end = fileName.indexOf("}"); if (end <= 0) { end = fileName.length(); } String result = fileName.substring(0, begin) + parseVariant(fileName.substring(begin + 2, end)) + fileName.substring(end + 1); return result; } private static String addSuffix(String fileName, String templateName) { int pos = fileName.lastIndexOf("."); if (pos > 0) { return fileName; } pos = templateName.lastIndexOf("."); if (pos < 0) { return fileName; } String suffix = templateName.substring(pos, templateName.length()); fileName = fileName + suffix; return fileName; } private static String parseVariant(String value) { return Util.newDateTimeStr(value); } }