From 4419b4d4ae8c42cd5bc98351f33fb8e630d60b94 Mon Sep 17 00:00:00 2001 From: boojack Date: Mon, 3 Apr 2023 14:13:22 +0800 Subject: [PATCH] chore: update version and remove isDev flag (#1452) * chore: update version and remove isDev flag * chore: update --- api/storage.go | 7 +++ api/system_setting.go | 2 +- server/resource.go | 31 +++++------ server/storage.go | 2 +- server/system.go | 2 +- server/version/version.go | 2 +- store/db/migration/prod/LATEST__SCHEMA.sql | 5 +- store/resource.go | 53 +++++++------------ .../components/UpdateLocalStorageDialog.tsx | 7 ++- 9 files changed, 49 insertions(+), 62 deletions(-) diff --git a/api/storage.go b/api/storage.go index b538c9563..5d646fada 100644 --- a/api/storage.go +++ b/api/storage.go @@ -1,5 +1,12 @@ package api +const ( + // LocalStorage means the storage service is local file system. + LocalStorage = -1 + // DatabaseStorage means the storage service is database. + DatabaseStorage = 0 +) + type StorageType string const ( diff --git a/api/system_setting.go b/api/system_setting.go index bea12fe25..7f61281f3 100644 --- a/api/system_setting.go +++ b/api/system_setting.go @@ -140,7 +140,7 @@ func (upsert SystemSettingUpsert) Validate() error { return fmt.Errorf("invalid appearance value") } } else if upsert.Name == SystemSettingStorageServiceIDName { - value := 0 + value := DatabaseStorage err := json.Unmarshal([]byte(upsert.Value), &value) if err != nil { return fmt.Errorf("failed to unmarshal system setting storage service id value") diff --git a/server/resource.go b/server/resource.go index 3d52c9e53..78fb1ba9a 100644 --- a/server/resource.go +++ b/server/resource.go @@ -80,17 +80,17 @@ func (s *Server) registerResourceRoutes(g *echo.Group) { filename := file.Filename filetype := file.Header.Get("Content-Type") size := file.Size - src, err := file.Open() + sourceFile, err := file.Open() if err != nil { return echo.NewHTTPError(http.StatusInternalServerError, "Failed to open file").SetInternal(err) } - defer src.Close() + defer sourceFile.Close() systemSettingStorageServiceID, err := s.Store.FindSystemSetting(ctx, &api.SystemSettingFind{Name: api.SystemSettingStorageServiceIDName}) if err != nil && common.ErrorCode(err) != common.NotFound { return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find storage").SetInternal(err) } - storageServiceID := 0 + storageServiceID := api.DatabaseStorage if systemSettingStorageServiceID != nil { err = json.Unmarshal([]byte(systemSettingStorageServiceID.Value), &storageServiceID) if err != nil { @@ -99,9 +99,9 @@ func (s *Server) registerResourceRoutes(g *echo.Group) { } var resourceCreate *api.ResourceCreate - if storageServiceID == 0 { + if storageServiceID == api.DatabaseStorage { // Database storage. - fileBytes, err := io.ReadAll(src) + fileBytes, err := io.ReadAll(sourceFile) if err != nil { return echo.NewHTTPError(http.StatusInternalServerError, "Failed to read file").SetInternal(err) } @@ -112,17 +112,17 @@ func (s *Server) registerResourceRoutes(g *echo.Group) { Size: size, Blob: fileBytes, } - } else if storageServiceID == -1 { + } else if storageServiceID == api.LocalStorage { // Local storage. systemSettingLocalStoragePath, err := s.Store.FindSystemSetting(ctx, &api.SystemSettingFind{Name: api.SystemSettingLocalStoragePathName}) if err != nil && common.ErrorCode(err) != common.NotFound { - return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find storage").SetInternal(err) + return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find local storage path setting").SetInternal(err) } localStoragePath := "" if systemSettingLocalStoragePath != nil { err = json.Unmarshal([]byte(systemSettingLocalStoragePath.Value), &localStoragePath) if err != nil { - return echo.NewHTTPError(http.StatusInternalServerError, "Failed to unmarshal storage service id").SetInternal(err) + return echo.NewHTTPError(http.StatusInternalServerError, "Failed to unmarshal local storage path setting").SetInternal(err) } } filePath := localStoragePath @@ -131,8 +131,7 @@ func (s *Server) registerResourceRoutes(g *echo.Group) { } filePath = path.Join(s.Profile.Data, replacePathTemplate(filePath, filename)) dirPath := filepath.Dir(filePath) - err = os.MkdirAll(dirPath, os.ModePerm) - if err != nil { + if err = os.MkdirAll(dirPath, os.ModePerm); err != nil { return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create directory").SetInternal(err) } dst, err := os.Create(filePath) @@ -140,8 +139,7 @@ func (s *Server) registerResourceRoutes(g *echo.Group) { return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create file").SetInternal(err) } defer dst.Close() - - _, err = io.Copy(dst, src) + _, err = io.Copy(dst, sourceFile) if err != nil { return echo.NewHTTPError(http.StatusInternalServerError, "Failed to copy file").SetInternal(err) } @@ -179,7 +177,7 @@ func (s *Server) registerResourceRoutes(g *echo.Group) { return echo.NewHTTPError(http.StatusInternalServerError, "Failed to new s3 client").SetInternal(err) } - link, err := s3client.UploadFile(ctx, s3FileKey, filetype, src) + link, err := s3client.UploadFile(ctx, s3FileKey, filetype, sourceFile) if err != nil { return echo.NewHTTPError(http.StatusInternalServerError, "Failed to upload via s3 client").SetInternal(err) } @@ -194,11 +192,8 @@ func (s *Server) registerResourceRoutes(g *echo.Group) { } } - if s.Profile.IsDev() { - publicID := common.GenUUID() - resourceCreate.PublicID = publicID - } - + publicID := common.GenUUID() + resourceCreate.PublicID = publicID resource, err := s.Store.CreateResource(ctx, resourceCreate) if err != nil { return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create resource").SetInternal(err) diff --git a/server/storage.go b/server/storage.go index e3afedbab..b58080cc1 100644 --- a/server/storage.go +++ b/server/storage.go @@ -129,7 +129,7 @@ func (s *Server) registerStorageRoutes(g *echo.Group) { return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find storage").SetInternal(err) } if systemSetting != nil { - storageServiceID := 0 + storageServiceID := api.DatabaseStorage err = json.Unmarshal([]byte(systemSetting.Value), &storageServiceID) if err != nil { return echo.NewHTTPError(http.StatusInternalServerError, "Failed to unmarshal storage service id").SetInternal(err) diff --git a/server/system.go b/server/system.go index 0db7aaecb..0ec1824c1 100644 --- a/server/system.go +++ b/server/system.go @@ -51,7 +51,7 @@ func (s *Server) registerSystemRoutes(g *echo.Group) { Appearance: "system", ExternalURL: "", }, - StorageServiceID: 0, + StorageServiceID: api.DatabaseStorage, LocalStoragePath: "", } diff --git a/server/version/version.go b/server/version/version.go index bf27cabfc..734ac97c5 100644 --- a/server/version/version.go +++ b/server/version/version.go @@ -9,7 +9,7 @@ import ( // Version is the service current released version. // Semantic versioning: https://semver.org/ -var Version = "0.11.2" +var Version = "0.12.0" // DevVersion is the service current development version. var DevVersion = "0.12.0" diff --git a/store/db/migration/prod/LATEST__SCHEMA.sql b/store/db/migration/prod/LATEST__SCHEMA.sql index b2448a1e8..a5b3cfac0 100644 --- a/store/db/migration/prod/LATEST__SCHEMA.sql +++ b/store/db/migration/prod/LATEST__SCHEMA.sql @@ -76,7 +76,10 @@ CREATE TABLE resource ( blob BLOB DEFAULT NULL, external_link TEXT NOT NULL DEFAULT '', type TEXT NOT NULL DEFAULT '', - size INTEGER NOT NULL DEFAULT 0 + size INTEGER NOT NULL DEFAULT 0, + internal_path TEXT NOT NULL DEFAULT '', + public_id TEXT NOT NULL DEFAULT '', + UNIQUE(id, public_id) ); -- memo_resource diff --git a/store/resource.go b/store/resource.go index 9266b086b..c12fb8764 100644 --- a/store/resource.go +++ b/store/resource.go @@ -94,7 +94,7 @@ func (s *Store) CreateResource(ctx context.Context, create *api.ResourceCreate) } defer tx.Rollback() - resourceRaw, err := s.createResourceImpl(ctx, tx, create) + resourceRaw, err := createResourceImpl(ctx, tx, create) if err != nil { return nil, err } @@ -115,7 +115,7 @@ func (s *Store) FindResourceList(ctx context.Context, find *api.ResourceFind) ([ } defer tx.Rollback() - resourceRawList, err := s.findResourceListImpl(ctx, tx, find) + resourceRawList, err := findResourceListImpl(ctx, tx, find) if err != nil { return nil, err } @@ -135,7 +135,7 @@ func (s *Store) FindResource(ctx context.Context, find *api.ResourceFind) (*api. } defer tx.Rollback() - list, err := s.findResourceListImpl(ctx, tx, find) + list, err := findResourceListImpl(ctx, tx, find) if err != nil { return nil, err } @@ -178,7 +178,7 @@ func (s *Store) PatchResource(ctx context.Context, patch *api.ResourcePatch) (*a } defer tx.Rollback() - resourceRaw, err := s.patchResourceImpl(ctx, tx, patch) + resourceRaw, err := patchResourceImpl(ctx, tx, patch) if err != nil { return nil, err } @@ -192,16 +192,10 @@ func (s *Store) PatchResource(ctx context.Context, patch *api.ResourcePatch) (*a return resource, nil } -func (s *Store) createResourceImpl(ctx context.Context, tx *sql.Tx, create *api.ResourceCreate) (*resourceRaw, error) { - fields := []string{"filename", "blob", "external_link", "type", "size", "creator_id"} - values := []any{create.Filename, create.Blob, create.ExternalLink, create.Type, create.Size, create.CreatorID} - placeholders := []string{"?", "?", "?", "?", "?", "?"} - if s.profile.IsDev() { - fields = append(fields, "internal_path", "public_id") - values = append(values, create.InternalPath, create.PublicID) - placeholders = append(placeholders, "?", "?") - } - +func createResourceImpl(ctx context.Context, tx *sql.Tx, create *api.ResourceCreate) (*resourceRaw, error) { + fields := []string{"filename", "blob", "external_link", "type", "size", "creator_id", "internal_path", "public_id"} + values := []any{create.Filename, create.Blob, create.ExternalLink, create.Type, create.Size, create.CreatorID, create.InternalPath, create.PublicID} + placeholders := []string{"?", "?", "?", "?", "?", "?", "?", "?"} query := ` INSERT INTO resource ( ` + strings.Join(fields, ",") + ` @@ -218,9 +212,8 @@ func (s *Store) createResourceImpl(ctx context.Context, tx *sql.Tx, create *api. &resourceRaw.Type, &resourceRaw.Size, &resourceRaw.CreatorID, - } - if s.profile.IsDev() { - dests = append(dests, &resourceRaw.InternalPath, &resourceRaw.PublicID) + &resourceRaw.InternalPath, + &resourceRaw.PublicID, } dests = append(dests, []any{&resourceRaw.CreatedTs, &resourceRaw.UpdatedTs}...) if err := tx.QueryRowContext(ctx, query, values...).Scan(dests...); err != nil { @@ -230,7 +223,7 @@ func (s *Store) createResourceImpl(ctx context.Context, tx *sql.Tx, create *api. return &resourceRaw, nil } -func (s *Store) patchResourceImpl(ctx context.Context, tx *sql.Tx, patch *api.ResourcePatch) (*resourceRaw, error) { +func patchResourceImpl(ctx context.Context, tx *sql.Tx, patch *api.ResourcePatch) (*resourceRaw, error) { set, args := []string{}, []any{} if v := patch.UpdatedTs; v != nil { @@ -244,12 +237,7 @@ func (s *Store) patchResourceImpl(ctx context.Context, tx *sql.Tx, patch *api.Re } args = append(args, patch.ID) - - fields := []string{"id", "filename", "external_link", "type", "size", "creator_id", "created_ts", "updated_ts"} - if s.profile.IsDev() { - fields = append(fields, "internal_path", "public_id") - } - + fields := []string{"id", "filename", "external_link", "type", "size", "creator_id", "created_ts", "updated_ts", "internal_path", "public_id"} query := ` UPDATE resource SET ` + strings.Join(set, ", ") + ` @@ -265,9 +253,8 @@ func (s *Store) patchResourceImpl(ctx context.Context, tx *sql.Tx, patch *api.Re &resourceRaw.CreatorID, &resourceRaw.CreatedTs, &resourceRaw.UpdatedTs, - } - if s.profile.IsDev() { - dests = append(dests, &resourceRaw.InternalPath, &resourceRaw.PublicID) + &resourceRaw.InternalPath, + &resourceRaw.PublicID, } if err := tx.QueryRowContext(ctx, query, args...).Scan(dests...); err != nil { return nil, FormatError(err) @@ -276,7 +263,7 @@ func (s *Store) patchResourceImpl(ctx context.Context, tx *sql.Tx, patch *api.Re return &resourceRaw, nil } -func (s *Store) findResourceListImpl(ctx context.Context, tx *sql.Tx, find *api.ResourceFind) ([]*resourceRaw, error) { +func findResourceListImpl(ctx context.Context, tx *sql.Tx, find *api.ResourceFind) ([]*resourceRaw, error) { where, args := []string{"1 = 1"}, []any{} if v := find.ID; v != nil { @@ -295,13 +282,10 @@ func (s *Store) findResourceListImpl(ctx context.Context, tx *sql.Tx, find *api. where, args = append(where, "resource.public_id = ?"), append(args, *v) } - fields := []string{"resource.id", "resource.filename", "resource.external_link", "resource.type", "resource.size", "resource.creator_id", "resource.created_ts", "resource.updated_ts"} + fields := []string{"resource.id", "resource.filename", "resource.external_link", "resource.type", "resource.size", "resource.creator_id", "resource.created_ts", "resource.updated_ts", "internal_path", "public_id"} if find.GetBlob { fields = append(fields, "resource.blob") } - if s.profile.IsDev() { - fields = append(fields, "internal_path", "public_id") - } query := fmt.Sprintf(` SELECT @@ -339,13 +323,12 @@ func (s *Store) findResourceListImpl(ctx context.Context, tx *sql.Tx, find *api. &resourceRaw.CreatorID, &resourceRaw.CreatedTs, &resourceRaw.UpdatedTs, + &resourceRaw.InternalPath, + &resourceRaw.PublicID, } if find.GetBlob { dests = append(dests, &resourceRaw.Blob) } - if s.profile.IsDev() { - dests = append(dests, &resourceRaw.InternalPath, &resourceRaw.PublicID) - } if err := rows.Scan(dests...); err != nil { return nil, FormatError(err) } diff --git a/web/src/components/UpdateLocalStorageDialog.tsx b/web/src/components/UpdateLocalStorageDialog.tsx index 1a6e89763..880776975 100644 --- a/web/src/components/UpdateLocalStorageDialog.tsx +++ b/web/src/components/UpdateLocalStorageDialog.tsx @@ -48,11 +48,10 @@ const UpdateLocalStorageDialog: React.FC = (props: Props) => {
- Local Path - - - {"e.g., {year}/{month}/{day}/your/path/{timestamp}_{filename}"} + Local path +

{"It's a relative path to your database file."}

+

{"e.g. assets/{timestamp}_{filename}"}

setPath(e.target.value)} fullWidth />