From 8589889af3ab538aaaf9edb0b65e7e133514a1ac Mon Sep 17 00:00:00 2001 From: okayhu Date: Thu, 23 Jun 2022 17:54:24 +0800 Subject: [PATCH] fix: use the original name when uploading the attachment to upyun oss (#2181) (#2185) --- .../app/handler/file/UpOssFileHandler.java | 52 +++++++++++-------- 1 file changed, 29 insertions(+), 23 deletions(-) diff --git a/src/main/java/run/halo/app/handler/file/UpOssFileHandler.java b/src/main/java/run/halo/app/handler/file/UpOssFileHandler.java index 3881b7803..322b446e9 100644 --- a/src/main/java/run/halo/app/handler/file/UpOssFileHandler.java +++ b/src/main/java/run/halo/app/handler/file/UpOssFileHandler.java @@ -18,8 +18,8 @@ import run.halo.app.exception.FileOperationException; import run.halo.app.model.enums.AttachmentType; import run.halo.app.model.properties.UpOssProperties; import run.halo.app.model.support.UploadResult; +import run.halo.app.repository.AttachmentRepository; import run.halo.app.service.OptionService; -import run.halo.app.utils.FilenameUtils; import run.halo.app.utils.ImageUtils; /** @@ -34,9 +34,12 @@ import run.halo.app.utils.ImageUtils; public class UpOssFileHandler implements FileHandler { private final OptionService optionService; + private final AttachmentRepository attachmentRepository; - public UpOssFileHandler(OptionService optionService) { + public UpOssFileHandler(OptionService optionService, + AttachmentRepository attachmentRepository) { this.optionService = optionService; + this.attachmentRepository = attachmentRepository; } @Override @@ -65,46 +68,49 @@ public class UpOssFileHandler implements FileHandler { Map params = new HashMap<>(); try { - // Get file basename - String basename = - FilenameUtils.getBasename(Objects.requireNonNull(file.getOriginalFilename())); - // Get file extension - String extension = FilenameUtils.getExtension(file.getOriginalFilename()); + FilePathDescriptor pathDescriptor = new FilePathDescriptor.Builder() + .setBasePath(protocol + domain) + .setSubPath(source) + .setAutomaticRename(true) + .setRenamePredicate(relativePath -> + attachmentRepository + .countByFileKeyAndType(relativePath, AttachmentType.UPOSS) > 0) + .setOriginalName(file.getOriginalFilename()) + .build(); + // Get md5 value of the file String md5OfFile = DigestUtils.md5DigestAsHex(file.getInputStream()); - // Build file path - String upFilePath = - StringUtils.appendIfMissing(source, "/") + md5OfFile + '.' + extension; // Set md5Content params.put(RestManager.PARAMS.CONTENT_MD5.getValue(), md5OfFile); + + String relativePath = pathDescriptor.getRelativePath(); // Write file - Response result = manager.writeFile(upFilePath, file.getInputStream(), params); + Response result = manager.writeFile(relativePath, file.getInputStream(), params); if (!result.isSuccessful()) { throw new FileOperationException( - "上传附件 " + file.getOriginalFilename() + " 到又拍云失败" + upFilePath); + "上传附件 " + file.getOriginalFilename() + " 到又拍云失败" + relativePath); } - - String filePath = protocol + StringUtils.removeEnd(domain, "/") + upFilePath; - + String fullPath = pathDescriptor.getFullPath(); + String extension = pathDescriptor.getExtension(); // Build upload result UploadResult uploadResult = new UploadResult(); - uploadResult.setFilename(basename); + uploadResult.setFilename(pathDescriptor.getName()); + uploadResult.setKey(relativePath); + uploadResult.setSuffix(extension); uploadResult - .setFilePath(StringUtils.isBlank(styleRule) ? filePath : filePath + styleRule); - uploadResult.setKey(upFilePath); + .setFilePath(StringUtils.isBlank(styleRule) ? fullPath : fullPath + styleRule); uploadResult .setMediaType(MediaType.valueOf(Objects.requireNonNull(file.getContentType()))); - uploadResult.setSuffix(extension); uploadResult.setSize(file.getSize()); // Handle thumbnail handleImageMetadata(file, uploadResult, () -> { if (ImageUtils.EXTENSION_ICO.equals(extension)) { - uploadResult.setThumbPath(filePath); - return filePath; + uploadResult.setThumbPath(fullPath); + return fullPath; } else { - return StringUtils.isBlank(thumbnailStyleRule) ? filePath : - filePath + thumbnailStyleRule; + return StringUtils.isBlank(thumbnailStyleRule) ? fullPath : + fullPath + thumbnailStyleRule; } }); result.close();