| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468 |
- package jnpf.util;
- import cn.hutool.core.bean.BeanUtil;
- import cn.hutool.core.io.file.FileNameUtil;
- import cn.hutool.core.util.StrUtil;
- import jnpf.constant.GenerateConstant;
- import jnpf.constant.MsgCode;
- import jnpf.entity.FileDetail;
- import jnpf.entity.FileParameter;
- import jnpf.exception.DataException;
- import jnpf.model.FileListVO;
- import jnpf.util.context.SpringContext;
- import lombok.extern.slf4j.Slf4j;
- import org.dromara.x.file.storage.core.FileInfo;
- import org.dromara.x.file.storage.core.FileStorageProperties;
- import org.dromara.x.file.storage.core.FileStorageService;
- import org.dromara.x.file.storage.core.get.*;
- import org.dromara.x.file.storage.core.platform.FileStorage;
- import org.dromara.x.file.storage.core.upload.UploadPretreatment;
- import org.dromara.x.file.storage.core.util.Tools;
- import org.dromara.x.file.storage.spring.SpringFileStorageProperties;
- import org.springframework.util.Assert;
- import org.springframework.util.ReflectionUtils;
- import org.springframework.web.multipart.MultipartFile;
- import javax.imageio.ImageIO;
- import java.io.File;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.lang.reflect.Field;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- import java.util.Objects;
- import java.util.function.Consumer;
- @Slf4j
- public class FileUploadUtils {
- private static final String PLATFORM_LOCAL = "local-plus-1";
- private static FileStorageService fileStorageService;
- // private static FileDetailService fileDetailService;
- private static SpringFileStorageProperties fileStorageProperties;
- private static List<String> imagesReaderSuffix;
- private static List<String> imagesWriterSuffix;
- static {
- fileStorageService = SpringContext.getBean(FileStorageService.class);
- // fileDetailService = SpringContext.getBean(FileDetailService.class);
- fileStorageProperties = SpringContext.getBean(SpringFileStorageProperties.class);
- //获取支持读取的图片后缀
- imagesReaderSuffix = Arrays.asList(ImageIO.getReaderFormatNames());
- //获取支持写入的图片后缀
- imagesWriterSuffix = Arrays.asList(ImageIO.getWriterFormatNames());
- }
- public static FileStorageProperties.LocalPlusConfig getLocalStorageConfig() {
- List<? extends FileStorageProperties.LocalPlusConfig> localPlus = fileStorageProperties.getLocalPlus();
- Assert.noNullElements(localPlus, "未配置本地存储");
- return localPlus.get(0);
- }
- /**
- * 获取文件信息
- *
- * @return
- */
- public static FileDetail getFileDetail(FileParameter parameter) {
- FileDetail fileDetail = new FileDetail();
- fileDetail.setPath(parameter.getRemotePath());
- fileDetail.setFilename(parameter.getRemoteFileName());
- String basePath;
- String platform;
- FileStorage fileStorage = fileStorageService.getFileStorage();
- if (parameter.isOrgin()) {
- basePath = getLocalBasePath();
- platform = PLATFORM_LOCAL;
- } else {
- basePath = getBasePath(fileStorage);
- platform = getDefaultPlatform();
- }
- fileDetail.setBasePath(basePath);
- fileDetail.setPlatform(platform);
- return fileDetail;
- }
- // /**
- // * 获取文件url
- // *
- // * @param path
- // * @param fileName
- // * @return
- // */
- // public static String getFileUrl(String path, String fileName) {
- // FileDetail fileDetail = getFileDetail(path, fileName, );
- // String url = null;
- // if (fileDetail == null) {
- // url = fileStorageService.getFileStorage().getBasePath() + path + fileName;
- // } else {
- // url = fileDetail.getUrl();
- // }
- // return url;
- // }
- /**
- * 返回本地地址且固定为local-plus-1
- *
- * @return
- */
- public static String getLocalBasePath() {
- FileStorageProperties.LocalPlusConfig localStorageConfig = getLocalStorageConfig();
- return Tools.getNotNull(localStorageConfig.getStoragePath(), StrUtil.EMPTY)
- + Tools.getNotNull(localStorageConfig.getBasePath(), StrUtil.EMPTY);
- }
- /**
- * 获取指定存储平台基础路径
- *
- * @return
- */
- public static String getBasePath(FileStorage fileStorage) {
- try {
- if (fileStorage != null) {
- Field field = ReflectionUtils.findField(fileStorage.getClass(), "basePath");
- if (field != null) {
- ReflectionUtils.makeAccessible(field);
- String basePath = (String) ReflectionUtils.getField(field, fileStorage);
- if (StringUtil.isNotEmpty(basePath)) {
- return basePath;
- }
- }
- }
- } catch (Exception e) {
- }
- return "";
- }
- /**
- * 获取当前存储平台基础路径
- *
- * @return
- */
- public static String getBasePath() {
- return getBasePath(fileStorageService.getFileStorage());
- }
- /**
- * 获取文件信息
- *
- * @return
- */
- public static FileInfo getFileInfo(FileParameter parameter) {
- FileDetail fileDetail = getFileDetail(parameter);
- return BeanUtil.copyProperties(fileDetail, FileInfo.class, "attr");
- }
- public static FileInfo getRemoteFileInfo(FileParameter parameter) {
- process(parameter);
- GetFilePretreatment getFilePretreatment = fileStorageService.getFile();
- getFilePretreatment.setPath(parameter.getRemotePath());
- getFilePretreatment.setFilename(parameter.getRemoteFileName());
- RemoteFileInfo remoteFileInfo = getFilePretreatment.getFile();
- return remoteFileInfo.toFileInfo();
- }
- /**
- * 上传文件,通过字节数组
- * 减少使用, 无法上传大于2G的文件
- *
- * @param parameter 文件参数
- * @param bytes 内容
- */
- public static FileInfo uploadFile(FileParameter parameter, byte[] bytes) {
- process(parameter);
- UploadPretreatment uploadPretreatment = fileStorageService.of(bytes);
- FileInfo fileInfo = uploadInner(parameter, uploadPretreatment);
- Assert.notNull(fileInfo, MsgCode.FA033.get());
- return fileInfo;
- }
- /**
- * 上传文件,MultipartFile
- *
- * @param parameter 文件参数
- * @param multipartFile 文件
- */
- public static FileInfo uploadFile(FileParameter parameter, MultipartFile multipartFile) {
- process(parameter);
- UploadPretreatment uploadPretreatment = fileStorageService.of(multipartFile);
- FileInfo fileInfo = uploadInner(parameter, uploadPretreatment);
- Assert.notNull(fileInfo, MsgCode.FA033.get());
- return fileInfo;
- }
- /**
- * 上传文件,MultipartFile
- *
- * @param parameter 文件参数
- * @param inputStream 文件
- */
- public static FileInfo uploadFile(FileParameter parameter, InputStream inputStream) {
- process(parameter);
- UploadPretreatment uploadPretreatment = fileStorageService.of(inputStream);
- FileInfo fileInfo = uploadInner(parameter, uploadPretreatment);
- Assert.notNull(fileInfo, MsgCode.FA033.get());
- return fileInfo;
- }
- /**
- * 上传文件,MultipartFile
- *
- * @param parameter 文件参数
- * @param file 文件
- */
- public static FileInfo uploadFile(FileParameter parameter, File file) {
- process(parameter);
- UploadPretreatment uploadPretreatment = fileStorageService.of(file);
- FileInfo fileInfo = uploadInner(parameter, uploadPretreatment);
- Assert.notNull(fileInfo, MsgCode.FA033.get());
- return fileInfo;
- }
- private static FileInfo uploadInner(FileParameter parameter, UploadPretreatment uploadPretreatment) {
- process(parameter);
- uploadPretreatment.setPath(parameter.getRemotePath())
- //设置上传文件名
- //.setSaveFilename(StringUtil.isNotEmpty(parameter.getRemoteFileName()), parameter.getRemoteFileName())
- .setOriginalFilename(StringUtil.isNotEmpty(parameter.getRemoteFileName()), parameter.getRemoteFileName());
- if (parameter.isThumbnail()) {
- String ext = FileNameUtil.getSuffix(StringUtil.isNotEmpty(parameter.getLocalFileName()) ? parameter.getLocalFileName() : parameter.getRemoteFileName());
- if (imagesReaderSuffix.contains(ext.toLowerCase())) {
- if (!imagesWriterSuffix.contains(ext.toLowerCase())) {
- if (log.isDebugEnabled()) {
- log.debug("不支持当前配置的缩略图格式:{}", fileStorageProperties.getThumbnailSuffix());
- }
- } else {
- try {
- uploadPretreatment.thumbnail(120, 120);
- } catch (Exception e) {
- if (log.isDebugEnabled()) {
- log.debug("生成缩略图失败: {}", e.getMessage());
- }
- }
- }
- }
- }
- return uploadPretreatment.upload();
- }
- /**
- * 获取文件列表
- */
- public static List<FileListVO> getFileList(FileParameter parameter) {
- FileStorage fileStorage = fileStorageService.getFileStorage();
- if (!fileStorage.isSupportListFiles().getIsSupport()) {
- throw new DataException(MsgCode.FA056.get());
- }
- process(parameter);
- List<FileListVO> listVOS = new ArrayList<>();
- getFileListInner(parameter, fileStorage, listVOS);
- return listVOS;
- }
- private static void getFileListInner(FileParameter parameter, FileStorage fileStorage, List<FileListVO> resultFiles) {
- process(parameter);
- ListFilesPretreatment listFilesPretreatment = new ListFilesPretreatment();
- listFilesPretreatment.setPath(parameter.getRemotePath());
- listFilesPretreatment.setMaxFiles(fileStorage.isSupportListFiles().getSupportMaxFiles());
- ListFilesResult listFilesResult = fileStorage.listFiles(listFilesPretreatment);
- List<RemoteFileInfo> fileList = listFilesResult.getFileList();
- for (int i = 0; i < fileList.size(); i++) {
- RemoteFileInfo remoteFileInfo = fileList.get(i);
- FileListVO fileListVO = new FileListVO();
- fileListVO.setFileId(i + "");
- fileListVO.setFileName(remoteFileInfo.getFilename());
- fileListVO.setFilePath(remoteFileInfo.getPath());
- fileListVO.setFileType(remoteFileInfo.getExt());
- fileListVO.setFileSize(String.valueOf(remoteFileInfo.getSize()));
- fileListVO.setFileTime(DateUtil.dateFormat(remoteFileInfo.getLastModified()));
- resultFiles.add(fileListVO);
- }
- if (parameter.isRecursive()) {
- List<RemoteDirInfo> dirList = listFilesResult.getDirList();
- for (RemoteDirInfo remoteDirInfo : dirList) {
- parameter.setRemotePath(remoteDirInfo.getPath() + remoteDirInfo.getName() + "/");
- getFileListInner(parameter, fileStorage, resultFiles);
- }
- }
- }
- /**
- * 获取命名空间
- */
- // public static String getBucketName() {
- // String bucketName = fileStorageService.getFileStorage().getBucketName();
- // if (StringUtil.isNotEmpty(bucketName)) {
- // return bucketName + "/";
- // }
- // return bucketName;
- // return "";
- // }
- /**
- * 获取命名空间
- */
- // public static String getDomain() {
- // return fileStorageService.getFileStorage().getDomain();
- // return "";
- // }
- /**
- * 删除文件
- */
- public static boolean removeFile(FileParameter parameter) {
- process(parameter);
- FileDetail fileDetail = getFileDetail(parameter);
- return fileStorageService.delete(fileDetail.getUrl());
- }
- // /**
- // * 下载文件
- // *
- // * @param path
- // * @param fileName
- // */
- // public static void downloadFile(String path, String fileName) {
- // String fileUrl = getFileUrl(path, fileName);
- // FileInfo fileInfo = fileStorageService.getFileInfoByUrl(fileUrl);
- // fileStorageService.download(fileInfo).file(fileUrl);
- // }
- /**
- * 下载文件
- */
- public static File downloadFileToLocal(FileParameter parameter) {
- process(parameter);
- if (StringUtil.isEmpty(parameter.getLocalFileName())) {
- parameter.setLocalFileName(parameter.getRemoteFileName());
- }
- if (StringUtil.isEmpty(parameter.getLocaFilelPath())) {
- parameter.setLocaFilelPath(parameter.getRemotePath());
- }
- FileStorageProperties.LocalPlusConfig localStorageConfig = getLocalStorageConfig();
- String localPath = Tools.getNotNull(localStorageConfig.getStoragePath(), StrUtil.EMPTY)
- + Tools.getNotNull(localStorageConfig.getBasePath(), StrUtil.EMPTY)
- + Tools.getNotNull(parameter.getLocaFilelPath(), StrUtil.EMPTY);
- FileInfo fileInfo = getFileInfo(parameter);
- File localFile = new File(localPath, parameter.getLocalFileName());
- if(!fileStorageProperties.getDefaultPlatform().startsWith(GenerateConstant.LOCAL)
- || !Objects.equals(parameter.getRemotePath(), parameter.getLocaFilelPath())
- || !Objects.equals(parameter.getRemoteFileName(), parameter.getLocalFileName())) {
- fileStorageService.download(fileInfo).file(localFile);
- }
- return localFile;
- }
- /**
- * 下载文件得到字节数组
- */
- public static byte[] downloadFile(FileParameter parameter) {
- process(parameter);
- FileInfo fileInfo = getFileInfo(parameter);
- return fileStorageService.download(fileInfo).bytes();
- }
- /**
- * 下载文件到文件
- */
- public static void downloadFile(FileParameter parameter, File targetFile) {
- process(parameter);
- FileInfo fileInfo = getFileInfo(parameter);
- fileStorageService.download(fileInfo).file(targetFile);
- }
- /**
- * 下载文件到输出流
- */
- public static void downloadFile(FileParameter parameter, OutputStream outputStream) {
- process(parameter);
- FileInfo fileInfo = getFileInfo(parameter);
- fileStorageService.download(fileInfo).outputStream(outputStream);
- }
- /**
- * 下载文件到输入流, 自定义处理
- */
- public static void downloadFile(FileParameter parameter, Consumer<InputStream> fileConsumer) {
- process(parameter);
- FileInfo fileInfo = getFileInfo(parameter);
- fileStorageService.download(fileInfo).inputStream(fileConsumer);
- }
- // /**
- // * 下载文件得到流
- // *
- // * @param path
- // * @param fileName
- // * @param origin
- // */
- // public static ByteArrayOutputStream downloadFilStream(String path, String fileName, boolean origin) {
- // FileInfo fileInfo = getFileInfo(path, fileName, origin);
- // ByteArrayOutputStream out = new ByteArrayOutputStream();
- // fileStorageService.download(fileInfo).outputStream(out);
- // return out;
- // }
- /**
- * 默认存储平台
- *
- * @param
- * @return
- */
- public static String getDefaultPlatform() {
- return fileStorageProperties.getDefaultPlatform();
- }
- /**
- * 判断文件是否存在
- *
- * @return
- */
- public static boolean exists(FileParameter parameter) {
- process(parameter);
- FileInfo fileInfo = getFileInfo(parameter);
- return fileStorageService.exists(fileInfo);
- }
- public static void process(FileParameter parameter) {
- String typePath = FilePathUtil.getFilePath(parameter.getRemotePath());
- String fileName = parameter.getRemoteFileName();
- if (StringUtil.isNotEmpty(fileName)) {
- if(fileName.contains(",")) {
- typePath += fileName.substring(0, fileName.lastIndexOf(",") + 1).replaceAll(",", "/");
- fileName = fileName.substring(fileName.lastIndexOf(",") + 1);
- }
- if(fileName.startsWith("/")){
- fileName = fileName.substring(1);
- }
- }
- typePath = XSSEscape.escapePath(typePath);
- fileName = XSSEscape.escapePath(fileName);
- if(!typePath.endsWith("/")){
- typePath += "/";
- }
- parameter.setRemotePath(typePath);
- parameter.setRemoteFileName(fileName);
- }
- /**
- * 根据路径和文件名删除文件
- *
- * @return
- */
- public static boolean deleteFileByPathAndFileName(FileParameter parameter) {
- FileInfo fileInfo = getFileInfo(parameter);
- return fileStorageService.delete(fileInfo);
- }
- }
|