50 lines
981 B
Go
50 lines
981 B
Go
package backend
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"os"
|
|
)
|
|
|
|
func CopyFile(src, dst string) error {
|
|
srcFile, err := os.Open(src)
|
|
if err != nil {
|
|
return fmt.Errorf("error opening source file: %v", err)
|
|
}
|
|
defer srcFile.Close()
|
|
|
|
dstFile, err := os.OpenFile(dst, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
|
|
if err != nil {
|
|
return fmt.Errorf("error opening destination file: %v", err)
|
|
}
|
|
defer dstFile.Close()
|
|
|
|
if _, err = io.Copy(dstFile, srcFile); err != nil {
|
|
return fmt.Errorf("error copying file: %v", err)
|
|
}
|
|
|
|
return dstFile.Sync()
|
|
}
|
|
|
|
func WriteFile(path string, src io.Reader) error {
|
|
file, err := os.Create(path)
|
|
if err != nil {
|
|
log.Println(err)
|
|
return fmt.Errorf("error creating file: %v", err)
|
|
}
|
|
defer file.Close()
|
|
|
|
if _, err = io.Copy(file, src); err != nil {
|
|
log.Println(err)
|
|
return fmt.Errorf("error copying file: %v", err)
|
|
}
|
|
|
|
if err = file.Sync(); err != nil {
|
|
log.Println(err)
|
|
return fmt.Errorf("error syncing file: %v", err)
|
|
}
|
|
|
|
return nil
|
|
}
|