refactor: 重构文件名称是否重复的判断方式

pull/1500/head
guqing 2021-10-21 18:14:54 +08:00
parent 68b4614cf6
commit 32db2334c0
11 changed files with 63 additions and 33 deletions

View File

@ -87,8 +87,9 @@ public class AliOssFileHandler implements FileHandler {
.setBasePath(basePath.toString()) .setBasePath(basePath.toString())
.setSubPath(source) .setSubPath(source)
.setAutomaticRename(true) .setAutomaticRename(true)
.setRenamePredicate(builder -> .setRenamePredicate(relativePath ->
attachmentRepository.countByPath(builder.getFullPath()) > 0) attachmentRepository
.countByFileKeyAndType(relativePath, AttachmentType.ALIOSS) > 0)
.setOriginalName(file.getOriginalFilename()) .setOriginalName(file.getOriginalFilename())
.build(); .build();

View File

@ -75,8 +75,9 @@ public class BaiduBosFileHandler implements FileHandler {
.setBasePath(domain) .setBasePath(domain)
.setSubPath(source) .setSubPath(source)
.setAutomaticRename(true) .setAutomaticRename(true)
.setRenamePredicate(builder -> .setRenamePredicate(relativePath ->
attachmentRepository.countByPath(builder.getFullPath()) > 0) attachmentRepository
.countByFileKeyAndType(relativePath, AttachmentType.BAIDUBOS) > 0)
.setOriginalName(file.getOriginalFilename()) .setOriginalName(file.getOriginalFilename())
.build(); .build();

View File

@ -3,10 +3,10 @@ package run.halo.app.handler.file;
import java.util.function.Predicate; import java.util.function.Predicate;
import lombok.Data; import lombok.Data;
import lombok.experimental.Accessors; import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import run.halo.app.utils.FilenameUtils; import run.halo.app.utils.FilenameUtils;
import run.halo.app.utils.HaloUtils;
/** /**
* File path descriptor. * File path descriptor.
@ -14,6 +14,7 @@ import run.halo.app.utils.HaloUtils;
* @author guqing * @author guqing
* @since 2021-10-21 * @since 2021-10-21
*/ */
@Slf4j
@Data @Data
@Accessors(chain = true) @Accessors(chain = true)
public final class FilePathDescriptor { public final class FilePathDescriptor {
@ -30,15 +31,16 @@ public final class FilePathDescriptor {
private String name; private String name;
private String extension; private String extension;
private String path; private String subPath;
private String basePath; private String basePath;
private String nameSuffix = StringUtils.EMPTY; private String nameSuffix = StringUtils.EMPTY;
private String separator = "/"; private String separator = "/";
private boolean automaticRename; private boolean automaticRename;
private Predicate<Builder> renamePredicate; private Predicate<String> renamePredicate;
private String relativePath;
public Builder setSubPath(String subPath) { public Builder setSubPath(String subPath) {
this.path = StringUtils.removeEnd(subPath, separator); this.subPath = StringUtils.removeEnd(subPath, separator);
return this; return this;
} }
@ -47,7 +49,7 @@ public final class FilePathDescriptor {
return this; return this;
} }
public Builder setRenamePredicate(Predicate<Builder> predicate) { public Builder setRenamePredicate(Predicate<String> predicate) {
this.renamePredicate = predicate; this.renamePredicate = predicate;
return this; return this;
} }
@ -80,7 +82,7 @@ public final class FilePathDescriptor {
} }
public Builder setBasePath(String basePath) { public Builder setBasePath(String basePath) {
this.basePath = basePath; this.basePath = StringUtils.removeEnd(basePath, separator);
return this; return this;
} }
@ -98,7 +100,7 @@ public final class FilePathDescriptor {
return this; return this;
} }
String getName() { String buildName() {
StringBuilder sb = new StringBuilder() StringBuilder sb = new StringBuilder()
.append(this.name); .append(this.name);
if (shouldRename()) { if (shouldRename()) {
@ -112,16 +114,20 @@ public final class FilePathDescriptor {
String getFullName() { String getFullName() {
// eg. hello.jpg -> hello-uuid-thumbnail.jpg // eg. hello.jpg -> hello-uuid-thumbnail.jpg
if (StringUtils.isNotBlank(this.extension)) { if (StringUtils.isNotBlank(this.extension)) {
return getName() + '.' + this.extension; return this.name + '.' + this.extension;
} }
return getName(); return this.name;
} }
String getFullPath() { String getFullPath() {
if (StringUtils.isNotBlank(this.basePath)) { if (StringUtils.isNotBlank(this.basePath)) {
return getPath(this.basePath, this.path, this.getFullName()); return getPath(this.basePath, this.subPath, this.getFullName());
} }
return getPath(this.path, this.getFullName()); return getPath(this.subPath, this.getFullName());
}
String getRelativePath() {
return getPath(this.subPath, getFullName());
} }
private boolean shouldRename() { private boolean shouldRename() {
@ -133,7 +139,7 @@ public final class FilePathDescriptor {
return true; return true;
} }
// renamePredicate not null // renamePredicate not null
return renamePredicate.test(this); return renamePredicate.test(this.relativePath);
} }
private String getPath(String first, String... more) { private String getPath(String first, String... more) {
@ -162,14 +168,22 @@ public final class FilePathDescriptor {
* @return file path * @return file path
*/ */
public FilePathDescriptor build() { public FilePathDescriptor build() {
return new FilePathDescriptor() // build relative path first, used to determine if it needs to be renamed
this.relativePath = getRelativePath();
// then build name, returns a new name if the relative path exists
this.name = buildName();
FilePathDescriptor descriptor = new FilePathDescriptor()
.setBasePath(this.basePath) .setBasePath(this.basePath)
.setSubPath(this.path) .setSubPath(this.subPath)
.setRelativePath(getPath(this.path, getFullName())) // regenerate relative path
.setName(FilenameUtils.getBasename(getName())) .setRelativePath(getRelativePath())
.setName(FilenameUtils.getBasename(this.name))
.setExtension(extension) .setExtension(extension)
.setFullPath(getFullPath()) .setFullPath(getFullPath())
.setFullName(getFullName()); .setFullName(getFullName());
log.info("FilePathDescriptor: [{}]", descriptor);
return descriptor;
} }
} }
} }

View File

@ -85,8 +85,9 @@ public class HuaweiObsFileHandler implements FileHandler {
.setBasePath(domain) .setBasePath(domain)
.setSubPath(source) .setSubPath(source)
.setAutomaticRename(true) .setAutomaticRename(true)
.setRenamePredicate(builder -> .setRenamePredicate(relativePath ->
attachmentRepository.countByPath(builder.getFullPath()) > 0) attachmentRepository
.countByFileKeyAndType(relativePath, AttachmentType.HUAWEIOBS) > 0)
.setOriginalName(file.getOriginalFilename()) .setOriginalName(file.getOriginalFilename())
.build(); .build();

View File

@ -94,8 +94,9 @@ public class LocalFileHandler implements FileHandler {
.setSubPath(generatePath()) .setSubPath(generatePath())
.setSeparator(FILE_SEPARATOR) .setSeparator(FILE_SEPARATOR)
.setAutomaticRename(true) .setAutomaticRename(true)
.setRenamePredicate(builder -> .setRenamePredicate(relativePath ->
attachmentRepository.countByPath(builder.getFullPath()) > 0) attachmentRepository
.countByFileKeyAndType(relativePath, AttachmentType.LOCAL) > 0)
.setOriginalName(file.getOriginalFilename()) .setOriginalName(file.getOriginalFilename())
.build(); .build();
log.info("Uploading file: [{}] to directory: [{}]", file.getOriginalFilename(), log.info("Uploading file: [{}] to directory: [{}]", file.getOriginalFilename(),

View File

@ -69,8 +69,9 @@ public class MinioFileHandler implements FileHandler {
.setBasePath(endpoint + bucketName) .setBasePath(endpoint + bucketName)
.setSubPath(source) .setSubPath(source)
.setAutomaticRename(true) .setAutomaticRename(true)
.setRenamePredicate(builder -> .setRenamePredicate(relativePath ->
attachmentRepository.countByPath(builder.getFullPath()) > 0) attachmentRepository
.countByFileKeyAndType(relativePath, AttachmentType.MINIO) > 0)
.setOriginalName(file.getOriginalFilename()) .setOriginalName(file.getOriginalFilename())
.build(); .build();

View File

@ -32,7 +32,6 @@ import run.halo.app.model.properties.QiniuOssProperties;
import run.halo.app.model.support.UploadResult; import run.halo.app.model.support.UploadResult;
import run.halo.app.repository.AttachmentRepository; 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;
import run.halo.app.utils.JsonUtils; import run.halo.app.utils.JsonUtils;
@ -105,8 +104,9 @@ public class QiniuOssFileHandler implements FileHandler {
.setBasePath(basePath.toString()) .setBasePath(basePath.toString())
.setSubPath(source) .setSubPath(source)
.setAutomaticRename(true) .setAutomaticRename(true)
.setRenamePredicate(builder -> .setRenamePredicate(relativePath ->
attachmentRepository.countByPath(builder.getFullPath()) > 0) attachmentRepository
.countByFileKeyAndType(relativePath, AttachmentType.QINIUOSS) > 0)
.setOriginalName(file.getOriginalFilename()) .setOriginalName(file.getOriginalFilename())
.build(); .build();

View File

@ -94,8 +94,9 @@ public class TencentCosFileHandler implements FileHandler {
.setBasePath(basePath.toString()) .setBasePath(basePath.toString())
.setSubPath(source) .setSubPath(source)
.setAutomaticRename(true) .setAutomaticRename(true)
.setRenamePredicate(builder -> .setRenamePredicate(relativePath ->
attachmentRepository.countByPath(builder.getFullPath()) > 0) attachmentRepository
.countByFileKeyAndType(relativePath, AttachmentType.TENCENTCOS) > 0)
.setOriginalName(file.getOriginalFilename()) .setOriginalName(file.getOriginalFilename())
.build(); .build();

View File

@ -107,6 +107,7 @@ public class UpOssFileHandler implements FileHandler {
filePath + thumbnailStyleRule; filePath + thumbnailStyleRule;
} }
}); });
result.close();
return uploadResult; return uploadResult;
} catch (Exception e) { } catch (Exception e) {
throw new FileOperationException("上传附件 " + file.getOriginalFilename() + " 到又拍云失败", e); throw new FileOperationException("上传附件 " + file.getOriginalFilename() + " 到又拍云失败", e);

View File

@ -41,4 +41,13 @@ public interface AttachmentRepository
* @return count of the given path * @return count of the given path
*/ */
long countByPath(@NonNull String path); long countByPath(@NonNull String path);
/**
* Counts by attachment file key and type.
*
* @param fileKey attachment file key must not be blank
* @param type attachment type must not be null
* @return count of the given path and type
*/
long countByFileKeyAndType(@NonNull String fileKey, @NonNull AttachmentType type);
} }

View File

@ -45,12 +45,12 @@ public class FilePathDescriptorTest {
@Test @Test
public void autoRenameWithPredicate() { public void autoRenameWithPredicate() {
FilePathDescriptor descriptor1 = descriptorBuilder.setAutomaticRename(true) FilePathDescriptor descriptor1 = descriptorBuilder.setAutomaticRename(true)
.setRenamePredicate(builder -> false).build(); .setRenamePredicate(path -> false).build();
assertEquals("/home/halo/2021/10/hello.jpg", descriptor1.getFullPath()); assertEquals("/home/halo/2021/10/hello.jpg", descriptor1.getFullPath());
assertEquals("2021/10/hello.jpg", descriptor1.getRelativePath()); assertEquals("2021/10/hello.jpg", descriptor1.getRelativePath());
FilePathDescriptor descriptor2 = descriptorBuilder.setAutomaticRename(true) FilePathDescriptor descriptor2 = descriptorBuilder.setAutomaticRename(true)
.setRenamePredicate(builder -> true).build(); .setRenamePredicate(path -> true).build();
assertNotEquals("/home/halo/2021/10/hello.jpg", descriptor2.getFullPath()); assertNotEquals("/home/halo/2021/10/hello.jpg", descriptor2.getFullPath());
assertNotEquals("2021/10/hello.jpg", descriptor2.getRelativePath()); assertNotEquals("2021/10/hello.jpg", descriptor2.getRelativePath());
} }