|
@@ -1,5 +1,6 @@
|
|
|
package com.backendsys.modules.upload.service.impl;
|
|
|
|
|
|
+import cn.hutool.core.codec.Base64;
|
|
|
import cn.hutool.core.convert.Convert;
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
import cn.hutool.core.io.file.FileNameUtil;
|
|
@@ -9,6 +10,7 @@ import com.backendsys.exception.CustException;
|
|
|
import com.backendsys.modules.common.config.security.utils.HttpRequestUtil;
|
|
|
import com.backendsys.modules.sdk.douyincloud.tos.service.DouyinTosService;
|
|
|
import com.backendsys.modules.sdk.tencentcloud.cos.service.TencentCosService;
|
|
|
+import com.backendsys.modules.system.entity.SysCommon;
|
|
|
import com.backendsys.modules.system.service.SysCommonService;
|
|
|
import com.backendsys.modules.upload.dao.SysFileDao;
|
|
|
import com.backendsys.modules.upload.entity.SysFile;
|
|
@@ -18,15 +20,17 @@ import com.backendsys.utils.response.PageEntity;
|
|
|
import com.backendsys.utils.response.PageInfoResult;
|
|
|
import com.backendsys.utils.v2.PageUtils;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
-import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.concurrent.CompletableFuture;
|
|
|
+import java.util.concurrent.atomic.AtomicReference;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
@Service
|
|
|
public class SysFileServiceImpl implements SysFileService {
|
|
@@ -50,8 +54,33 @@ public class SysFileServiceImpl implements SysFileService {
|
|
|
@Override
|
|
|
public PageEntity selectUploadFileList(SysFile sysFile) {
|
|
|
PageUtils.startPage(); // 分页
|
|
|
- List<SysFile> list = sysFileDao.selectUploadFileList(sysFile);
|
|
|
- return new PageInfoResult(list).toEntity();
|
|
|
+ List<SysFile> sysFileList = sysFileDao.selectUploadFileList(sysFile);
|
|
|
+
|
|
|
+ Integer width = 100;
|
|
|
+ Integer height = 100;
|
|
|
+ String backgroundColor = "#f8f8f8";
|
|
|
+
|
|
|
+ // 不同的云环境 (target),缩略图配置也不一样
|
|
|
+ // - -1:本地:
|
|
|
+ // - 1:腾讯云: https://cloud.tencent.com/document/product/436/113295
|
|
|
+ // - 2:阿里云:
|
|
|
+ // - 3:抖音云: https://www.volcengine.com/docs/6349/153626
|
|
|
+ sysFileList = sysFileList.stream().map(item -> {
|
|
|
+
|
|
|
+ // 腾讯云 (color值通过base64加密, #f8f8f8)
|
|
|
+ if (item.getTarget() == 1) {
|
|
|
+ System.out.println("base64 encode: " + Base64.encode(backgroundColor));
|
|
|
+ item.setUrl_thumb(item.getUrl() + "?imageMogr2/thumbnail/" + width + "x" + height + "/pad/1/color/" + Base64.encode(backgroundColor));
|
|
|
+ }
|
|
|
+ // 抖音云
|
|
|
+ if (item.getTarget() == 3) {
|
|
|
+ item.setUrl_thumb(item.getUrl() + "?x-tos-process=image/resize,w_" + width + ",h_" + height + ",m_pad,color_" + backgroundColor);
|
|
|
+ }
|
|
|
+
|
|
|
+ return item;
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+
|
|
|
+ return new PageInfoResult(sysFileList).toEntity();
|
|
|
}
|
|
|
|
|
|
// [方法] 上传事件
|
|
@@ -75,7 +104,32 @@ public class SysFileServiceImpl implements SysFileService {
|
|
|
sysFileEntity.setRequest_id(uploadResult.getRequest_id());
|
|
|
sysFileEntity.setUser_id(httpRequestUtil.getUserId());
|
|
|
sysFileEntity.setObject_key(uploadResult.getKey());
|
|
|
- sysFileEntity.setName(FileNameUtil.getName(uploadResult.getKey()));
|
|
|
+
|
|
|
+ // 文件名
|
|
|
+// sysFileEntity.setName(FileNameUtil.getName(uploadResult.getKey()));
|
|
|
+
|
|
|
+ String filename = multipartFile.getOriginalFilename();
|
|
|
+ String filename_prefix = StrUtil.subBefore(filename, '.', true);
|
|
|
+ String filename_suffix = StringUtils.getFilenameExtension(filename);
|
|
|
+ /*
|
|
|
+ filename = 200x134.png
|
|
|
+ filename_prefix = 200x134
|
|
|
+ filename_suffix = png
|
|
|
+ */
|
|
|
+
|
|
|
+ // 查询如果有同名文件,则在文件中加入数量命名
|
|
|
+ LambdaQueryWrapper<SysFile> wrapper = new LambdaQueryWrapper<>();
|
|
|
+ // 构造正则表达式 (匹配同名、同名(数量))
|
|
|
+ String regex = "^" + filename_prefix + "(\\(\\d+\\))?\\." + filename_suffix + "$";
|
|
|
+ wrapper.apply("name REGEXP {0}", regex);
|
|
|
+ Long count = sysFileDao.selectCount(wrapper);
|
|
|
+ if (count > 0) {
|
|
|
+ sysFileEntity.setName(filename_prefix + "(" + count + ")." + filename_suffix);
|
|
|
+ } else {
|
|
|
+ sysFileEntity.setName(filename_prefix + "." + filename_suffix);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
sysFileEntity.setContent_type(multipartFile.getContentType());
|
|
|
sysFileEntity.setUrl(uploadResult.getDomain() + "/" + uploadResult.getKey());
|
|
|
sysFileEntity.setSize(multipartFile.getSize());
|
|
@@ -101,16 +155,24 @@ public class SysFileServiceImpl implements SysFileService {
|
|
|
@Override
|
|
|
public SysFile uploadSmall(MultipartFile multipartFile, Long category_id) {
|
|
|
|
|
|
- // [系统配置] 单文件上传大小限制(MB)
|
|
|
- Integer UPLOAD_TARGET = Convert.toInt(sysCommonService.getCommonByTag("UPLOAD_TARGET"));
|
|
|
- System.out.println("[系统配置] 上传目标(-1:本地, 1:腾讯云, 2:阿里云, 3.抖音云): " + UPLOAD_TARGET);
|
|
|
+ List<SysCommon> sysCommonList = sysCommonService.getCommonByCategory("UPLOAD");
|
|
|
|
|
|
- // [系统配置] 单文件上传大小限制(MB)
|
|
|
- Long UPLOAD_MAX_SIZE_MB = Convert.toLong(sysCommonService.getCommonByTag("UPLOAD_MAX_SIZE_MB"));
|
|
|
+ AtomicReference<Integer> UPLOAD_TARGET = new AtomicReference<>();
|
|
|
+ AtomicReference<Long> UPLOAD_MAX_SIZE_MB = new AtomicReference<>();
|
|
|
+ AtomicReference<Boolean> UPLOAD_MD5_DUPLICATE = new AtomicReference<>();
|
|
|
+
|
|
|
+ sysCommonList.stream().forEach(sysCommon -> {
|
|
|
+ if (sysCommon.getTag().equals("UPLOAD_TARGET")) UPLOAD_TARGET.set(Convert.toInt(sysCommon.getValue()));
|
|
|
+ if (sysCommon.getTag().equals("UPLOAD_MAX_SIZE_MB")) UPLOAD_MAX_SIZE_MB.set(Convert.toLong(sysCommon.getValue()));
|
|
|
+ if (sysCommon.getTag().equals("UPLOAD_MD5_DUPLICATE")) UPLOAD_MD5_DUPLICATE.set(Convert.toBool(sysCommon.getValue()));
|
|
|
+ });
|
|
|
+
|
|
|
+ System.out.println("[系统配置] 上传目标(-1:本地, 1:腾讯云, 2:阿里云, 3.抖音云): " + UPLOAD_TARGET);
|
|
|
System.out.println("[系统配置] 单文件上传大小限制(MB): " + UPLOAD_MAX_SIZE_MB);
|
|
|
+ System.out.println("[系统配置] 是否启用文件MD5查重: " + UPLOAD_MD5_DUPLICATE);
|
|
|
|
|
|
// 判断文件是否超过大小
|
|
|
- Long MAX_SIZE = UPLOAD_MAX_SIZE_MB * 1024 * 1024;
|
|
|
+ Long MAX_SIZE = UPLOAD_MAX_SIZE_MB.get() * 1024 * 1024;
|
|
|
if (multipartFile.getSize() > MAX_SIZE) {
|
|
|
throw new CustException("上传文件不能大于 " + MAX_SIZE / 1024 / 1024 + " MB,请使用大文件上传功能");
|
|
|
}
|
|
@@ -118,9 +180,7 @@ public class SysFileServiceImpl implements SysFileService {
|
|
|
try {
|
|
|
|
|
|
// [系统配置] 是否启用文件MD5查重
|
|
|
- Boolean UPLOAD_MD5_DUPLICATE = Convert.toBool(sysCommonService.getCommonByTag("UPLOAD_MD5_DUPLICATE"));
|
|
|
- System.out.println("[系统配置] 是否启用文件MD5查重: " + UPLOAD_MD5_DUPLICATE);
|
|
|
- if (UPLOAD_MD5_DUPLICATE) {
|
|
|
+ if (UPLOAD_MD5_DUPLICATE.get()) {
|
|
|
// - 是
|
|
|
// 根据文件MD5 判断是否上传过文件
|
|
|
// - 如果上传过,则仅更新记录,不再上传文件,仅更新文件分类
|
|
@@ -134,7 +194,7 @@ public class SysFileServiceImpl implements SysFileService {
|
|
|
SysFile sysFileEntity = (sysFileEntityList != null && sysFileEntityList.size() > 0) ? sysFileEntityList.get(0) : null;
|
|
|
if (sysFileEntity == null) {
|
|
|
// [方法] 上传事件
|
|
|
- sysFileEntity = uploadEvent(multipartFile, category_id, UPLOAD_TARGET);
|
|
|
+ sysFileEntity = uploadEvent(multipartFile, category_id, UPLOAD_TARGET.get());
|
|
|
} else {
|
|
|
// [更新] 上传文件记录 (更换文件分类)
|
|
|
sysFileEntity.setCategory_id(category_id);
|
|
@@ -146,7 +206,7 @@ public class SysFileServiceImpl implements SysFileService {
|
|
|
} else {
|
|
|
// - 否
|
|
|
// [方法] 上传事件
|
|
|
- SysFile sysFileEntity = uploadEvent(multipartFile, category_id, UPLOAD_TARGET);
|
|
|
+ SysFile sysFileEntity = uploadEvent(multipartFile, category_id, UPLOAD_TARGET.get());
|
|
|
return sysFileEntity;
|
|
|
}
|
|
|
|