mirror of https://github.com/Xhofe/alist
fix(fs): copy file if symlink failed (#3368)
parent
e1b6368343
commit
22843ffc70
|
@ -43,7 +43,7 @@ func getFileStreamFromLink(file model.Obj, link *model.Link) (*model.FileStream,
|
||||||
} else if link.FilePath != nil {
|
} else if link.FilePath != nil {
|
||||||
// create a new temp symbolic link, because it will be deleted after upload
|
// create a new temp symbolic link, because it will be deleted after upload
|
||||||
newFilePath := stdpath.Join(conf.Conf.TempDir, fmt.Sprintf("%s-%s", uuid.NewString(), file.GetName()))
|
newFilePath := stdpath.Join(conf.Conf.TempDir, fmt.Sprintf("%s-%s", uuid.NewString(), file.GetName()))
|
||||||
err := os.Symlink(*link.FilePath, newFilePath)
|
err := utils.SymlinkOrCopyFile(*link.FilePath, newFilePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,7 +40,7 @@ func CopyFile(src, dst string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// CopyDir Dir copies a whole directory recursively
|
// CopyDir Dir copies a whole directory recursively
|
||||||
func CopyDir(src string, dst string) error {
|
func CopyDir(src, dst string) error {
|
||||||
var err error
|
var err error
|
||||||
var fds []os.DirEntry
|
var fds []os.DirEntry
|
||||||
var srcinfo os.FileInfo
|
var srcinfo os.FileInfo
|
||||||
|
@ -71,6 +71,17 @@ func CopyDir(src string, dst string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SymlinkOrCopyFile symlinks a file or copy if symlink failed
|
||||||
|
func SymlinkOrCopyFile(src, dst string) error {
|
||||||
|
if err := CreateNestedDirectory(filepath.Dir(dst)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := os.Symlink(src, dst); err != nil {
|
||||||
|
return CopyFile(src, dst)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// Exists determine whether the file exists
|
// Exists determine whether the file exists
|
||||||
func Exists(name string) bool {
|
func Exists(name string) bool {
|
||||||
if _, err := os.Stat(name); err != nil {
|
if _, err := os.Stat(name); err != nil {
|
||||||
|
@ -81,15 +92,20 @@ func Exists(name string) bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CreateNestedDirectory create nested directory
|
||||||
|
func CreateNestedDirectory(path string) error {
|
||||||
|
err := os.MkdirAll(path, 0700)
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("can't create folder, %s", err)
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// CreateNestedFile create nested file
|
// CreateNestedFile create nested file
|
||||||
func CreateNestedFile(path string) (*os.File, error) {
|
func CreateNestedFile(path string) (*os.File, error) {
|
||||||
basePath := filepath.Dir(path)
|
basePath := filepath.Dir(path)
|
||||||
if !Exists(basePath) {
|
if err := CreateNestedDirectory(basePath); err != nil {
|
||||||
err := os.MkdirAll(basePath, 0700)
|
return nil, err
|
||||||
if err != nil {
|
|
||||||
log.Errorf("can't create folder, %s", err)
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return os.Create(path)
|
return os.Create(path)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue