|
|
@@ -1,233 +0,0 @@
|
|
|
-package cn.reghao.bnt.web.admin.service;
|
|
|
-
|
|
|
-import cn.reghao.bnt.web.admin.model.vo.DiskFileInfo;
|
|
|
-import cn.reghao.bnt.web.admin.model.vo.FileInfo;
|
|
|
-import cn.reghao.jutil.jdk.converter.ByteConverter;
|
|
|
-import cn.reghao.jutil.jdk.converter.ByteType;
|
|
|
-import cn.reghao.jutil.jdk.security.DigestUtil;
|
|
|
-import cn.reghao.jutil.web.ServletUtil;
|
|
|
-import cn.reghao.bnt.web.admin.db.repository.DiskFileRepository;
|
|
|
-import cn.reghao.bnt.web.admin.model.po.DiskFile;
|
|
|
-import cn.reghao.bnt.web.config.AppProperties;
|
|
|
-import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.apache.commons.io.FileUtils;
|
|
|
-import org.springframework.cache.annotation.Cacheable;
|
|
|
-import org.springframework.data.domain.Page;
|
|
|
-import org.springframework.data.domain.PageImpl;
|
|
|
-import org.springframework.data.domain.PageRequest;
|
|
|
-import org.springframework.data.domain.Sort;
|
|
|
-import org.springframework.stereotype.Service;
|
|
|
-import org.springframework.web.multipart.MultipartFile;
|
|
|
-
|
|
|
-import jakarta.annotation.PostConstruct;
|
|
|
-import jakarta.servlet.http.HttpServletResponse;
|
|
|
-import java.io.*;
|
|
|
-import java.nio.file.Files;
|
|
|
-import java.nio.file.Path;
|
|
|
-import java.nio.file.StandardCopyOption;
|
|
|
-import java.util.List;
|
|
|
-import java.util.UUID;
|
|
|
-import java.util.stream.Collectors;
|
|
|
-
|
|
|
-/**
|
|
|
- * @author reghao
|
|
|
- * @date 2024-01-19 21:29:24
|
|
|
- */
|
|
|
-@Slf4j
|
|
|
-@Service
|
|
|
-public class FileService {
|
|
|
- // 8MiB
|
|
|
- private final int bufSize = 1024*1024*8;
|
|
|
- private final AppProperties appProperties;
|
|
|
- private final DiskFileRepository diskFileRepository;
|
|
|
- private final ByteConverter byteConverter;
|
|
|
-
|
|
|
- public FileService(DiskFileRepository diskFileRepository, AppProperties appProperties, ByteConverter byteConverter) {
|
|
|
- this.diskFileRepository = diskFileRepository;
|
|
|
- this.appProperties = appProperties;
|
|
|
- this.byteConverter = byteConverter;
|
|
|
- }
|
|
|
-
|
|
|
- @PostConstruct
|
|
|
- public void initLuceneDir() throws IOException {
|
|
|
- String luceneDir = appProperties.getLuceneDir();
|
|
|
- File dir2 = new File(luceneDir);
|
|
|
- if (!dir2.exists()) {
|
|
|
- log.info("创建索引目录...");
|
|
|
- FileUtils.forceMkdir(dir2);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public void getFile(String objectName) throws IOException {
|
|
|
- DiskFile diskFile = getByObjectName(objectName);
|
|
|
- if (diskFile == null) {
|
|
|
- log.error("DiskFile {} not exist", objectName);
|
|
|
- writeResponse(HttpServletResponse.SC_NOT_FOUND);
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- String absolutePath = diskFile.getAbsolutePath();
|
|
|
- File file = new File(absolutePath);
|
|
|
- if (!file.exists() || file.isDirectory()) {
|
|
|
- log.error("file {} not exist", absolutePath);
|
|
|
- writeResponse(HttpServletResponse.SC_NOT_FOUND);
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- String contentType = diskFile.getContentType();
|
|
|
- long size = diskFile.getSize();
|
|
|
- HttpServletResponse response = ServletUtil.getResponse();
|
|
|
- response.setStatus(HttpServletResponse.SC_OK);
|
|
|
- response.setContentType(contentType);
|
|
|
- response.setContentLengthLong(size);
|
|
|
-
|
|
|
- OutputStream outputStream = response.getOutputStream();
|
|
|
- writeResponse(outputStream, absolutePath, 0, size);
|
|
|
- }
|
|
|
-
|
|
|
- @Cacheable(cacheNames = "filePaths", key = "#objectName", unless = "#result == null")
|
|
|
- public DiskFile getByObjectName(String objectName) {
|
|
|
- DiskFile diskFile = diskFileRepository.findByObjectName(objectName);
|
|
|
- return diskFile;
|
|
|
- }
|
|
|
-
|
|
|
- private void writeResponse(int statusCode) throws IOException {
|
|
|
- HttpServletResponse response = ServletUtil.getResponse();
|
|
|
- response.setStatus(statusCode);
|
|
|
- OutputStream outputStream = response.getOutputStream();
|
|
|
- outputStream.flush();
|
|
|
- outputStream.close();
|
|
|
- }
|
|
|
-
|
|
|
- private void writeResponse(OutputStream outputStream, String absolutePath, long start, long end) throws IOException {
|
|
|
- RandomAccessFile raf = new RandomAccessFile(absolutePath, "r");
|
|
|
- raf.seek(start);
|
|
|
-
|
|
|
- long len = end-start+1;
|
|
|
- if (len < bufSize) {
|
|
|
- int len1 = (int) len;
|
|
|
- byte[] buf1 = new byte[len1];
|
|
|
- int readLen1 = raf.read(buf1, 0, len1);
|
|
|
- outputStream.write(buf1, 0, readLen1);
|
|
|
- } else {
|
|
|
- byte[] buf = new byte[bufSize];
|
|
|
- long totalRead = 0;
|
|
|
- int readLen;
|
|
|
- while ((readLen = raf.read(buf, 0, bufSize)) != -1) {
|
|
|
- outputStream.write(buf, 0, readLen);
|
|
|
- totalRead += readLen;
|
|
|
-
|
|
|
- long left = len - totalRead;
|
|
|
- if (left < bufSize) {
|
|
|
- int left1 = (int) left;
|
|
|
- byte[] buf1 = new byte[left1];
|
|
|
- int readLen1 = raf.read(buf1, 0, left1);
|
|
|
- outputStream.write(buf1, 0, readLen1);
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- outputStream.flush();
|
|
|
- outputStream.close();
|
|
|
- raf.close();
|
|
|
- }
|
|
|
-
|
|
|
- public DiskFile putDiskFile(MultipartFile file) throws Exception {
|
|
|
- long size = file.getSize();
|
|
|
- String filename = file.getOriginalFilename();
|
|
|
- String suffix = getSuffix(filename);
|
|
|
-
|
|
|
- String objectId = UUID.randomUUID().toString().replace("-", "");;
|
|
|
- String objectName;
|
|
|
- if (suffix.isBlank()) {
|
|
|
- objectName = String.format("file/%s", objectId);
|
|
|
- } else {
|
|
|
- objectName = String.format("file/%s%s", objectId, suffix);
|
|
|
- }
|
|
|
-
|
|
|
- String contentId = UUID.randomUUID().toString().replace("-", "");
|
|
|
- String fileDir = appProperties.getFileDir();
|
|
|
- String destPath = String.format("%s/%s", fileDir, contentId);
|
|
|
- File savedFile = saveFile(file.getInputStream(), destPath);
|
|
|
- String contentType = "image/jpeg";
|
|
|
- int fileType = 1001;
|
|
|
- String sha256sum = DigestUtil.sha256sum(savedFile.getAbsolutePath());
|
|
|
-
|
|
|
- List<DiskFile> diskFiles = diskFileRepository.findBySha256sum(sha256sum);
|
|
|
- DiskFile diskFile;
|
|
|
- if (!diskFiles.isEmpty()) {
|
|
|
- DiskFile existFile = diskFiles.get(0);
|
|
|
- diskFile = new DiskFile(objectName, objectId, existFile, filename);
|
|
|
- FileUtils.deleteQuietly(savedFile);
|
|
|
- } else {
|
|
|
- diskFile = new DiskFile(objectName, objectId, savedFile.getAbsolutePath(), sha256sum, filename, contentType, fileType, size);
|
|
|
- }
|
|
|
-
|
|
|
- diskFileRepository.save(diskFile);
|
|
|
- return diskFile;
|
|
|
- }
|
|
|
-
|
|
|
- private File saveFile(InputStream inputStream, String absolutePath) throws IOException {
|
|
|
- File file = new File(absolutePath);
|
|
|
- if (file.exists()) {
|
|
|
- throw new IOException(absolutePath + " exist");
|
|
|
- }
|
|
|
-
|
|
|
- Files.copy(inputStream, Path.of(absolutePath), StandardCopyOption.REPLACE_EXISTING);
|
|
|
- return file;
|
|
|
- }
|
|
|
-
|
|
|
- private String getSuffix(String filename) {
|
|
|
- if (filename == null) {
|
|
|
- return "";
|
|
|
- }
|
|
|
-
|
|
|
- int idx = filename.lastIndexOf(".");
|
|
|
- return idx == -1 ? "" : filename.substring(idx);
|
|
|
- }
|
|
|
-
|
|
|
- public Page<FileInfo> getDiskFiles(int pageNumber) {
|
|
|
- int owner = UserContext.getUserId();
|
|
|
- Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
|
|
|
- int pageSize = 12;
|
|
|
- PageRequest pageRequest = PageRequest.of(pageNumber-1, pageSize, sort);
|
|
|
- Page<DiskFile> page = diskFileRepository.findByOwner(owner, pageRequest);
|
|
|
- List<FileInfo> list = page.getContent().stream().map(diskFile -> {
|
|
|
- String size = byteConverter.convert(ByteType.Bytes, diskFile.getSize());
|
|
|
- return new FileInfo(diskFile, size);
|
|
|
- }).collect(Collectors.toList());
|
|
|
-
|
|
|
- return new PageImpl<>(list, pageRequest, page.getTotalElements());
|
|
|
- }
|
|
|
-
|
|
|
- public Page<DiskFileInfo> getImageFiles(int pageNumber) {
|
|
|
- int owner = UserContext.getUserId();
|
|
|
- Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
|
|
|
- int pageSize = 12;
|
|
|
- PageRequest pageRequest = PageRequest.of(pageNumber-1, pageSize, sort);
|
|
|
- int fileType = 1001;
|
|
|
- Page<DiskFile> page = diskFileRepository.findByFileTypeAndOwner(fileType, owner, pageRequest);
|
|
|
- List<DiskFileInfo> list = page.getContent().stream().map(diskFile -> {
|
|
|
- String fileId = diskFile.getObjectId();
|
|
|
- String filename = diskFile.getFilename();
|
|
|
- String objectName = diskFile.getObjectName();
|
|
|
- String url = String.format("/%s", objectName);
|
|
|
- return new DiskFileInfo(fileId, filename, url);
|
|
|
- }).collect(Collectors.toList());
|
|
|
- return new PageImpl<>(list, pageRequest, page.getTotalElements());
|
|
|
- }
|
|
|
-
|
|
|
- public DiskFile getDiskFile(String objectId) {
|
|
|
- return diskFileRepository.findByObjectId(objectId);
|
|
|
- }
|
|
|
-
|
|
|
- public String getObjectName(String path) {
|
|
|
- String objectName = "";
|
|
|
- if (!path.equals("/")) {
|
|
|
- objectName = path.substring(1) + "/";
|
|
|
- }
|
|
|
-
|
|
|
- return objectName;
|
|
|
- }
|
|
|
-}
|