feat: make cross device moves possible
This commit is contained in:
parent
051e257fba
commit
6c38703cf2
2
main.go
2
main.go
@ -230,7 +230,7 @@ func handleExistingFile(cfg config, fileExistsResponse FileExistsResult) error {
|
||||
subfolder := fmt.Sprintf("%s %s - %s.%s", date.Format("20060102"), corr, itemName, extension)
|
||||
newPath := filepath.Join(curDir, subfolder)
|
||||
|
||||
if err := os.Rename(fileExistsResponse.File, newPath); err != nil {
|
||||
if err := MoveFile(fileExistsResponse.File, newPath); err != nil {
|
||||
return fmt.Errorf("move file: %w", err)
|
||||
}
|
||||
|
||||
|
40
util.go
Normal file
40
util.go
Normal file
@ -0,0 +1,40 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
// MoveFile moves a file across file system boundaries
|
||||
// taken from https://stackoverflow.com/questions/50740902/move-a-file-to-a-different-drive-with-go
|
||||
func MoveFile(sourcePath, destPath string) error {
|
||||
inputFile, err := os.Open(sourcePath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Couldn't open source file: %w", err)
|
||||
}
|
||||
defer inputFile.Close()
|
||||
|
||||
outputFile, err := os.Create(destPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Couldn't open dest file: %w", err)
|
||||
}
|
||||
defer outputFile.Close()
|
||||
|
||||
_, err = io.Copy(outputFile, inputFile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Couldn't copy to dest from source: %w", err)
|
||||
}
|
||||
|
||||
// for Windows, close before trying to remove: https://stackoverflow.com/a/64943554/246801
|
||||
err = inputFile.Close()
|
||||
if err != nil {
|
||||
return fmt.Errorf("Couldn't close input: %w", err)
|
||||
}
|
||||
|
||||
err = os.Remove(sourcePath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Couldn't remove source file: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user