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 {
@@ -37,10 +40,6 @@ func (c *cHandlers) CollectionMoveFilesIntoAnotherCollection(cmd *stoservertypes
3740 return err
3841 }
3942
40- if cmd .Source == cmd .Destination {
41- return errors .New ("Source and destination cannot be same" )
42- }
43-
4443 sourceState , err := stateresolver .ComputeStateAtHead (* collSrc )
4544 if err != nil {
4645 return err
@@ -51,7 +50,12 @@ func (c *cHandlers) CollectionMoveFilesIntoAnotherCollection(cmd *stoservertypes
5150
5251 filesInSource := sourceState .Files ()
5352
54- for _ , filePathToMove := range filesToMove {
53+ relatedFiles := discoverRelatedFilePaths (* cmd .Files , filesInSource )
54+
55+ // now includes thumbnails and such
56+ filesAndRelatedFilesToMove := append (* cmd .Files , relatedFiles ... )
57+
58+ for _ , filePathToMove := range filesAndRelatedFilesToMove {
5559 fileToMove , found := filesInSource [filePathToMove ]
5660 if ! found {
5761 return fmt .Errorf ("file to move not found: %s" , filePathToMove )
@@ -228,3 +232,22 @@ func noDuplicates(items []string) error {
228232
229233 return nil
230234}
235+
236+ // related files = thumbnails and metadata content belonging to a primary file
237+ func discoverRelatedFilePaths (filesForToDiscover []string , collectionAllFiles map [string ]stotypes.File ) []string {
238+ relatedFiles := []string {}
239+
240+ for _ , item := range filesForToDiscover {
241+ file , found := collectionAllFiles [item ]
242+ if ! found { // should not happen, but let's ignore here since this error is caught in later code
243+ continue
244+ }
245+
246+ thumbPath := stomediascanner .CollectionThumbPath (file )
247+ if _ , haveThumb := collectionAllFiles [thumbPath ]; haveThumb {
248+ relatedFiles = append (relatedFiles , thumbPath )
249+ }
250+ }
251+
252+ return relatedFiles
253+ }
0 commit comments