Skip to content

Commit b0bacd5

Browse files
committed
collection.MoveFilesIntoAnotherCollection: move related (e.g. thumbs) together
1 parent 6b2a4b3 commit b0bacd5

3 files changed

Lines changed: 38 additions & 13 deletions

File tree

pkg/stateresolver/resolver.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ func (s *StateAt) FileList() []stotypes.File {
3636
files = append(files, file)
3737
}
3838

39+
// since these represent files over multiple directories, the only sorting strategy that makes sense is by path
40+
// (to make files in same directory be closer to each other)
3941
sort.Slice(files, func(i, j int) bool {
4042
return files[i].Path < files[j].Path
4143
})

pkg/stomediascanner/thumbnailer.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ func collectionThumbnailsOneBatch(
150150
deletedFiles = append(deletedFiles, thumb.Path)
151151

152152
// move into thumb path, assume it has sensible dimensions (suitable as a thumbnail)
153-
thumbPath := collectionThumbPath(file)
153+
thumbPath := CollectionThumbPath(file)
154154

155155
// there already exists thumbnail? delete it (by updating)
156156
if toReplace, thumbExists := collFiles[thumbPath]; thumbExists {
@@ -171,7 +171,7 @@ func collectionThumbnailsOneBatch(
171171
}
172172

173173
alreadyThumbnailed := func(file stotypes.File) bool {
174-
thumbPath := collectionThumbPath(file)
174+
thumbPath := CollectionThumbPath(file)
175175

176176
// since thumbPath is based on file content, "foo.jpg" and "foo (Copy).jpg" have
177177
// same thumb path (if they have same content)
@@ -229,7 +229,7 @@ func collectionThumbnailsOneBatch(
229229

230230
createdThumbnail, err := stoclient.ScanAndDiscoverBlobs(
231231
ctx,
232-
collectionThumbPath(file),
232+
CollectionThumbPath(file),
233233
thumbOutput,
234234
0,
235235
fileModifiedTime, // created
@@ -269,7 +269,7 @@ func collectionThumbnailsOneBatch(
269269
return more, err
270270
}
271271

272-
func collectionThumbPath(f stotypes.File) string {
272+
func CollectionThumbPath(f stotypes.File) string {
273273
return ".sto/thumb/" + f.Sha256[0:10] + ".jpg"
274274
}
275275

pkg/stoserver/commandhandlerscollectionmutations.go

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
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

1819
func (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

Comments
 (0)