package com.walker.file; import com.walker.infrastructure.utils.FileUtils; import com.walker.infrastructure.utils.LongCalendar; import com.walker.infrastructure.utils.NumberGenerator; import com.walker.infrastructure.utils.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; import java.io.InputStream; public abstract class AbstractFileOperateEngine implements FileOperateEngine{ protected final transient Logger logger = LoggerFactory.getLogger(getClass()); @Override public void close() { } @Override public byte[] downloadFile(String id) throws FileOperateException{ if(StringUtils.isEmpty(id)){ throw new IllegalArgumentException("文件id不能为空!"); } FileInfo fileInfo = this.getFileInfo(id); if(fileInfo == null){ throw new FileOperateException("文件不存在, fileId=" + id, null); } return this.executeDownload(fileInfo); } @Deprecated @Override public FileInfo uploadFile(InputStream inputStream, String fileName, String groupId, long fileSize) throws FileOperateException{ return this.uploadFile(inputStream, fileName, groupId, fileSize, null, "-1"); } @Override public FileInfo[] uploadFile(InputStream[] inputStream , String[] fileName, String groupId, long[] fileSize, Integer businessType, String owner) throws FileOperateException{ if(inputStream == null || fileName == null || fileSize == null){ throw new IllegalArgumentException("上传多个文件,参数为空"); } if(inputStream.length != fileName.length || inputStream.length != fileSize.length){ throw new IllegalArgumentException("上传多个文件,inputStream, fileName, fileSize个数必须一致!"); } int size = inputStream.length; FileInfo[] fileInfos = new FileInfo[size]; for(int i=0; i * 1.对于ftp,url为全路径,因为根路径已在服务中固定,如: /fileRoot/2023/02/ * 2.对于磁盘,url为相对路径,根路径依据配置动态拼接,如: 2023/02/ * * @param containRoot 是否包含根路径 * @return */ protected String generateSaveFolder(boolean containRoot){ if(StringUtils.isEmpty(this.fileRoot)){ throw new IllegalArgumentException("请设置根目录:fileRoot"); } StringBuilder filePath = new StringBuilder(); if(containRoot){ filePath.append(this.fileRoot); if(!this.fileRoot.endsWith("/")){ filePath.append("/"); } filePath.append(LongCalendar.getCurrentMonth()); return filePath.toString(); } else { // 本地文件存储,不存根路径,只要相对路径 return LongCalendar.getCurrentMonth(); } } /** * 返回文件名,不带路径,如: demo.txt * @param fileInfo * @return */ protected String getFileName(FileInfo fileInfo){ return new StringBuilder(fileInfo.getId()) .append(StringUtils.EXTENSION_SEPARATOR).append(fileInfo.getFileExt()).toString(); } public String getFileRoot() { return fileRoot; } @Override public void setFileRoot(String fileRoot) { this.fileRoot = fileRoot; } private String fileRoot = null; /** * 获得上传文件结果对象。 * @param fileName 文件名,如: demo.txt * @return */ protected abstract FileInfo acquireFileInfo(String fileName); /** * 执行上传文件动作,由子类实现。 * @param inputStream */ protected abstract void executeUpload(InputStream inputStream, FileInfo fileInfo) throws FileOperateException; /** * 把上传成功的文件信息写入到持久层(一般为数据库)中。 * @param fileInfo * @throws FileOperateException */ protected abstract void writeFileInfo(FileInfo fileInfo); /** * 执行文件下载操作,并返回文件二进制数据。 * @param fileInfo * @return * @throws FileOperateException * @date 2023-02-14 */ protected abstract byte[] executeDownload(FileInfo fileInfo) throws FileOperateException; }