dedupe/files/mover.go

40 lines
1 KiB
Go
Raw Normal View History

2023-08-15 14:51:54 +02:00
package files
import (
"io/fs"
"os"
"path/filepath"
"strings"
"time"
"velvettear/dedupe/log"
"velvettear/dedupe/settings"
)
// exported function(s)
func MoveFile(file string) bool {
timestamp := time.Now()
targetFile := filepath.Join(settings.MoveDirectory, strings.Replace(file, settings.ComparisonDirectory, "", 1))
targetDirectory := filepath.Dir(targetFile)
error := createTargetDirectory(targetDirectory, 0777)
if error != nil {
log.Error("encountered an error creating the directory '"+targetDirectory+"'", error.Error())
return false
}
error = os.Rename(file, targetFile)
if error != nil {
log.Error("encountered an error moving the file '"+file+"' to '"+targetFile+"'", error.Error())
return false
}
log.DebugTimed("moved file '"+file+"' to '"+targetFile+"'", timestamp.UnixMilli())
return true
}
func createTargetDirectory(directory string, permissions fs.FileMode) error {
error := os.MkdirAll(directory, permissions)
if error != nil {
return error
}
log.Debug("created directory '" + directory + "'")
return nil
}