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>
pull/1723/head
Athurg Gooth 2 years ago committed by GitHub
parent d24632682f
commit 616b8b0ee6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -142,19 +142,20 @@ func (s *Server) registerResourceRoutes(g *echo.Group) {
if err != nil && common.ErrorCode(err) != common.NotFound { if err != nil && common.ErrorCode(err) != common.NotFound {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find local storage path setting").SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find local storage path setting").SetInternal(err)
} }
localStoragePath := "assets/{timestamp}_{filename}" localStoragePath := "assets/{publicid}"
if systemSettingLocalStoragePath != nil { if systemSettingLocalStoragePath != nil && systemSettingLocalStoragePath.Value != "" {
err = json.Unmarshal([]byte(systemSettingLocalStoragePath.Value), &localStoragePath) err = json.Unmarshal([]byte(systemSettingLocalStoragePath.Value), &localStoragePath)
if err != nil { if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to unmarshal local storage path setting").SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, "Failed to unmarshal local storage path setting").SetInternal(err)
} }
} }
filePath := filepath.FromSlash(localStoragePath) filePath := filepath.FromSlash(localStoragePath)
if !strings.Contains(filePath, "{filename}") { if !strings.Contains(filePath, "{publicid}") {
filePath = filepath.Join(filePath, "{filename}") filePath = filepath.Join(filePath, "{publicid}")
} }
filePath = filepath.Join(s.Profile.Data, replacePathTemplate(filePath, file.Filename)) filePath = filepath.Join(s.Profile.Data, replacePathTemplate(filePath, file.Filename, publicID+filepath.Ext(file.Filename)))
dir, filename := filepath.Split(filePath)
dir := filepath.Dir(filePath)
if err = os.MkdirAll(dir, os.ModePerm); err != nil { if err = os.MkdirAll(dir, os.ModePerm); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create directory").SetInternal(err) 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{ resourceCreate = &api.ResourceCreate{
CreatorID: userID, CreatorID: userID,
Filename: filename, Filename: file.Filename,
Type: filetype, Type: filetype,
Size: size, Size: size,
InternalPath: filePath, InternalPath: filePath,
@ -197,10 +198,10 @@ func (s *Server) registerResourceRoutes(g *echo.Group) {
} }
filePath := s3Config.Path filePath := s3Config.Path
if !strings.Contains(filePath, "{filename}") { if !strings.Contains(filePath, "{publicid}") {
filePath = path.Join(filePath, "{filename}") filePath = path.Join(filePath, "{publicid}")
} }
filePath = replacePathTemplate(filePath, file.Filename) filePath = replacePathTemplate(filePath, file.Filename, publicID+filepath.Ext(file.Filename))
_, filename := filepath.Split(filePath) _, filename := filepath.Split(filePath)
link, err := s3Client.UploadFile(ctx, filePath, filetype, sourceFile) link, err := s3Client.UploadFile(ctx, filePath, filetype, sourceFile)
if err != nil { if err != nil {
@ -476,10 +477,12 @@ func (s *Server) createResourceCreateActivity(c echo.Context, resource *api.Reso
return err return err
} }
func replacePathTemplate(path string, filename string) string { func replacePathTemplate(path, filename, publicID string) string {
t := time.Now() t := time.Now()
path = fileKeyPattern.ReplaceAllStringFunc(path, func(s string) string { path = fileKeyPattern.ReplaceAllStringFunc(path, func(s string) string {
switch s { switch s {
case "{publicid}":
return publicID
case "{filename}": case "{filename}":
return filename return filename
case "{timestamp}": 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> <p className="text-sm break-words mb-1">{t("setting.storage-section.update-local-path-description")}</p>
<div className="flex flex-row"> <div className="flex flex-row">
<p className="text-sm text-gray-400 mb-2 break-all"> <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> </p>
<HelpButton hint={t("common.learn-more")} url="https://usememos.com/docs/local-storage" /> <HelpButton hint={t("common.learn-more")} url="https://usememos.com/docs/local-storage" />
</div> </div>

Loading…
Cancel
Save