|
@@ -0,0 +1,186 @@
|
|
|
+/*
|
|
|
+ * Copyright (c) 2025 GaoKunW
|
|
|
+ *
|
|
|
+ */
|
|
|
+
|
|
|
+package org.eco.vip.nexus.core.service.file;
|
|
|
+
|
|
|
+
|
|
|
+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.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.StrUtils;
|
|
|
+import org.eco.vip.nexus.core.domain.file.File;
|
|
|
+import org.eco.vip.nexus.core.domain.file.pojo.FileBO;
|
|
|
+import org.eco.vip.nexus.core.domain.file.pojo.FileVO;
|
|
|
+import org.eco.vip.nexus.core.mapper.FileMapper;
|
|
|
+import org.eco.vip.orm.domain.PageQuery;
|
|
|
+import org.eco.vip.orm.service.BaseService;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.List;
|
|
|
+import java.util.UUID;
|
|
|
+
|
|
|
+import static org.eco.vip.nexus.core.domain.file.table.FileTableDef.FILE;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @description FileService
|
|
|
+ *
|
|
|
+ * @author GaoKunW
|
|
|
+ * @date 2025/7/22 11:10
|
|
|
+ */
|
|
|
+@Service
|
|
|
+@Slf4j
|
|
|
+public class FileService extends BaseService<FileMapper, File> implements IFileService {
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private FileMapper fileMapper;
|
|
|
+
|
|
|
+ private QueryWrapper buildQueryWrapper(FileBO fileBO) {
|
|
|
+ return super.buildBaseQueryWrapper()
|
|
|
+ .and(FILE.FILE_ID.eq(fileBO.getFileId()))
|
|
|
+ .and(FILE.ENGINE.eq(fileBO.getEngine()))
|
|
|
+ .and(FILE.BUCKET.eq(fileBO.getBucket()))
|
|
|
+ .and(FILE.FILE_NAME.eq(fileBO.getFileName()))
|
|
|
+ .and(FILE.ORIGINAL_NAME.eq(fileBO.getOriginalName()))
|
|
|
+ .and(FILE.STATUS.eq(fileBO.getStatus()));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public PageResult<FileVO> selectPage(FileBO fileBO) {
|
|
|
+ QueryWrapper queryWrapper = buildQueryWrapper(fileBO);
|
|
|
+ Page<FileVO> page = fileMapper.paginateWithRelationsAs(PageQuery.build(), queryWrapper, FileVO.class);
|
|
|
+ return PageResult.build(page);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<FileVO> selectList(FileBO fileBO) {
|
|
|
+ QueryWrapper queryWrapper = buildQueryWrapper(fileBO);
|
|
|
+ return fileMapper.selectListWithRelationsByQueryAs(queryWrapper, FileVO.class);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean insert(FileBO fileBO) {
|
|
|
+ File file = MapstructUtils.convert(fileBO, File.class);
|
|
|
+ return this.save(file);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean update(FileBO fileBO) {
|
|
|
+ File file = MapstructUtils.convert(fileBO, File.class);
|
|
|
+ return this.updateById(file);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean delete(List<String> ids) {
|
|
|
+ return this.removeByIds(ids);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean deleteFile(List<String> ids) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public FileVO selectById(String id) {
|
|
|
+ QueryWrapper queryWrapper = query().where(FILE.FILE_ID.eq(id).when(StrUtils.isNotBlank(id))).and(FILE.FILE_ID.isNull().when(StrUtils.isBlank(id)));
|
|
|
+ return this.getOneAs(queryWrapper, FileVO.class);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<FileVO> selectByUrl(List<String> urls) {
|
|
|
+ QueryWrapper queryWrapper = query().where(FILE.DOWNLOAD_URL.in(urls));
|
|
|
+ return fileMapper.selectListWithRelationsByQueryAs(queryWrapper, FileVO.class);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String upload(String engine, MultipartFile file) {
|
|
|
+ return this.storageFile(engine, file);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void download(String id, HttpServletResponse response) throws IOException {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void authDownload(String id, HttpServletResponse response) throws IOException {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 存储文件
|
|
|
+ *
|
|
|
+ * @param engine 引擎
|
|
|
+ * @param file 文件
|
|
|
+ * @return 文件id/url
|
|
|
+ */
|
|
|
+ private String storageFile(String engine, MultipartFile file) {
|
|
|
+ String key = UUID.randomUUID().toString().replace("-", "");
|
|
|
+ String storageUrl = FileLocalUtils.storageFileWithReturnUrl("LocalBucket", key, file);
|
|
|
+ File fileInfo = new File();
|
|
|
+ fileInfo.setFileId(key);
|
|
|
+ fileInfo.setEngine(engine);
|
|
|
+ fileInfo.setBucket("LocalBucket");
|
|
|
+ fileInfo.setOriginalName(file.getOriginalFilename());
|
|
|
+ fileInfo.setDownloadUrl(storageUrl);
|
|
|
+ String suffix = ObjectUtil.isNotEmpty(file.getOriginalFilename()) ? StrUtil.subAfter(file.getOriginalFilename(),
|
|
|
+ StrUtil.DOT, true) : null;
|
|
|
+ fileInfo.setFileSuffix(suffix);
|
|
|
+ fileInfo.setSizeKb(12);
|
|
|
+ fileInfo.setSizeInfo(FileUtils.readableFileSize(file.getSize()));
|
|
|
+ fileInfo.setFileName(ObjectUtil.isNotEmpty(fileInfo.getFileSuffix()) ? key + StrUtil.DOT + fileInfo.getFileSuffix() : null);
|
|
|
+ if (ObjectUtil.isNotEmpty(suffix)) {
|
|
|
+ if (isPic(suffix)) {
|
|
|
+ try {
|
|
|
+ fileInfo.setThumbnail(ImgUtils.toBase64DataUri(ImgUtils.scale(ImgUtils.toImage(file.getBytes()),
|
|
|
+ 100, 100, null), suffix));
|
|
|
+ } catch (Exception ignored) {
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ fileInfo.setStorageUrl(storageUrl);
|
|
|
+
|
|
|
+ this.save(fileInfo);
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+
|
|
|
+ private void unifiedDownload(FileBO fileBO, HttpServletResponse response, boolean isDownloadAuth) {
|
|
|
+ FileVO fileVO = this.selectById(fileBO.getFileId());
|
|
|
+ java.io.File file = FileUtils.file(fileVO.getStorageUrl());
|
|
|
+ if (!FileUtil.exist(file)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ FileDownloadUtils.download(file.getName(), IoUtils.readBytes(FileUtil.getInputStream(file)), response);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断是否是图片
|
|
|
+ *
|
|
|
+ * @param fileSuffix 文件后缀
|
|
|
+ * @return 是否是图片
|
|
|
+ */
|
|
|
+ private static boolean isPic(String fileSuffix) {
|
|
|
+ fileSuffix = fileSuffix.toLowerCase();
|
|
|
+ return ImgUtils.IMAGE_TYPE_GIF.equals(fileSuffix)
|
|
|
+ || ImgUtils.IMAGE_TYPE_JPG.equals(fileSuffix)
|
|
|
+ || ImgUtils.IMAGE_TYPE_JPEG.equals(fileSuffix)
|
|
|
+ || ImgUtils.IMAGE_TYPE_BMP.equals(fileSuffix)
|
|
|
+ || ImgUtils.IMAGE_TYPE_PNG.equals(fileSuffix)
|
|
|
+ || ImgUtils.IMAGE_TYPE_PSD.equals(fileSuffix);
|
|
|
+ }
|
|
|
+}
|