|
@@ -0,0 +1,88 @@
|
|
|
|
|
+package com.sckw.file.service;
|
|
|
|
|
+
|
|
|
|
|
+import cn.hutool.core.io.file.FileNameUtil;
|
|
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
|
|
+import com.sckw.core.common.enums.StringConstant;
|
|
|
|
|
+import com.sckw.core.config.ProjectConfig;
|
|
|
|
|
+import com.sckw.core.exception.BusinessException;
|
|
|
|
|
+import com.sckw.core.model.constant.NumberConstant;
|
|
|
|
|
+import com.sckw.core.model.vo.FileInfoVO;
|
|
|
|
|
+import com.sckw.core.utils.FileUploadUtils;
|
|
|
|
|
+import com.sckw.core.utils.FileUtils;
|
|
|
|
|
+import com.sckw.core.utils.IdWorker;
|
|
|
|
|
+import com.sckw.core.utils.StringUtils;
|
|
|
|
|
+import com.sckw.core.web.context.LoginUserHolder;
|
|
|
|
|
+import com.sckw.file.dao.KwsFileInfoDao;
|
|
|
|
|
+import com.sckw.file.model.kwfFileInfo;
|
|
|
|
|
+import jakarta.annotation.Resource;
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.apache.commons.codec.digest.DigestUtils;
|
|
|
|
|
+import org.apache.commons.io.FilenameUtils;
|
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
+
|
|
|
|
|
+import java.io.IOException;
|
|
|
|
|
+import java.math.BigDecimal;
|
|
|
|
|
+import java.util.Date;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * @author xucaiqin
|
|
|
|
|
+ * @date 2026-05-07 13:39:43
|
|
|
|
|
+ */
|
|
|
|
|
+@Service
|
|
|
|
|
+@Slf4j
|
|
|
|
|
+public class FileInfoService {
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private KwsFileInfoDao kwsFileInfoDao;
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private ProjectConfig projectConfig;
|
|
|
|
|
+
|
|
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
|
|
+ public Object uploadFile(MultipartFile file) {
|
|
|
|
|
+ boolean isEmpty = file.isEmpty();
|
|
|
|
|
+ if (isEmpty) {
|
|
|
|
|
+ throw new BusinessException("文件不能为空");
|
|
|
|
|
+ }
|
|
|
|
|
+ String filePath = projectConfig.getProfile();
|
|
|
|
|
+
|
|
|
|
|
+ String fileName, md5;
|
|
|
|
|
+ try {
|
|
|
|
|
+ fileName = FileUploadUtils.upload(filePath, file);
|
|
|
|
|
+ md5 = DigestUtils.md5Hex(file.getInputStream());
|
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
|
+ log.error("文件上传异常 {}", e.getMessage());
|
|
|
|
|
+ throw new BusinessException(e.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ String oFileName = file.getOriginalFilename();
|
|
|
|
|
+ BigDecimal fileSize = FileUtils.getFileSize(file, StringConstant.KB);
|
|
|
|
|
+ kwfFileInfo fileInfo = new kwfFileInfo();
|
|
|
|
|
+ fileInfo.setId(new IdWorker(1).nextId());
|
|
|
|
|
+ fileInfo.setOriginalName(oFileName);
|
|
|
|
|
+ fileInfo.setFileSuffix(FilenameUtils.getExtension(oFileName));
|
|
|
|
|
+ fileInfo.setFileSize(StringUtils.isBlank(fileSize) ? new BigDecimal(NumberConstant.ZERO) : fileSize);
|
|
|
|
|
+ fileInfo.setType("LOCAL");
|
|
|
|
|
+ fileInfo.setFileName(FileNameUtil.getName(fileName));
|
|
|
|
|
+ fileInfo.setMd5(md5);
|
|
|
|
|
+ fileInfo.setFilePath(fileName);
|
|
|
|
|
+ fileInfo.setCreateTime(new Date());
|
|
|
|
|
+ fileInfo.setUpdateTime(new Date());
|
|
|
|
|
+ fileInfo.setStatus(0);
|
|
|
|
|
+ fileInfo.setDelFlag(0);
|
|
|
|
|
+ fileInfo.setCreateBy(LoginUserHolder.getUserId());
|
|
|
|
|
+ fileInfo.setUpdateBy(LoginUserHolder.getUserId());
|
|
|
|
|
+ kwsFileInfoDao.insert(fileInfo);
|
|
|
|
|
+
|
|
|
|
|
+ FileInfoVO fileInfoVO = new FileInfoVO();
|
|
|
|
|
+ fileInfoVO.setType(fileInfo.getType());
|
|
|
|
|
+ fileInfoVO.setFileOriginalName(fileInfo.getOriginalName());
|
|
|
|
|
+ fileInfoVO.setFileMd5(fileInfo.getMd5());
|
|
|
|
|
+ fileInfoVO.setFileName(fileInfo.getFileName());
|
|
|
|
|
+ fileInfoVO.setFileSuffix(fileInfo.getFileSuffix());
|
|
|
|
|
+ fileInfoVO.setFileSize(fileInfo.getFileSize());
|
|
|
|
|
+ fileInfoVO.setFilePath(fileName);
|
|
|
|
|
+ fileInfoVO.setFileAbsolutePath(StrUtil.removeSuffix(projectConfig.getDomain(), "/") + "/" + StrUtil.removePrefix(fileName, "/"));
|
|
|
|
|
+ return fileInfoVO;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|