fix: use the original name when uploading the attachment to upyun oss (#2181) (#2185)

pull/2187/head
okayhu 2022-06-23 17:54:24 +08:00 committed by GitHub
parent f5d35dd26d
commit 8589889af3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 29 additions and 23 deletions

View File

@ -18,8 +18,8 @@ import run.halo.app.exception.FileOperationException;
import run.halo.app.model.enums.AttachmentType; import run.halo.app.model.enums.AttachmentType;
import run.halo.app.model.properties.UpOssProperties; import run.halo.app.model.properties.UpOssProperties;
import run.halo.app.model.support.UploadResult; import run.halo.app.model.support.UploadResult;
import run.halo.app.repository.AttachmentRepository;
import run.halo.app.service.OptionService; import run.halo.app.service.OptionService;
import run.halo.app.utils.FilenameUtils;
import run.halo.app.utils.ImageUtils; import run.halo.app.utils.ImageUtils;
/** /**
@ -34,9 +34,12 @@ import run.halo.app.utils.ImageUtils;
public class UpOssFileHandler implements FileHandler { public class UpOssFileHandler implements FileHandler {
private final OptionService optionService; private final OptionService optionService;
private final AttachmentRepository attachmentRepository;
public UpOssFileHandler(OptionService optionService) { public UpOssFileHandler(OptionService optionService,
AttachmentRepository attachmentRepository) {
this.optionService = optionService; this.optionService = optionService;
this.attachmentRepository = attachmentRepository;
} }
@Override @Override
@ -65,46 +68,49 @@ public class UpOssFileHandler implements FileHandler {
Map<String, String> params = new HashMap<>(); Map<String, String> params = new HashMap<>();
try { try {
// Get file basename FilePathDescriptor pathDescriptor = new FilePathDescriptor.Builder()
String basename = .setBasePath(protocol + domain)
FilenameUtils.getBasename(Objects.requireNonNull(file.getOriginalFilename())); .setSubPath(source)
// Get file extension .setAutomaticRename(true)
String extension = FilenameUtils.getExtension(file.getOriginalFilename()); .setRenamePredicate(relativePath ->
attachmentRepository
.countByFileKeyAndType(relativePath, AttachmentType.UPOSS) > 0)
.setOriginalName(file.getOriginalFilename())
.build();
// Get md5 value of the file // Get md5 value of the file
String md5OfFile = DigestUtils.md5DigestAsHex(file.getInputStream()); String md5OfFile = DigestUtils.md5DigestAsHex(file.getInputStream());
// Build file path
String upFilePath =
StringUtils.appendIfMissing(source, "/") + md5OfFile + '.' + extension;
// Set md5Content // Set md5Content
params.put(RestManager.PARAMS.CONTENT_MD5.getValue(), md5OfFile); params.put(RestManager.PARAMS.CONTENT_MD5.getValue(), md5OfFile);
String relativePath = pathDescriptor.getRelativePath();
// Write file // Write file
Response result = manager.writeFile(upFilePath, file.getInputStream(), params); Response result = manager.writeFile(relativePath, file.getInputStream(), params);
if (!result.isSuccessful()) { if (!result.isSuccessful()) {
throw new FileOperationException( throw new FileOperationException(
"上传附件 " + file.getOriginalFilename() + " 到又拍云失败" + upFilePath); "上传附件 " + file.getOriginalFilename() + " 到又拍云失败" + relativePath);
} }
String fullPath = pathDescriptor.getFullPath();
String filePath = protocol + StringUtils.removeEnd(domain, "/") + upFilePath; String extension = pathDescriptor.getExtension();
// Build upload result // Build upload result
UploadResult uploadResult = new UploadResult(); UploadResult uploadResult = new UploadResult();
uploadResult.setFilename(basename); uploadResult.setFilename(pathDescriptor.getName());
uploadResult.setKey(relativePath);
uploadResult.setSuffix(extension);
uploadResult uploadResult
.setFilePath(StringUtils.isBlank(styleRule) ? filePath : filePath + styleRule); .setFilePath(StringUtils.isBlank(styleRule) ? fullPath : fullPath + styleRule);
uploadResult.setKey(upFilePath);
uploadResult uploadResult
.setMediaType(MediaType.valueOf(Objects.requireNonNull(file.getContentType()))); .setMediaType(MediaType.valueOf(Objects.requireNonNull(file.getContentType())));
uploadResult.setSuffix(extension);
uploadResult.setSize(file.getSize()); uploadResult.setSize(file.getSize());
// Handle thumbnail // Handle thumbnail
handleImageMetadata(file, uploadResult, () -> { handleImageMetadata(file, uploadResult, () -> {
if (ImageUtils.EXTENSION_ICO.equals(extension)) { if (ImageUtils.EXTENSION_ICO.equals(extension)) {
uploadResult.setThumbPath(filePath); uploadResult.setThumbPath(fullPath);
return filePath; return fullPath;
} else { } else {
return StringUtils.isBlank(thumbnailStyleRule) ? filePath : return StringUtils.isBlank(thumbnailStyleRule) ? fullPath :
filePath + thumbnailStyleRule; fullPath + thumbnailStyleRule;
} }
}); });
result.close(); result.close();