From 616b8b0ee60cb72308caa32a761fc0352582b71d Mon Sep 17 00:00:00 2001 From: Athurg Gooth Date: Tue, 23 May 2023 19:15:30 +0800 Subject: [PATCH] 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 --- server/resource.go | 25 +++++++++++-------- .../components/UpdateLocalStorageDialog.tsx | 2 +- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/server/resource.go b/server/resource.go index 8bf5c800..c506d7f5 100644 --- a/server/resource.go +++ b/server/resource.go @@ -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}": diff --git a/web/src/components/UpdateLocalStorageDialog.tsx b/web/src/components/UpdateLocalStorageDialog.tsx index 4e7aa6ae..acb2d531 100644 --- a/web/src/components/UpdateLocalStorageDialog.tsx +++ b/web/src/components/UpdateLocalStorageDialog.tsx @@ -52,7 +52,7 @@ const UpdateLocalStorageDialog: React.FC = (props: Props) => {

{t("setting.storage-section.update-local-path-description")}

- {t("common.e.g")} {"assets/{timestamp}_{filename}"} + {t("common.e.g")} {"assets/{publicid}"}