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.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<String, String> 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();