Skip to content

Commit e8e73a4

Browse files
committed
collection.MoveFilesIntoAnotherCollection: move related (e.g. thumbs) together
- delete related files as well
1 parent 447ec67 commit e8e73a4

3 files changed

Lines changed: 45 additions & 11 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: 39 additions & 7 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 {
@@ -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

Comments
 (0)