代码优化
This commit is contained in:
parent
693e63ca0a
commit
8177a09719
@ -0,0 +1,20 @@
|
||||
package com.qiwenshare.common.config;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.env.Environment;
|
||||
|
||||
@Configuration
|
||||
public class PropertiesConfig {
|
||||
|
||||
@Resource
|
||||
private Environment env;
|
||||
|
||||
@PostConstruct
|
||||
public void setProperties() {
|
||||
PropertiesUtil.setEnvironment(env);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package com.qiwenshare.common.config;
|
||||
|
||||
import org.springframework.core.env.Environment;
|
||||
|
||||
public class PropertiesUtil {
|
||||
|
||||
private static Environment env = null;
|
||||
|
||||
public static void setEnvironment(Environment env) {
|
||||
PropertiesUtil.env = env;
|
||||
}
|
||||
|
||||
public static String getProperty(String key) {
|
||||
return PropertiesUtil.env.getProperty(key);
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -13,6 +13,7 @@ public class QiwenFileConfig {
|
||||
|
||||
private String storageType;
|
||||
private String cacheMode;
|
||||
private String localStoragePath;
|
||||
|
||||
private AliyunConfig aliyun = new AliyunConfig();
|
||||
|
||||
|
@ -1,9 +1,11 @@
|
||||
package com.qiwenshare.common.operation.upload;
|
||||
|
||||
import com.qiwenshare.common.config.PropertiesUtil;
|
||||
import com.qiwenshare.common.domain.UploadFile;
|
||||
import com.qiwenshare.common.util.PathUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.io.File;
|
||||
@ -14,9 +16,11 @@ import java.security.SecureRandom;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.ResourceBundle;
|
||||
|
||||
@Slf4j
|
||||
public abstract class Uploader {
|
||||
|
||||
|
||||
public static final String ROOT_PATH = "upload";
|
||||
public static final String FILE_SEPARATOR = "/";
|
||||
@ -31,10 +35,16 @@ public abstract class Uploader {
|
||||
* @return
|
||||
*/
|
||||
protected String getSaveFilePath() {
|
||||
|
||||
String path = ROOT_PATH;
|
||||
SimpleDateFormat formater = new SimpleDateFormat("yyyyMMdd");
|
||||
path = FILE_SEPARATOR + path + FILE_SEPARATOR + formater.format(new Date());
|
||||
File dir = new File(PathUtil.getStaticPath() + path);
|
||||
String localStoragePath = PropertiesUtil.getProperty("qiwen-file.local-storage-path");
|
||||
String staticPath = PathUtil.getStaticPath();
|
||||
if (StringUtils.isNotEmpty(localStoragePath)) {
|
||||
staticPath = localStoragePath;
|
||||
}
|
||||
File dir = new File(staticPath + path);
|
||||
//LOG.error(PathUtil.getStaticPath() + path);
|
||||
if (!dir.exists()) {
|
||||
try {
|
||||
|
@ -9,17 +9,10 @@ import java.util.List;
|
||||
|
||||
public interface IFileService extends IService<FileBean> {
|
||||
|
||||
|
||||
// void batchInsertFile(List<FileBean> fileBeanList, Long userId);
|
||||
//void updateFile(FileBean fileBean);
|
||||
|
||||
void increaseFilePointCount(Long fileId);
|
||||
|
||||
void decreaseFilePointCount(Long fileId);
|
||||
|
||||
// List<FileBean> selectFileListByPath(FileBean fileBean);
|
||||
|
||||
// void deleteLocalFile(FileBean fileBean);
|
||||
|
||||
|
||||
|
||||
|
@ -164,10 +164,6 @@ public class FileController {
|
||||
} else {
|
||||
FileBean file = fileService.getById(userFile.getFileId());
|
||||
if (file.getIsOSS() == 1 || file.getStorageType() == 1) {
|
||||
// LambdaQueryWrapper<UserFile> lambdaQueryWrapper = new LambdaQueryWrapper<>();
|
||||
// lambdaQueryWrapper.eq(UserFile::getUserFileId, renameFileDto.getUserFileId());
|
||||
// UserFile userFile = userFileService.getOne(lambdaQueryWrapper);
|
||||
|
||||
|
||||
String fileUrl = file.getFileUrl();
|
||||
String newFileUrl = fileUrl.replace(userFile.getFileName(), renameFileDto.getFileName());
|
||||
@ -297,12 +293,7 @@ public class FileController {
|
||||
}
|
||||
|
||||
UserBean sessionUserBean = userService.getUserBeanByToken(token);
|
||||
// String uuid = UUID.randomUUID().toString();
|
||||
|
||||
// UserFile userFile = new UserFile();
|
||||
// userFile.setUserFileId(deleteFileDto.getUserFileId());
|
||||
//// userFile.setDeleteBatchNum(uuid);
|
||||
// BeanUtil.copyProperties(deleteFileDto, userFile);
|
||||
userFileService.deleteUserFile(deleteFileDto.getUserFileId(), sessionUserBean.getUserId());
|
||||
|
||||
|
||||
|
@ -1,18 +1,14 @@
|
||||
package com.qiwenshare.file.controller;
|
||||
|
||||
import com.github.tobato.fastdfs.proto.storage.DownloadByteArray;
|
||||
import com.github.tobato.fastdfs.service.FastFileStorageClient;
|
||||
import com.qiwenshare.common.util.DateUtil;
|
||||
import com.qiwenshare.common.operation.FileOperation;
|
||||
import com.qiwenshare.common.util.FileUtil;
|
||||
import com.qiwenshare.common.config.QiwenFileConfig;
|
||||
import com.qiwenshare.common.result.RestResult;
|
||||
import com.qiwenshare.common.util.PathUtil;
|
||||
import com.qiwenshare.common.util.DateUtil;
|
||||
import com.qiwenshare.common.util.FileUtil;
|
||||
import com.qiwenshare.file.anno.MyLog;
|
||||
import com.qiwenshare.file.api.IFileService;
|
||||
import com.qiwenshare.file.api.IFiletransferService;
|
||||
import com.qiwenshare.file.api.IUserFileService;
|
||||
import com.qiwenshare.file.api.IUserService;
|
||||
import com.qiwenshare.common.config.QiwenFileConfig;
|
||||
import com.qiwenshare.file.domain.FileBean;
|
||||
import com.qiwenshare.file.domain.StorageBean;
|
||||
import com.qiwenshare.file.domain.UserBean;
|
||||
@ -26,10 +22,8 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.ServletOutputStream;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.*;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@ -53,8 +47,6 @@ public class FiletransferController {
|
||||
IUserService userService;
|
||||
@Resource
|
||||
IUserFileService userFileService;
|
||||
@Autowired
|
||||
private FastFileStorageClient fastFileStorageClient;
|
||||
public static final String CURRENT_MODULE = "文件传输接口";
|
||||
|
||||
@Operation(summary = "极速上传", description = "校验文件MD5判断文件是否存在,如果存在直接上传成功并返回skipUpload=true,如果不存在返回skipUpload=false需要再次调用该接口的POST方法", tags = {"filetransfer"})
|
||||
@ -142,68 +134,6 @@ public class FiletransferController {
|
||||
filetransferService.downloadFile(response, downloadFileDTO);
|
||||
}
|
||||
|
||||
private void localFileDownload(HttpServletResponse response, FileBean fileBean) {
|
||||
BufferedInputStream bis = null;
|
||||
byte[] buffer = new byte[1024];
|
||||
//设置文件路径
|
||||
File file = FileOperation.newFile(PathUtil.getStaticPath() + fileBean.getFileUrl());
|
||||
if (file.exists()) {
|
||||
|
||||
|
||||
FileInputStream fis = null;
|
||||
|
||||
try {
|
||||
fis = new FileInputStream(file);
|
||||
bis = new BufferedInputStream(fis);
|
||||
OutputStream os = response.getOutputStream();
|
||||
int i = bis.read(buffer);
|
||||
while (i != -1) {
|
||||
os.write(buffer, 0, i);
|
||||
i = bis.read(buffer);
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (bis != null) {
|
||||
try {
|
||||
bis.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public void fastFDSDownload(HttpServletResponse response, FileBean fileBean){
|
||||
String group = fileBean.getFileUrl().substring(0, fileBean.getFileUrl().indexOf("/"));
|
||||
String path = fileBean.getFileUrl().substring(fileBean.getFileUrl().indexOf("/") + 1);
|
||||
DownloadByteArray downloadByteArray = new DownloadByteArray();
|
||||
byte[] bytes = fastFileStorageClient.downloadFile(group, path, downloadByteArray);
|
||||
|
||||
// // 这里只是为了整合fastdfs,所以写死了文件格式。需要在上传的时候保存文件名。下载的时候使用对应的格式
|
||||
// response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode("sb.xlsx", "UTF-8"));
|
||||
// response.setCharacterEncoding("UTF-8");
|
||||
ServletOutputStream outputStream = null;
|
||||
try {
|
||||
outputStream = response.getOutputStream();
|
||||
outputStream.write(bytes);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
outputStream.flush();
|
||||
outputStream.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Operation(summary = "获取存储信息", description = "获取存储信息", tags = {"filetransfer"})
|
||||
@RequestMapping(value = "/getstorage", method = RequestMethod.GET)
|
||||
|
@ -9,7 +9,7 @@ import javax.persistence.*;
|
||||
|
||||
@Data
|
||||
@Table(name = "userfile", uniqueConstraints = {
|
||||
@UniqueConstraint(name = "fileindex", columnNames = {"fileName", "filePath", "extendName", "deleteFlag"})})
|
||||
@UniqueConstraint(name = "fileindex", columnNames = {"fileName", "filePath", "extendName", "deleteFlag", "userId"})})
|
||||
@Entity
|
||||
@TableName("userfile")
|
||||
public class UserFile {
|
||||
|
@ -1,14 +1,10 @@
|
||||
package com.qiwenshare.file.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.github.tobato.fastdfs.service.FastFileStorageClient;
|
||||
import com.qiwenshare.file.api.IFileService;
|
||||
import com.qiwenshare.common.config.QiwenFileConfig;
|
||||
import com.qiwenshare.file.domain.FileBean;
|
||||
import com.qiwenshare.file.mapper.FileMapper;
|
||||
import com.qiwenshare.file.mapper.UserFileMapper;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
@ -19,32 +15,7 @@ public class FileService extends ServiceImpl<FileMapper, FileBean> implements IF
|
||||
|
||||
@Resource
|
||||
FileMapper fileMapper;
|
||||
@Resource
|
||||
UserFileMapper userFileMapper;
|
||||
@Resource
|
||||
FiletransferService filetransferService;
|
||||
@Resource
|
||||
QiwenFileConfig qiwenFileConfig;
|
||||
@Autowired
|
||||
private FastFileStorageClient fastFileStorageClient;
|
||||
|
||||
// @Override
|
||||
// public void batchInsertFile(List<FileBean> fileBeanList, Long userId) {
|
||||
// StorageBean storageBean = filetransferService.selectStorageBean(new StorageBean(userId));
|
||||
// long fileSizeSum = 0;
|
||||
// for (FileBean fileBean : fileBeanList) {
|
||||
// if (fileBean.getIsDir() == 0) {
|
||||
// fileSizeSum += fileBean.getFileSize();
|
||||
// }
|
||||
// }
|
||||
// fileMapper.batchInsertFile(fileBeanList);
|
||||
// if (storageBean != null) {
|
||||
// long updateFileSize = storageBean.getStorageSize() + fileSizeSum;
|
||||
//
|
||||
// storageBean.setStorageSize(updateFileSize);
|
||||
// filetransferService.updateStorageBean(storageBean);
|
||||
// }
|
||||
// }
|
||||
|
||||
@Override
|
||||
public void increaseFilePointCount(Long fileId) {
|
||||
@ -60,52 +31,5 @@ public class FileService extends ServiceImpl<FileMapper, FileBean> implements IF
|
||||
fileMapper.updateById(fileBean);
|
||||
}
|
||||
|
||||
// @Override
|
||||
// public void updateFile(FileBean fileBean) {
|
||||
// fileBean.setUploadTime(DateUtil.getCurrentTime());
|
||||
// fileMapper.updateFile(fileBean);
|
||||
// }
|
||||
|
||||
|
||||
|
||||
|
||||
// @Override
|
||||
// public List<FileBean> selectFileListByPath(FileBean fileBean) {
|
||||
// LambdaQueryWrapper<FileBean> lambdaQueryWrapper = new LambdaQueryWrapper<>();
|
||||
// lambdaQueryWrapper.eq(FileBean::getFilePath, fileBean.getFilePath())
|
||||
// .eq(FileBean::getUserId, fileBean.getUserId())
|
||||
// .orderByDesc(FileBean::getIsDir);
|
||||
// return fileMapper.selectList(lambdaQueryWrapper);
|
||||
// }
|
||||
// @Override
|
||||
// public void deleteLocalFile(FileBean fileBean) {
|
||||
// log.info("删除本地文件:" + JSON.toJSONString(fileBean));
|
||||
// //删除服务器文件
|
||||
// if (fileBean.getFileUrl() != null && fileBean.getFileUrl().indexOf("upload") != -1){
|
||||
// if (fileBean.getIsOSS() != null && fileBean.getIsOSS() == 1) {
|
||||
// AliyunOSSDelete.deleteObject(qiwenFileConfig.getAliyun().getOss(), fileBean.getFileUrl().substring(1));
|
||||
// } else if (fileBean.getStorageType() == 0) {
|
||||
// FileOperation.deleteFile(PathUtil.getStaticPath() + fileBean.getFileUrl());
|
||||
// if (FileUtil.isImageFile(FileUtil.getFileExtendName(fileBean.getFileUrl()))) {
|
||||
// FileOperation.deleteFile(PathUtil.getStaticPath() + fileBean.getFileUrl().replace(fileBean.getTimeStampName(), fileBean.getTimeStampName() + "_min"));
|
||||
// }
|
||||
// } else if (fileBean.getStorageType() == 1) {
|
||||
// AliyunOSSDelete.deleteObject(qiwenFileConfig.getAliyun().getOss(), fileBean.getFileUrl().substring(1));
|
||||
// } else if (fileBean.getStorageType() == 2){
|
||||
// fastFileStorageClient.deleteFile(fileBean.getFileUrl());
|
||||
//
|
||||
// } else {
|
||||
// FileOperation.deleteFile(PathUtil.getStaticPath() + fileBean.getFileUrl());
|
||||
// if (FileUtil.isImageFile(FileUtil.getFileExtendName(fileBean.getFileUrl()))) {
|
||||
// FileOperation.deleteFile(PathUtil.getStaticPath() + fileBean.getFileUrl().replace(fileBean.getTimeStampName(), fileBean.getTimeStampName() + "_min"));
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
@ -2,6 +2,9 @@ server.port=8080
|
||||
#环境切换dev/prod
|
||||
spring.profiles.active=dev
|
||||
|
||||
qiwen-file.local-storage-path=D:/export
|
||||
|
||||
|
||||
eureka.client.register-with-eureka=false
|
||||
eureka.client.fetchRegistry=false
|
||||
#eureka.client.server.waitTimeInMsWhenSyncEmpty=0
|
||||
@ -35,7 +38,7 @@ spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.Ph
|
||||
|
||||
#静态资源指定
|
||||
spring.mvc.static-path-pattern=/**
|
||||
spring.web.resources.static-locations=classpath:/static
|
||||
spring.web.resources.static-locations=classpath:/static,file:${qiwen-file.local-storage-path}
|
||||
|
||||
#上传下载
|
||||
spring.servlet.multipart.max-file-size=2048MB
|
||||
|
Loading…
Reference in New Issue
Block a user