mirror of
				https://github.com/usememos/memos.git
				synced 2025-10-31 08:46:39 +08:00 
			
		
		
		
	chore: update resource internal path migrator
This commit is contained in:
		
							parent
							
								
									0f8bfb6328
								
							
						
					
					
						commit
						c797099950
					
				
					 6 changed files with 51 additions and 58 deletions
				
			
		|  | @ -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, | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		
		Reference in a new issue