mirror of
https://github.com/usememos/memos.git
synced 2025-06-05 22:09:59 +02:00
chore: update resource internal path migrator
This commit is contained in:
@@ -59,6 +59,12 @@ var (
|
|||||||
}
|
}
|
||||||
|
|
||||||
store := store.New(dbDriver, profile)
|
store := store.New(dbDriver, profile)
|
||||||
|
if err := store.MigrateResourceInternalPath(ctx); err != nil {
|
||||||
|
cancel()
|
||||||
|
log.Error("failed to migrate resource internal path", zap.Error(err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
s, err := server.NewServer(ctx, profile, store)
|
s, err := server.NewServer(ctx, profile, store)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cancel()
|
cancel()
|
||||||
|
@@ -1,19 +0,0 @@
|
|||||||
-- Make resource internal_path relative (to MEMOS_DATA) and replace backslash with slash
|
|
||||||
-- This is a best-effort approach, but even if it fails, it won't break assets from loading
|
|
||||||
UPDATE resource
|
|
||||||
SET
|
|
||||||
internal_path = REPLACE (internal_path, '\\', '/')
|
|
||||||
WHERE
|
|
||||||
internal_path LIKE '%assets\\\%';
|
|
||||||
|
|
||||||
UPDATE resource
|
|
||||||
SET
|
|
||||||
internal_path = REPLACE (
|
|
||||||
internal_path,
|
|
||||||
SUBSTR (
|
|
||||||
internal_path,
|
|
||||||
1,
|
|
||||||
INSTR (internal_path, '/assets')
|
|
||||||
),
|
|
||||||
''
|
|
||||||
);
|
|
@@ -1,19 +0,0 @@
|
|||||||
-- Make resource internal_path relative (to MEMOS_DATA) and replace backslash with slash
|
|
||||||
-- This is a best-effort approach, but even if it fails, it won't break assets from loading
|
|
||||||
UPDATE resource
|
|
||||||
SET
|
|
||||||
internal_path = REPLACE (internal_path, '\', '/')
|
|
||||||
WHERE
|
|
||||||
internal_path LIKE '%assets\\%';
|
|
||||||
|
|
||||||
UPDATE resource
|
|
||||||
SET
|
|
||||||
internal_path = REPLACE (
|
|
||||||
internal_path,
|
|
||||||
SUBSTRING(
|
|
||||||
internal_path
|
|
||||||
FROM
|
|
||||||
1 FOR POSITION('/assets' IN internal_path)
|
|
||||||
),
|
|
||||||
''
|
|
||||||
);
|
|
@@ -1,19 +0,0 @@
|
|||||||
-- Make resource internal_path relative (to MEMOS_DATA) and replace backslash with slash
|
|
||||||
-- This is a best-effort approach, but even if it fails, it won't break assets from loading
|
|
||||||
UPDATE resource
|
|
||||||
SET
|
|
||||||
internal_path = REPLACE (internal_path, '\', '/')
|
|
||||||
WHERE
|
|
||||||
internal_path LIKE '%assets\%';
|
|
||||||
|
|
||||||
UPDATE resource
|
|
||||||
SET
|
|
||||||
internal_path = REPLACE (
|
|
||||||
internal_path,
|
|
||||||
SUBSTR (
|
|
||||||
internal_path,
|
|
||||||
1,
|
|
||||||
INSTR (internal_path, '/assets')
|
|
||||||
),
|
|
||||||
''
|
|
||||||
);
|
|
44
store/migrator.go
Normal file
44
store/migrator.go
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
package store
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
// MigrateResourceInternalPath migrates resource internal path from absolute path to relative path.
|
||||||
|
func (s *Store) MigrateResourceInternalPath(ctx context.Context) error {
|
||||||
|
resources, err := s.ListResources(ctx, &FindResource{})
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "failed to list resources")
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, resource := range resources {
|
||||||
|
if resource.InternalPath == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
internalPath := resource.InternalPath
|
||||||
|
if filepath.IsAbs(internalPath) {
|
||||||
|
if !strings.HasPrefix(internalPath, s.Profile.Data) {
|
||||||
|
// Invalid internal path, skip.
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
internalPath = strings.TrimPrefix(internalPath, s.Profile.Data)
|
||||||
|
for strings.HasPrefix(internalPath, "/") {
|
||||||
|
internalPath = strings.TrimPrefix(internalPath, "/")
|
||||||
|
}
|
||||||
|
_, err := s.UpdateResource(ctx, &UpdateResource{
|
||||||
|
ID: resource.ID,
|
||||||
|
InternalPath: &internalPath,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "failed to update resource")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
@@ -20,8 +20,8 @@ type Store struct {
|
|||||||
// New creates a new instance of Store.
|
// New creates a new instance of Store.
|
||||||
func New(driver Driver, profile *profile.Profile) *Store {
|
func New(driver Driver, profile *profile.Profile) *Store {
|
||||||
return &Store{
|
return &Store{
|
||||||
Profile: profile,
|
|
||||||
driver: driver,
|
driver: driver,
|
||||||
|
Profile: profile,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user