|
@@ -8,19 +8,20 @@ package org.eco.vip.nexus.core.service.files;
|
|
|
|
|
|
import cn.hutool.core.io.FileUtil;
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
-import cn.hutool.core.util.StrUtil;
|
|
|
import com.mybatisflex.core.paginate.Page;
|
|
|
import com.mybatisflex.core.query.QueryWrapper;
|
|
|
import jakarta.annotation.Resource;
|
|
|
import jakarta.servlet.http.HttpServletResponse;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.eco.vip.core.constant.Constants;
|
|
|
+import org.eco.vip.core.exception.BusinessException;
|
|
|
import org.eco.vip.core.pojo.PageResult;
|
|
|
import org.eco.vip.core.utils.FileDownloadUtils;
|
|
|
import org.eco.vip.core.utils.FileLocalUtils;
|
|
|
import org.eco.vip.core.utils.FileUtils;
|
|
|
import org.eco.vip.core.utils.ImgUtils;
|
|
|
-import org.eco.vip.core.utils.IoUtils;
|
|
|
import org.eco.vip.core.utils.MapstructUtils;
|
|
|
+import org.eco.vip.core.utils.Seqs;
|
|
|
import org.eco.vip.core.utils.StrUtils;
|
|
|
import org.eco.vip.nexus.core.domain.files.Files;
|
|
|
import org.eco.vip.nexus.core.domain.files.pojo.FilesBO;
|
|
@@ -28,14 +29,15 @@ import org.eco.vip.nexus.core.domain.files.pojo.FilesVO;
|
|
|
import org.eco.vip.nexus.core.mapper.FilesMapper;
|
|
|
import org.eco.vip.orm.domain.PageQuery;
|
|
|
import org.eco.vip.orm.service.BaseService;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import java.io.File;
|
|
|
import java.io.IOException;
|
|
|
import java.util.List;
|
|
|
-import java.util.UUID;
|
|
|
|
|
|
+import static org.eco.vip.core.utils.IpUtils.getIpAddress;
|
|
|
import static org.eco.vip.nexus.core.domain.files.table.FilesTableDef.FILES;
|
|
|
|
|
|
/**
|
|
@@ -48,6 +50,8 @@ import static org.eco.vip.nexus.core.domain.files.table.FilesTableDef.FILES;
|
|
|
@Slf4j
|
|
|
public class FilesService extends BaseService<FilesMapper, Files> implements IFilesService {
|
|
|
|
|
|
+ @Value("${server.port:9040}")
|
|
|
+ private Long port;
|
|
|
@Resource
|
|
|
private FilesMapper filesMapper;
|
|
|
|
|
@@ -93,7 +97,21 @@ public class FilesService extends BaseService<FilesMapper, Files> implements IFi
|
|
|
|
|
|
@Override
|
|
|
public boolean deleteFile(List<String> ids) {
|
|
|
- return false;
|
|
|
+
|
|
|
+ List<Files> files = filesMapper.selectListByIds(ids);
|
|
|
+ if (ObjectUtil.isEmpty(files)) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ files.forEach(filesInfo -> {
|
|
|
+ try {
|
|
|
+ FileLocalUtils.deleteFile(filesInfo.getStorageUrl());
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("文件删除失败:{},路径:{}", filesInfo.getOriginalName(), filesInfo.getStorageUrl(), e);
|
|
|
+ throw new BusinessException("文件删除失败");
|
|
|
+ }
|
|
|
+ });
|
|
|
+ return this.delete(ids);
|
|
|
+
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -115,7 +133,7 @@ public class FilesService extends BaseService<FilesMapper, Files> implements IFi
|
|
|
|
|
|
@Override
|
|
|
public void download(String id, HttpServletResponse response) throws IOException {
|
|
|
-
|
|
|
+ uniDownload(id, response);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -131,42 +149,45 @@ public class FilesService extends BaseService<FilesMapper, Files> implements IFi
|
|
|
* @return 文件id/url
|
|
|
*/
|
|
|
private String storageFile(String engine, MultipartFile file) {
|
|
|
- String key = UUID.randomUUID().toString().replace("-", "");
|
|
|
- String storageUrl = FileLocalUtils.storageFileWithReturnUrl("LocalBucket", key, file);
|
|
|
+ String key = Seqs.getId(Seqs.UPLOAD_SEQ_TYPE);
|
|
|
+ String path = FileUtils.genFilePath(key, file.getOriginalFilename());
|
|
|
+ String storageUrl = FileLocalUtils.storageFileWithReturnUrl(Constants.LOCAL_FILE_BUCKET_KEY, path, file);
|
|
|
Files filesInfo = new Files();
|
|
|
- filesInfo.setFileId(key);
|
|
|
filesInfo.setEngine(engine);
|
|
|
- filesInfo.setBucket("LocalBucket");
|
|
|
+ filesInfo.setBucket(Constants.LOCAL_FILE_BUCKET_KEY);
|
|
|
filesInfo.setOriginalName(file.getOriginalFilename());
|
|
|
- filesInfo.setDownloadUrl(storageUrl);
|
|
|
- String suffix = ObjectUtil.isNotEmpty(file.getOriginalFilename()) ? StrUtil.subAfter(file.getOriginalFilename(),
|
|
|
- StrUtil.DOT, true) : null;
|
|
|
+ String suffix = FileUtils.getSuffix(file.getOriginalFilename());
|
|
|
filesInfo.setFileSuffix(suffix);
|
|
|
- filesInfo.setSizeKb(12);
|
|
|
+ filesInfo.setSizeKb(file.getSize() / 1024);
|
|
|
filesInfo.setSizeInfo(FileUtils.readableFileSize(file.getSize()));
|
|
|
- filesInfo.setFileName(ObjectUtil.isNotEmpty(filesInfo.getFileSuffix()) ? key + StrUtil.DOT + filesInfo.getFileSuffix() : null);
|
|
|
+ filesInfo.setFileName(StrUtils.format("{}_{}", key, file.getOriginalFilename()));
|
|
|
if (ObjectUtil.isNotEmpty(suffix)) {
|
|
|
if (isPic(suffix)) {
|
|
|
try {
|
|
|
filesInfo.setThumbnail(ImgUtils.toBase64DataUri(ImgUtils.scale(ImgUtils.toImage(file.getBytes()),
|
|
|
100, 100, null), suffix));
|
|
|
- } catch (Exception ignored) {
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("图片压缩失败:{}", filesInfo.getOriginalName(), e);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+ String downloadUrl = Constants.HTTP + getIpAddress() + ":" + port + FileUtils.FILE_SEPARATOR + Constants.LOCAL_FILE_BUCKET_KEY + FileUtils.FILE_SEPARATOR + path;
|
|
|
+ filesInfo.setDownloadUrl(downloadUrl);
|
|
|
filesInfo.setStorageUrl(storageUrl);
|
|
|
-
|
|
|
this.save(filesInfo);
|
|
|
- return "";
|
|
|
+ return filesInfo.getFileId();
|
|
|
}
|
|
|
|
|
|
- private void unifiedDownload(FilesBO filesBO, HttpServletResponse response, boolean isDownloadAuth) {
|
|
|
- FilesVO filesVO = this.selectById(filesBO.getFileId());
|
|
|
- File file = FileUtils.file(filesVO.getStorageUrl());
|
|
|
+ private void uniDownload(String id, HttpServletResponse response) {
|
|
|
+ FilesVO filesVO = this.selectById(id);
|
|
|
+ if (ObjectUtil.isEmpty(filesVO)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ File file = FileLocalUtils.getFileByBucketNameAndPath(filesVO.getStorageUrl());
|
|
|
if (!FileUtil.exist(file)) {
|
|
|
return;
|
|
|
}
|
|
|
- FileDownloadUtils.download(file.getName(), IoUtils.readBytes(FileUtil.getInputStream(file)), response);
|
|
|
+ FileDownloadUtils.download(file, response);
|
|
|
}
|
|
|
|
|
|
/**
|