88
99 "github.com/function61/eventkit/command"
1010 "github.com/function61/varasto/pkg/stateresolver"
11+ "github.com/function61/varasto/pkg/stomediascanner"
1112 "github.com/function61/varasto/pkg/stoserver/stodb"
1213 "github.com/function61/varasto/pkg/stoserver/stoservertypes"
1314 "github.com/function61/varasto/pkg/stotypes"
@@ -16,16 +17,18 @@ import (
1617)
1718
1819func (c * cHandlers ) CollectionMoveFilesIntoAnotherCollection (cmd * stoservertypes.CollectionMoveFilesIntoAnotherCollection , ctx * command.Ctx ) error {
19- filesToMove := * cmd .Files
20-
21- if len (filesToMove ) == 0 {
20+ if len (* cmd .Files ) == 0 {
2221 return nil // no-op
2322 }
2423
25- if err := noDuplicates (filesToMove ); err != nil {
24+ if err := noDuplicates (* cmd . Files ); err != nil {
2625 return err
2726 }
2827
28+ if cmd .Source == cmd .Destination {
29+ return errors .New ("source and destination cannot be same" )
30+ }
31+
2932 return c .db .Update (func (tx * bbolt.Tx ) error {
3033 collSrc , err := stodb .Read (tx ).Collection (cmd .Source )
3134 if err != nil {
@@ -51,7 +54,12 @@ func (c *cHandlers) CollectionMoveFilesIntoAnotherCollection(cmd *stoservertypes
5154
5255 filesInSource := sourceState .Files ()
5356
54- for _ , filePathToMove := range filesToMove {
57+ relatedFiles := discoverRelatedFilePaths (* cmd .Files , filesInSource )
58+
59+ // now includes thumbnails and such
60+ filesAndRelatedFilesToMove := append (* cmd .Files , relatedFiles ... )
61+
62+ for _ , filePathToMove := range filesAndRelatedFilesToMove {
5563 fileToMove , found := filesInSource [filePathToMove ]
5664 if ! found {
5765 return fmt .Errorf ("file to move not found: %s" , filePathToMove )
@@ -149,7 +157,12 @@ func (c *cHandlers) CollectionDeleteFiles(cmd *stoservertypes.CollectionDeleteFi
149157
150158 existingFiles := stateForValidation .Files ()
151159
152- for _ , fileToDelete := range filesToDelete {
160+ // delete related thumbnails etc. as well
161+ allFilesToDelete := []string {}
162+ allFilesToDelete = append (allFilesToDelete , filesToDelete ... )
163+ allFilesToDelete = append (allFilesToDelete , discoverRelatedFilePaths (filesToDelete , existingFiles )... )
164+
165+ for _ , fileToDelete := range allFilesToDelete {
153166 if _ , has := existingFiles [fileToDelete ]; ! has {
154167 return fmt .Errorf ("file to delete does not exist: %s" , fileToDelete )
155168 }
@@ -161,7 +174,7 @@ func (c *cHandlers) CollectionDeleteFiles(cmd *stoservertypes.CollectionDeleteFi
161174 ctx .Meta .Timestamp ,
162175 nil ,
163176 nil ,
164- filesToDelete )
177+ allFilesToDelete )
165178
166179 if err := appendAndValidateChangeset (changeset , coll ); err != nil {
167180 return err
@@ -228,3 +241,22 @@ func noDuplicates(items []string) error {
228241
229242 return nil
230243}
244+
245+ // related files = thumbnails and metadata content belonging to a primary file
246+ func discoverRelatedFilePaths (filesForToDiscover []string , collectionAllFiles map [string ]stotypes.File ) []string {
247+ relatedFiles := []string {}
248+
249+ for _ , item := range filesForToDiscover {
250+ file , found := collectionAllFiles [item ]
251+ if ! found { // should not happen, but let's ignore here since this error is caught in later code
252+ continue
253+ }
254+
255+ thumbPath := stomediascanner .CollectionThumbPath (file )
256+ if _ , haveThumb := collectionAllFiles [thumbPath ]; haveThumb {
257+ relatedFiles = append (relatedFiles , thumbPath )
258+ }
259+ }
260+
261+ return relatedFiles
262+ }
0 commit comments