chore: update version and remove isDev flag (#1452)

* chore: update version and remove isDev flag

* chore: update
This commit is contained in:
boojack 2023-04-03 14:13:22 +08:00 committed by GitHub
parent 1cab30f32f
commit 4419b4d4ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 49 additions and 62 deletions

View File

@ -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 (

View File

@ -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")

View File

@ -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)

View File

@ -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)

View File

@ -51,7 +51,7 @@ func (s *Server) registerSystemRoutes(g *echo.Group) {
Appearance: "system",
ExternalURL: "",
},
StorageServiceID: 0,
StorageServiceID: api.DatabaseStorage,
LocalStoragePath: "",
}

View File

@ -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"

View File

@ -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

View File

@ -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)
}

View File

@ -48,11 +48,10 @@ const UpdateLocalStorageDialog: React.FC<Props> = (props: Props) => {
<div className="dialog-content-container">
<div className="py-2">
<Typography className="!mb-1" level="body2">
Local Path
</Typography>
<Typography className="!mb-1" level="body2">
<span className="text-sm text-gray-400 ml-1">{"e.g., {year}/{month}/{day}/your/path/{timestamp}_{filename}"}</span>
Local path
</Typography>
<p className="text-sm text-gray-400 break-all">{"It's a relative path to your database file."}</p>
<p className="text-sm text-gray-400 mb-2 break-all">{"e.g. assets/{timestamp}_{filename}"}</p>
<Input className="mb-2" placeholder="Path" value={path} onChange={(e) => setPath(e.target.value)} fullWidth />
</div>
<div className="mt-2 w-full flex flex-row justify-end items-center space-x-1">