|
@@ -5,6 +5,7 @@ import cn.hutool.core.io.FileTypeUtil;
|
|
|
import cn.hutool.core.io.FileUtil;
|
|
|
import cn.hutool.core.io.file.FileNameUtil;
|
|
|
import cn.hutool.core.lang.Assert;
|
|
|
+import cn.hutool.core.util.IdUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
import cn.hutool.crypto.digest.DigestUtil;
|
|
|
import com.alibaba.fastjson.JSON;
|
|
@@ -22,6 +23,7 @@ import com.google.common.annotations.VisibleForTesting;
|
|
|
import lombok.SneakyThrows;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import me.chanjar.weixin.common.util.fs.FileUtils;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
@@ -76,7 +78,7 @@ public class FileServiceImpl implements FileService {
|
|
|
|
|
|
@Override
|
|
|
@SneakyThrows
|
|
|
- public String createFile(byte[] content, String name, String directory, String type) {
|
|
|
+ public String createFile(byte[] content, String name, String directory, String type, String restricted) {
|
|
|
// 1.1 处理 type 为空的情况
|
|
|
if (StrUtil.isEmpty(type)) {
|
|
|
type = FileTypeUtils.getMineType(content, name);
|
|
@@ -94,10 +96,15 @@ public class FileServiceImpl implements FileService {
|
|
|
}
|
|
|
|
|
|
// 2.1 生成上传的 path,需要保证唯一
|
|
|
- String path = generateUploadPath(name, directory);
|
|
|
+ String path = generateUploadPath(content, name);
|
|
|
String filePath = fileUploadDir + path;
|
|
|
FileUtil.writeBytes(content, filePath);
|
|
|
- String url = StrUtil.format("/files/{}", path);
|
|
|
+ String url = "";
|
|
|
+ if (StringUtils.isBlank( restricted)) {
|
|
|
+ url = StrUtil.format("/files/{}", path);
|
|
|
+ }else {
|
|
|
+ url = StrUtil.format("/files/no/{}", path);
|
|
|
+ }
|
|
|
log.info("📂 文件上传信息如下:");
|
|
|
log.info("Name: {}", name);
|
|
|
log.info("Path: {}", path);
|
|
@@ -128,7 +135,7 @@ public class FileServiceImpl implements FileService {
|
|
|
}
|
|
|
@Override
|
|
|
@SneakyThrows
|
|
|
- public Map<String, Object> getFile(Long businessId, String jobTypeCode, String name, String path, byte[] content) {
|
|
|
+ public Map<String, Object> getFile(Long businessId, String jobTypeCode, String name, String path, byte[] content,String restricted) {
|
|
|
// 计算默认的 path 名
|
|
|
String type = FileTypeUtils.getMineType(content, name);
|
|
|
if (StrUtil.isEmpty(path)) {
|
|
@@ -139,23 +146,28 @@ public class FileServiceImpl implements FileService {
|
|
|
name = path;
|
|
|
}
|
|
|
|
|
|
- // 上传到文件存储器
|
|
|
- FileClient client = fileConfigService.getMasterFileClient();
|
|
|
- Assert.notNull(client, "客户端(master) 不能为空");
|
|
|
- String url = client.upload(content, path, type);
|
|
|
-
|
|
|
- // 保存到数据库
|
|
|
- FileDO file = new FileDO();
|
|
|
- file.setConfigId(client.getId());
|
|
|
- file.setName(name);
|
|
|
- file.setPath(path);
|
|
|
- file.setUrl(url);
|
|
|
- file.setType(type);
|
|
|
-// file.setBusinessId(businessId);
|
|
|
-// file.setJobTypeCode(jobTypeCode);
|
|
|
- file.setSize(content.length);
|
|
|
- fileMapper.insert(file);
|
|
|
- Map<String, Object> map = JSON.parseObject(JSON.toJSONString(file), Map.class);
|
|
|
+ path = generateUploadPath(content, name);
|
|
|
+
|
|
|
+ String filePath = fileUploadDir + path;
|
|
|
+ FileUtil.writeBytes(content, filePath);
|
|
|
+ String url = "";
|
|
|
+ if (StringUtils.isBlank( restricted)) {
|
|
|
+ url = StrUtil.format("/files/{}", path);
|
|
|
+ }else {
|
|
|
+ url = StrUtil.format("/restricted/files/{}", path);
|
|
|
+ }
|
|
|
+ log.info("📂 文件上传信息如下:");
|
|
|
+ log.info("Name: {}", name);
|
|
|
+ log.info("Path: {}", path);
|
|
|
+ log.info("Url: {}", url);
|
|
|
+ log.info("Type: {}", convertMimeToExtension(type));
|
|
|
+ log.info("Size: {}", formatFileSize(content.length));
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ map.put("url", url);
|
|
|
+ map.put("type", convertMimeToExtension(type));
|
|
|
+ map.put("size", formatFileSize(content.length));
|
|
|
+ map.put("name", name);
|
|
|
+ map.put("path", path);
|
|
|
return map;
|
|
|
}
|
|
|
|
|
@@ -250,6 +262,24 @@ public class FileServiceImpl implements FileService {
|
|
|
return name;
|
|
|
}
|
|
|
|
|
|
+ public String generateUploadPath(byte[] content, String originalName) {
|
|
|
+ // 1. 生成文件内容的 sha256 作为唯一标识
|
|
|
+ String fileHash = DigestUtil.sha256Hex(content);
|
|
|
+
|
|
|
+ // 2. 生成短随机字符串,防止重复
|
|
|
+ String randomSuffix = IdUtil.fastSimpleUUID().substring(0, 8);
|
|
|
+
|
|
|
+ // 3. 获取文件扩展名
|
|
|
+ String ext = FileUtil.extName(originalName);
|
|
|
+ String extension = StrUtil.isNotEmpty(ext) ? "." + ext : "";
|
|
|
+
|
|
|
+ // 4. 拼接最终文件名:{hash}-{random}.{ext}
|
|
|
+ String fileName = fileHash + "-" + randomSuffix + extension;
|
|
|
+
|
|
|
+ return fileName; // 直接作为相对路径存储,无目录结构
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
@Override
|
|
|
@SneakyThrows
|
|
|
public FilePresignedUrlRespVO getFilePresignedUrl(String name, String directory) {
|