Replace os.Rename with util.SafeMove to allow cross device moving to not fail. (#745)

Fixed annoyingly noisy transcoding progress log messages.
Fixed minor minor issue with directories than are named "blah.mp4" being detected as files to be scanned.
This commit is contained in:
JoeSmithStarkers
2020-08-21 17:57:07 +10:00
committed by GitHub
parent 6a3588e4e0
commit 85aa1d8790
7 changed files with 70 additions and 14 deletions

View File

@@ -3,6 +3,7 @@ package utils
import (
"archive/zip"
"fmt"
"io"
"io/ioutil"
"math"
"net/http"
@@ -12,6 +13,7 @@ import (
"github.com/h2non/filetype"
"github.com/h2non/filetype/types"
"github.com/stashapp/stash/pkg/logger"
)
// FileType uses the filetype package to determine the given file path's type
@@ -131,6 +133,43 @@ func GetHomeDirectory() string {
return currentUser.HomeDir
}
func SafeMove(src, dst string) error {
err := os.Rename(src, dst)
if err != nil {
logger.Errorf("[Util] unable to rename: \"%s\" due to %s. Falling back to copying.", src, err.Error())
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()
out, err := os.Create(dst)
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, in)
if err != nil {
return err
}
err = out.Close()
if err != nil {
return err
}
err = os.Remove(src)
if err != nil {
return err
}
}
return nil
}
// IsZipFileUnmcompressed returns true if zip file in path is using 0 compression level
func IsZipFileUncompressed(path string) (bool, error) {
r, err := zip.OpenReader(path)