photoprism/pkg/fs/move.go
Michael Mayer 91774c4068 Security: Create new files without execution permission #2809
Signed-off-by: Michael Mayer <michael@photoprism.app>
2022-10-31 15:01:48 +01:00

34 lines
510 B
Go

package fs
import (
"fmt"
"os"
"path/filepath"
)
// Moves a file to a new destination.
func Move(src, dest string) (err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("move: %s (panic)", r)
}
}()
if err := os.MkdirAll(filepath.Dir(dest), ModeDir); err != nil {
return err
}
if err := os.Rename(src, dest); err == nil {
return nil
}
if err := Copy(src, dest); err != nil {
return err
}
if err := os.Remove(src); err != nil {
return err
}
return nil
}