mirror of
https://github.com/usememos/memos.git
synced 2025-02-25 05:45:00 +08:00
feat: introduce publicid to filename template (#1713)
* Add support for `publicid` in PathTemplate * Use `publicid` by default instead of `filename` in filesystem * Fix blank string of `systemSettingLocalStoragePath` affect incorrectly * Add ext name to compatible with OS's preview * Optimize code for systemSettingLocalStoragePath empty --------- Co-authored-by: Athurg Feng <athurg@gooth.org>
This commit is contained in:
parent
d24632682f
commit
616b8b0ee6
2 changed files with 15 additions and 12 deletions
|
@ -142,19 +142,20 @@ func (s *Server) registerResourceRoutes(g *echo.Group) {
|
|||
if err != nil && common.ErrorCode(err) != common.NotFound {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find local storage path setting").SetInternal(err)
|
||||
}
|
||||
localStoragePath := "assets/{timestamp}_{filename}"
|
||||
if systemSettingLocalStoragePath != nil {
|
||||
localStoragePath := "assets/{publicid}"
|
||||
if systemSettingLocalStoragePath != nil && systemSettingLocalStoragePath.Value != "" {
|
||||
err = json.Unmarshal([]byte(systemSettingLocalStoragePath.Value), &localStoragePath)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to unmarshal local storage path setting").SetInternal(err)
|
||||
}
|
||||
}
|
||||
filePath := filepath.FromSlash(localStoragePath)
|
||||
if !strings.Contains(filePath, "{filename}") {
|
||||
filePath = filepath.Join(filePath, "{filename}")
|
||||
if !strings.Contains(filePath, "{publicid}") {
|
||||
filePath = filepath.Join(filePath, "{publicid}")
|
||||
}
|
||||
filePath = filepath.Join(s.Profile.Data, replacePathTemplate(filePath, file.Filename))
|
||||
dir, filename := filepath.Split(filePath)
|
||||
filePath = filepath.Join(s.Profile.Data, replacePathTemplate(filePath, file.Filename, publicID+filepath.Ext(file.Filename)))
|
||||
|
||||
dir := filepath.Dir(filePath)
|
||||
if err = os.MkdirAll(dir, os.ModePerm); err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create directory").SetInternal(err)
|
||||
}
|
||||
|
@ -170,7 +171,7 @@ func (s *Server) registerResourceRoutes(g *echo.Group) {
|
|||
|
||||
resourceCreate = &api.ResourceCreate{
|
||||
CreatorID: userID,
|
||||
Filename: filename,
|
||||
Filename: file.Filename,
|
||||
Type: filetype,
|
||||
Size: size,
|
||||
InternalPath: filePath,
|
||||
|
@ -197,10 +198,10 @@ func (s *Server) registerResourceRoutes(g *echo.Group) {
|
|||
}
|
||||
|
||||
filePath := s3Config.Path
|
||||
if !strings.Contains(filePath, "{filename}") {
|
||||
filePath = path.Join(filePath, "{filename}")
|
||||
if !strings.Contains(filePath, "{publicid}") {
|
||||
filePath = path.Join(filePath, "{publicid}")
|
||||
}
|
||||
filePath = replacePathTemplate(filePath, file.Filename)
|
||||
filePath = replacePathTemplate(filePath, file.Filename, publicID+filepath.Ext(file.Filename))
|
||||
_, filename := filepath.Split(filePath)
|
||||
link, err := s3Client.UploadFile(ctx, filePath, filetype, sourceFile)
|
||||
if err != nil {
|
||||
|
@ -476,10 +477,12 @@ func (s *Server) createResourceCreateActivity(c echo.Context, resource *api.Reso
|
|||
return err
|
||||
}
|
||||
|
||||
func replacePathTemplate(path string, filename string) string {
|
||||
func replacePathTemplate(path, filename, publicID string) string {
|
||||
t := time.Now()
|
||||
path = fileKeyPattern.ReplaceAllStringFunc(path, func(s string) string {
|
||||
switch s {
|
||||
case "{publicid}":
|
||||
return publicID
|
||||
case "{filename}":
|
||||
return filename
|
||||
case "{timestamp}":
|
||||
|
|
|
@ -52,7 +52,7 @@ const UpdateLocalStorageDialog: React.FC<Props> = (props: Props) => {
|
|||
<p className="text-sm break-words mb-1">{t("setting.storage-section.update-local-path-description")}</p>
|
||||
<div className="flex flex-row">
|
||||
<p className="text-sm text-gray-400 mb-2 break-all">
|
||||
{t("common.e.g")} {"assets/{timestamp}_{filename}"}
|
||||
{t("common.e.g")} {"assets/{publicid}"}
|
||||
</p>
|
||||
<HelpButton hint={t("common.learn-more")} url="https://usememos.com/docs/local-storage" />
|
||||
</div>
|
||||
|
|
Loading…
Reference in a new issue