mirror of https://github.com/halo-dev/halo
refactor: the usage of file path descriptor
parent
32db2334c0
commit
b46ff3b44e
|
@ -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());
|
||||
|
||||
|
|
|
@ -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 =
|
||||
|
|
|
@ -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();
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -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());
|
||||
|
||||
|
|
|
@ -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())
|
||||
|
|
|
@ -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());
|
||||
|
|
|
@ -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();
|
||||
|
|
Loading…
Reference in New Issue