filebrowser/fileutils/file.go

52 lines
865 B
Go
Raw Normal View History

package fileutils
import (
"io"
"path/filepath"
"github.com/spf13/afero"
)
// CopyFile copies a file from source to dest and returns
// an error if any.
2020-05-31 23:12:36 +00:00
func CopyFile(fs afero.Fs, source, dest string) error {
// Open the source file.
src, err := fs.Open(source)
if err != nil {
return err
}
defer src.Close()
// Makes the directory needed to create the dst
// file.
err = fs.MkdirAll(filepath.Dir(dest), 0666)
if err != nil {
return err
}
// Create the destination file.
dst, err := fs.Create(dest)
if err != nil {
return err
}
defer dst.Close()
// Copy the contents of the file.
_, err = io.Copy(dst, src)
if err != nil {
return err
}
// Copy the mode if the user can't
// open the file.
info, err := fs.Stat(source)
if err != nil {
err = fs.Chmod(dest, info.Mode())
if err != nil {
return err
}
}
return nil
}