-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: auto rename attachment if it exists (#22)
Fixes halo-dev/halo#3337 不更新依赖了,直接复制了FileNameUtils 在有image.png的情况下再同时粘贴两张截图,期望两张都能被上传且被自动重命名。 ![image](https://user-images.githubusercontent.com/28662535/220059741-da25a490-6f6a-4172-a393-aa3f84ab6b38.png) ![image](https://user-images.githubusercontent.com/28662535/220059786-24cda2bb-6faa-4377-8eb8-a70920916f3d.png) ```release-note 文件存在时自动重命名 ```
- Loading branch information
Showing
2 changed files
with
195 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package run.halo.s3os; | ||
|
||
import com.google.common.io.Files; | ||
import org.apache.commons.lang3.RandomStringUtils; | ||
import org.apache.commons.lang3.StringUtils; | ||
|
||
public final class FileNameUtils { | ||
|
||
private FileNameUtils() { | ||
} | ||
|
||
public static String removeFileExtension(String filename, boolean removeAllExtensions) { | ||
if (filename == null || filename.isEmpty()) { | ||
return filename; | ||
} | ||
var extPattern = "(?<!^)[.]" + (removeAllExtensions ? ".*" : "[^.]*$"); | ||
return filename.replaceAll(extPattern, ""); | ||
} | ||
|
||
/** | ||
* Append random string after file name. | ||
* <pre> | ||
* Case 1: halo.run -> halo-xyz.run | ||
* Case 2: .run -> xyz.run | ||
* Case 3: halo -> halo-xyz | ||
* </pre> | ||
* | ||
* @param filename is name of file. | ||
* @param length is for generating random string with specific length. | ||
* @return File name with random string. | ||
*/ | ||
public static String randomFileName(String filename, int length) { | ||
var nameWithoutExt = Files.getNameWithoutExtension(filename); | ||
var ext = Files.getFileExtension(filename); | ||
var random = RandomStringUtils.randomAlphabetic(length).toLowerCase(); | ||
if (StringUtils.isBlank(nameWithoutExt)) { | ||
return random + "." + ext; | ||
} | ||
if (StringUtils.isBlank(ext)) { | ||
return nameWithoutExt + "-" + random; | ||
} | ||
return nameWithoutExt + "-" + random + "." + ext; | ||
} | ||
} |
Oops, something went wrong.