|
@@ -1,20 +1,257 @@
|
|
|
package com.zksy.base.service.impl;
|
|
package com.zksy.base.service.impl;
|
|
|
|
|
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import com.zksy.base.domain.FileManage;
|
|
import com.zksy.base.domain.FileManage;
|
|
|
-import com.zksy.base.service.FileManageService;
|
|
|
|
|
import com.zksy.base.mapper.FileManageMapper;
|
|
import com.zksy.base.mapper.FileManageMapper;
|
|
|
|
|
+import com.zksy.base.service.FileManageService;
|
|
|
|
|
+import com.zksy.common.core.domain.AjaxResult;
|
|
|
|
|
+import com.zksy.common.exception.ServiceException;
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.apache.commons.lang3.exception.ExceptionUtils;
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
+
|
|
|
|
|
+import java.io.File;
|
|
|
|
|
+import java.io.IOException;
|
|
|
|
|
+import java.time.LocalDateTime;
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+import java.util.Objects;
|
|
|
|
|
+import java.util.UUID;
|
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* @author Administrator
|
|
* @author Administrator
|
|
|
* @description 针对表【file_manage(文件管理)】的数据库操作Service实现
|
|
* @description 针对表【file_manage(文件管理)】的数据库操作Service实现
|
|
|
* @createDate 2025-09-08 08:57:08
|
|
* @createDate 2025-09-08 08:57:08
|
|
|
*/
|
|
*/
|
|
|
|
|
+@Slf4j
|
|
|
@Service
|
|
@Service
|
|
|
public class FileManageServiceImpl extends ServiceImpl<FileManageMapper, FileManage>
|
|
public class FileManageServiceImpl extends ServiceImpl<FileManageMapper, FileManage>
|
|
|
implements FileManageService{
|
|
implements FileManageService{
|
|
|
|
|
|
|
|
|
|
+ @Value("${local.fileserver.filetypes}")
|
|
|
|
|
+ private String allowFileTypes;
|
|
|
|
|
+
|
|
|
|
|
+ @Value("${local.fileserver.upload.path}")
|
|
|
|
|
+ private String localFileServerUploadDir;
|
|
|
|
|
+
|
|
|
|
|
+ @Value("${local.fileserver.baseurl}")
|
|
|
|
|
+ private String localFileServerBaseUrl;
|
|
|
|
|
+
|
|
|
|
|
+ @Value("${local.fileserver.mapping.path}")
|
|
|
|
|
+ private String localFileServerMappingPath;
|
|
|
|
|
+
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private FileManageMapper mapper;
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public AjaxResult uploadFile(MultipartFile multipartFile, String fid, String moduleName) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ // 获取文件名
|
|
|
|
|
+ String fileOriginalName = multipartFile.getOriginalFilename();
|
|
|
|
|
+ //获取文件大小
|
|
|
|
|
+ Long fileSize = multipartFile.getSize();
|
|
|
|
|
+ String fileName = UUID.randomUUID().toString() + fileOriginalName.substring(fileOriginalName.lastIndexOf("."), fileOriginalName.length());
|
|
|
|
|
+
|
|
|
|
|
+ log.info("(加个时间戳,尽量避免文件名称重复)保存的文件名为: " + fileName + "\n");
|
|
|
|
|
+ //加个时间戳,尽量避免文件名称重复
|
|
|
|
|
+ String path = localFileServerUploadDir + fileName;
|
|
|
|
|
+ //文件绝对路径
|
|
|
|
|
+ log.info("保存文件绝对路径" + path + "\n");
|
|
|
|
|
+ //创建文件路径
|
|
|
|
|
+ File dest = new File(path);
|
|
|
|
|
+ //判断文件是否已经存在
|
|
|
|
|
+ if (dest.exists()) {
|
|
|
|
|
+ return AjaxResult.error("文件已存在");
|
|
|
|
|
+ }
|
|
|
|
|
+ //判断文件父目录是否存在
|
|
|
|
|
+ if (!dest.getParentFile().exists()) {
|
|
|
|
|
+ dest.getParentFile().mkdir();
|
|
|
|
|
+ }
|
|
|
|
|
+ //上传文件
|
|
|
|
|
+ multipartFile.transferTo(dest); //保存文件
|
|
|
|
|
+ log.info("保存文件路径" + path + "\n");
|
|
|
|
|
+ //url="http://你自己的域名/项目名/images/"+fileName;//正式项目
|
|
|
|
|
+// String url = localFileServerBaseUrl + serverContextPath + localFileServerMappingPath +"/" + fileName;
|
|
|
|
|
+ String url = localFileServerMappingPath + "/" + fileName;
|
|
|
|
|
+
|
|
|
|
|
+ //log.info("保存的完整url====" + url + "\n");
|
|
|
|
|
+ FileManage uploadRes = new FileManage();
|
|
|
|
|
+ uploadRes.setId(UUID.randomUUID().toString());
|
|
|
|
|
+ uploadRes.setFid(fid);
|
|
|
|
|
+ uploadRes.setFileOriginalName(fileOriginalName);
|
|
|
|
|
+ uploadRes.setFileUrl(url);
|
|
|
|
|
+ uploadRes.setFileName(fileName);
|
|
|
|
|
+ uploadRes.setFileSize(fileSize);
|
|
|
|
|
+ uploadRes.setCreateTime(LocalDateTime.now());
|
|
|
|
|
+ uploadRes.setCreateBy("管理员");
|
|
|
|
|
+ uploadRes.setModuleName(moduleName);
|
|
|
|
|
+ mapper.insert(uploadRes);
|
|
|
|
|
+ return AjaxResult.success("文件上传成功",uploadRes);
|
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
|
+ log.error("文件上传异常!");
|
|
|
|
|
+ log.error(ExceptionUtils.getStackTrace(e));
|
|
|
|
|
+ return AjaxResult.error("文件上传失败");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public String uploadFilePath(MultipartFile multipartFile) {
|
|
|
|
|
+ String url = "";
|
|
|
|
|
+ try {
|
|
|
|
|
+ // 获取文件名
|
|
|
|
|
+ String fileOriginalName = multipartFile.getOriginalFilename();
|
|
|
|
|
+ //获取文件大小
|
|
|
|
|
+ Long fileSize = multipartFile.getSize();
|
|
|
|
|
+ String fileName = UUID.randomUUID().toString() + fileOriginalName.substring(fileOriginalName.lastIndexOf("."), fileOriginalName.length());
|
|
|
|
|
+
|
|
|
|
|
+ log.info("(加个时间戳,尽量避免文件名称重复)保存的文件名为: " + fileName + "\n");
|
|
|
|
|
+ //加个时间戳,尽量避免文件名称重复
|
|
|
|
|
+ String path = localFileServerUploadDir + fileName;
|
|
|
|
|
+ //文件绝对路径
|
|
|
|
|
+ log.info("保存文件绝对路径" + path + "\n");
|
|
|
|
|
+ //创建文件路径
|
|
|
|
|
+ File dest = new File(path);
|
|
|
|
|
+ //判断文件是否已经存在
|
|
|
|
|
+ if (dest.exists()) {
|
|
|
|
|
+ throw new ServiceException("文件已存在");
|
|
|
|
|
+ }
|
|
|
|
|
+ //判断文件父目录是否存在
|
|
|
|
|
+ if (!dest.getParentFile().exists()) {
|
|
|
|
|
+ dest.getParentFile().mkdir();
|
|
|
|
|
+ }
|
|
|
|
|
+ //上传文件
|
|
|
|
|
+ multipartFile.transferTo(dest); //保存文件
|
|
|
|
|
+ log.info("保存文件路径" + path + "\n");
|
|
|
|
|
+ //url="http://你自己的域名/项目名/images/"+fileName;//正式项目
|
|
|
|
|
+// String url = localFileServerBaseUrl + serverContextPath + localFileServerMappingPath +"/" + fileName;
|
|
|
|
|
+ url = localFileServerMappingPath + "/" + fileName;
|
|
|
|
|
+
|
|
|
|
|
+ }catch (IOException e){
|
|
|
|
|
+ log.error("文件上传异常!");
|
|
|
|
|
+ log.error(ExceptionUtils.getStackTrace(e));
|
|
|
|
|
+ }
|
|
|
|
|
+ return url;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public AjaxResult uploadListFile(List<MultipartFile> multipartFiles, String fid, String moduleName) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ List<FileManage> FileManageList = new ArrayList<>();
|
|
|
|
|
+ if (multipartFiles.size() > 0) {
|
|
|
|
|
+ for (MultipartFile multipartFile : multipartFiles) {
|
|
|
|
|
+ // 获取文件名
|
|
|
|
|
+ String fileOriginalName = multipartFile.getOriginalFilename();
|
|
|
|
|
+ //获取文件大小
|
|
|
|
|
+ Long fileSize = multipartFile.getSize();
|
|
|
|
|
+ String fileName = UUID.randomUUID().toString() + fileOriginalName.substring(fileOriginalName.lastIndexOf("."), fileOriginalName.length());
|
|
|
|
|
+
|
|
|
|
|
+ log.info("(加个时间戳,尽量避免文件名称重复)保存的文件名为: " + fileName + "\n");
|
|
|
|
|
+ //加个时间戳,尽量避免文件名称重复
|
|
|
|
|
+ String path = localFileServerUploadDir + fileName;
|
|
|
|
|
+ //文件绝对路径
|
|
|
|
|
+ log.info("保存文件绝对路径" + path + "\n");
|
|
|
|
|
+ //创建文件路径
|
|
|
|
|
+ File dest = new File(path);
|
|
|
|
|
+ //判断文件是否已经存在
|
|
|
|
|
+ if (dest.exists()) {
|
|
|
|
|
+ return AjaxResult.error("文件已存在");
|
|
|
|
|
+ }
|
|
|
|
|
+ //判断文件父目录是否存在
|
|
|
|
|
+ if (!dest.getParentFile().exists()) {
|
|
|
|
|
+ dest.getParentFile().mkdir();
|
|
|
|
|
+ }
|
|
|
|
|
+ //上传文件
|
|
|
|
|
+ multipartFile.transferTo(dest); //保存文件
|
|
|
|
|
+ log.info("保存文件路径" + path + "\n");
|
|
|
|
|
+ //url="http://你自己的域名/项目名/images/"+fileName;//正式项目
|
|
|
|
|
+// String url = localFileServerBaseUrl + serverContextPath + localFileServerMappingPath +"/" + fileName;
|
|
|
|
|
+ String url = localFileServerMappingPath + "/" + fileName;
|
|
|
|
|
+
|
|
|
|
|
+ //log.info("保存的完整url====" + url + "\n");
|
|
|
|
|
+ FileManage uploadRes = new FileManage();
|
|
|
|
|
+ uploadRes.setId(UUID.randomUUID().toString());
|
|
|
|
|
+ uploadRes.setFid(fid);
|
|
|
|
|
+ uploadRes.setFileOriginalName(fileOriginalName);
|
|
|
|
|
+ uploadRes.setFileUrl(url);
|
|
|
|
|
+ uploadRes.setFileName(fileName);
|
|
|
|
|
+ uploadRes.setFileSize(fileSize);
|
|
|
|
|
+ uploadRes.setCreateTime(LocalDateTime.now());
|
|
|
|
|
+ uploadRes.setCreateBy("管理员");
|
|
|
|
|
+ uploadRes.setModuleName(moduleName);
|
|
|
|
|
+ mapper.insert(uploadRes);
|
|
|
|
|
+ FileManageList.add(uploadRes);
|
|
|
|
|
+ }
|
|
|
|
|
+ return AjaxResult.success("文件上传成功",FileManageList);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ return AjaxResult.error("无文件上传");
|
|
|
|
|
+ }
|
|
|
|
|
+ }catch (IOException e) {
|
|
|
|
|
+ log.error("文件上传异常!");
|
|
|
|
|
+ log.error(ExceptionUtils.getStackTrace(e));
|
|
|
|
|
+ return AjaxResult.error("文件上传异常");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public AjaxResult deleteFile(String id) {
|
|
|
|
|
+ FileManage fileUpload = mapper.selectById(id);
|
|
|
|
|
+ String filePath = localFileServerUploadDir+fileUpload.getFileName();
|
|
|
|
|
+ File file = new File(filePath);
|
|
|
|
|
+ if (file.exists()) {
|
|
|
|
|
+ boolean isDeleted = file.delete();
|
|
|
|
|
+ if (isDeleted) {
|
|
|
|
|
+ //删除数据库信息
|
|
|
|
|
+ mapper.deleteById(id);
|
|
|
|
|
+ return AjaxResult.success("文件已成功删除");
|
|
|
|
|
+ } else {
|
|
|
|
|
+ return AjaxResult.error("无法删除该文件");
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ return AjaxResult.error("指定的文件不存在");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public List<String> listFileByFid(String id) {
|
|
|
|
|
+ LambdaQueryWrapper<FileManage> wrapper = new LambdaQueryWrapper<>();
|
|
|
|
|
+ wrapper.eq(FileManage::getFid,id);
|
|
|
|
|
+ List<FileManage> list = this.list(wrapper);
|
|
|
|
|
+ List<String> collect = list.stream().map(FileManage::getFileUrl).collect(Collectors.toList());
|
|
|
|
|
+ return collect;
|
|
|
|
|
+ }
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 批量删除文件优化版
|
|
|
|
|
+ */
|
|
|
|
|
+ @Override
|
|
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
|
|
+ public AjaxResult deleteFileByFids(List<String> fids) {
|
|
|
|
|
+ if (Objects.isNull(fids) || fids.isEmpty()) {
|
|
|
|
|
+ return AjaxResult.error("删除失败:文件ID集合不能为空");
|
|
|
|
|
+ }
|
|
|
|
|
+ int deleteCount = 0;
|
|
|
|
|
+ try {
|
|
|
|
|
+ LambdaQueryWrapper<FileManage> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
|
|
+ queryWrapper.in(FileManage::getFid, fids);
|
|
|
|
|
+ List<FileManage> fileList = list(queryWrapper);
|
|
|
|
|
+
|
|
|
|
|
+ if (!fileList.isEmpty()) {
|
|
|
|
|
+ for (FileManage file : fileList) {
|
|
|
|
|
+ deleteFile(file.getId());
|
|
|
|
|
+ deleteCount++;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return AjaxResult.success("删除成功", deleteCount);
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ return AjaxResult.error("删除失败:" + e.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|