refactor: the usage of file path descriptor

pull/1500/head
guqing 2021-10-25 09:45:08 +08:00
parent 32db2334c0
commit b46ff3b44e
7 changed files with 52 additions and 54 deletions

View File

@ -83,15 +83,11 @@ public class AliOssFileHandler implements FileHandler {
}
try {
FilePathDescriptor uploadFilePath = new FilePathDescriptor.Builder()
.setBasePath(basePath.toString())
.setSubPath(source)
.setAutomaticRename(true)
.setRenamePredicate(relativePath ->
FilePathDescriptor uploadFilePath =
getFilePathDescriptor(basePath.toString(), source, file.getOriginalFilename(),
relativePath ->
attachmentRepository
.countByFileKeyAndType(relativePath, AttachmentType.ALIOSS) > 0)
.setOriginalName(file.getOriginalFilename())
.build();
.countByFileKeyAndType(relativePath, AttachmentType.ALIOSS) > 0);
log.info(basePath.toString());

View File

@ -71,15 +71,11 @@ public class BaiduBosFileHandler implements FileHandler {
domain = protocol + domain;
try {
FilePathDescriptor pathDescriptor = new FilePathDescriptor.Builder()
.setBasePath(domain)
.setSubPath(source)
.setAutomaticRename(true)
.setRenamePredicate(relativePath ->
FilePathDescriptor pathDescriptor =
getFilePathDescriptor(domain, source, file.getOriginalFilename(),
relativePath ->
attachmentRepository
.countByFileKeyAndType(relativePath, AttachmentType.BAIDUBOS) > 0)
.setOriginalName(file.getOriginalFilename())
.build();
.countByFileKeyAndType(relativePath, AttachmentType.BAIDUBOS) > 0);
// Upload
PutObjectResponse putObjectResponseFromInputStream =

View File

@ -5,6 +5,7 @@ import static run.halo.app.model.support.HaloConst.FILE_SEPARATOR;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.util.function.Predicate;
import java.util.function.Supplier;
import javax.imageio.ImageReader;
import org.apache.commons.lang3.StringUtils;
@ -117,4 +118,25 @@ public interface FileHandler {
*/
AttachmentType getAttachmentType();
/**
* Gets file path descriptor.
*
* @param basePath root path, e.g. 'https://halo.run' or '/'
* @param subPath sub path
* @param originalFileName original file name
* @param predicate predicate of auto rename
* @return a file path descriptor
*/
default FilePathDescriptor getFilePathDescriptor(String basePath, String subPath,
String originalFileName, Predicate<String> predicate) {
return new FilePathDescriptor.Builder()
.setBasePath(basePath)
.setSubPath(subPath)
.setAutomaticRename(true)
.setRenamePredicate(predicate)
.setOriginalName(originalFileName)
.build();
}
}

View File

@ -81,15 +81,11 @@ public class HuaweiObsFileHandler implements FileHandler {
}
try {
FilePathDescriptor pathDescriptor = new FilePathDescriptor.Builder()
.setBasePath(domain)
.setSubPath(source)
.setAutomaticRename(true)
.setRenamePredicate(relativePath ->
FilePathDescriptor pathDescriptor =
getFilePathDescriptor(domain, source, file.getOriginalFilename(),
relativePath ->
attachmentRepository
.countByFileKeyAndType(relativePath, AttachmentType.HUAWEIOBS) > 0)
.setOriginalName(file.getOriginalFilename())
.build();
.countByFileKeyAndType(relativePath, AttachmentType.HUAWEIOBS) > 0);
log.info(basePath.toString());

View File

@ -65,15 +65,11 @@ public class MinioFileHandler implements FileHandler {
.build();
try {
FilePathDescriptor pathDescriptor = new FilePathDescriptor.Builder()
.setBasePath(endpoint + bucketName)
.setSubPath(source)
.setAutomaticRename(true)
.setRenamePredicate(relativePath ->
FilePathDescriptor pathDescriptor =
getFilePathDescriptor(endpoint + bucketName, source, file.getOriginalFilename(),
relativePath ->
attachmentRepository
.countByFileKeyAndType(relativePath, AttachmentType.MINIO) > 0)
.setOriginalName(file.getOriginalFilename())
.build();
.countByFileKeyAndType(relativePath, AttachmentType.MINIO) > 0);
PutObjectArgs putObjectArgs = PutObjectArgs.builder()
.contentType(file.getContentType())

View File

@ -100,15 +100,11 @@ public class QiniuOssFileHandler implements FileHandler {
.append(URL_SEPARATOR);
try {
FilePathDescriptor pathDescriptor = new FilePathDescriptor.Builder()
.setBasePath(basePath.toString())
.setSubPath(source)
.setAutomaticRename(true)
.setRenamePredicate(relativePath ->
FilePathDescriptor pathDescriptor =
getFilePathDescriptor(basePath.toString(), source, file.getOriginalFilename(),
relativePath ->
attachmentRepository
.countByFileKeyAndType(relativePath, AttachmentType.QINIUOSS) > 0)
.setOriginalName(file.getOriginalFilename())
.build();
.countByFileKeyAndType(relativePath, AttachmentType.QINIUOSS) > 0);
// Get file recorder for temp directory
FileRecorder fileRecorder = new FileRecorder(tmpPath.toFile());

View File

@ -90,15 +90,11 @@ public class TencentCosFileHandler implements FileHandler {
}
try {
FilePathDescriptor pathDescriptor = new FilePathDescriptor.Builder()
.setBasePath(basePath.toString())
.setSubPath(source)
.setAutomaticRename(true)
.setRenamePredicate(relativePath ->
FilePathDescriptor pathDescriptor =
getFilePathDescriptor(basePath.toString(), source, file.getOriginalFilename(),
relativePath ->
attachmentRepository
.countByFileKeyAndType(relativePath, AttachmentType.TENCENTCOS) > 0)
.setOriginalName(file.getOriginalFilename())
.build();
.countByFileKeyAndType(relativePath, AttachmentType.TENCENTCOS) > 0);
// Upload
ObjectMetadata objectMetadata = new ObjectMetadata();