2022-06-06 13:48:53 +00:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
2022-09-15 09:58:32 +00:00
|
|
|
"fmt"
|
2022-06-15 06:56:43 +00:00
|
|
|
"io"
|
2022-10-09 11:29:55 +00:00
|
|
|
"mime"
|
2022-06-06 13:48:53 +00:00
|
|
|
"os"
|
2022-09-15 09:58:32 +00:00
|
|
|
"path"
|
2022-06-06 13:48:53 +00:00
|
|
|
"path/filepath"
|
2022-09-14 07:14:04 +00:00
|
|
|
"strings"
|
2022-06-15 06:56:43 +00:00
|
|
|
|
2023-08-11 06:23:30 +00:00
|
|
|
"github.com/alist-org/alist/v3/internal/errs"
|
|
|
|
|
2022-08-07 16:51:05 +00:00
|
|
|
"github.com/alist-org/alist/v3/internal/conf"
|
2022-06-15 06:56:43 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2022-06-06 13:48:53 +00:00
|
|
|
)
|
|
|
|
|
2022-09-15 09:58:32 +00:00
|
|
|
// CopyFile File copies a single file from src to dst
|
|
|
|
func CopyFile(src, dst string) error {
|
|
|
|
var err error
|
|
|
|
var srcfd *os.File
|
|
|
|
var dstfd *os.File
|
|
|
|
var srcinfo os.FileInfo
|
|
|
|
|
|
|
|
if srcfd, err = os.Open(src); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer srcfd.Close()
|
|
|
|
|
|
|
|
if dstfd, err = CreateNestedFile(dst); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer dstfd.Close()
|
|
|
|
|
2024-04-25 12:11:15 +00:00
|
|
|
if _, err = CopyWithBuffer(dstfd, srcfd); err != nil {
|
2022-09-15 09:58:32 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if srcinfo, err = os.Stat(src); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return os.Chmod(dst, srcinfo.Mode())
|
|
|
|
}
|
|
|
|
|
|
|
|
// CopyDir Dir copies a whole directory recursively
|
2023-02-13 06:41:35 +00:00
|
|
|
func CopyDir(src, dst string) error {
|
2022-09-15 09:58:32 +00:00
|
|
|
var err error
|
2022-12-17 11:49:05 +00:00
|
|
|
var fds []os.DirEntry
|
2022-09-15 09:58:32 +00:00
|
|
|
var srcinfo os.FileInfo
|
|
|
|
|
|
|
|
if srcinfo, err = os.Stat(src); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = os.MkdirAll(dst, srcinfo.Mode()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-12-17 11:49:05 +00:00
|
|
|
if fds, err = os.ReadDir(src); err != nil {
|
2022-09-15 09:58:32 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, fd := range fds {
|
|
|
|
srcfp := path.Join(src, fd.Name())
|
|
|
|
dstfp := path.Join(dst, fd.Name())
|
|
|
|
|
|
|
|
if fd.IsDir() {
|
|
|
|
if err = CopyDir(srcfp, dstfp); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err = CopyFile(srcfp, dstfp); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-02-13 06:41:35 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2022-06-06 13:48:53 +00:00
|
|
|
// Exists determine whether the file exists
|
|
|
|
func Exists(name string) bool {
|
|
|
|
if _, err := os.Stat(name); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-02-13 06:41:35 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2022-06-15 06:57:13 +00:00
|
|
|
// CreateNestedFile create nested file
|
|
|
|
func CreateNestedFile(path string) (*os.File, error) {
|
2022-06-06 13:48:53 +00:00
|
|
|
basePath := filepath.Dir(path)
|
2023-02-13 06:41:35 +00:00
|
|
|
if err := CreateNestedDirectory(basePath); err != nil {
|
|
|
|
return nil, err
|
2022-06-06 13:48:53 +00:00
|
|
|
}
|
|
|
|
return os.Create(path)
|
|
|
|
}
|
2022-06-15 06:56:43 +00:00
|
|
|
|
|
|
|
// CreateTempFile create temp file from io.ReadCloser, and seek to 0
|
2023-08-27 13:14:23 +00:00
|
|
|
func CreateTempFile(r io.Reader, size int64) (*os.File, error) {
|
2022-09-10 06:11:06 +00:00
|
|
|
if f, ok := r.(*os.File); ok {
|
|
|
|
return f, nil
|
|
|
|
}
|
2022-09-08 12:03:07 +00:00
|
|
|
f, err := os.CreateTemp(conf.Conf.TempDir, "file-*")
|
2022-06-15 06:56:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-04-25 12:11:15 +00:00
|
|
|
readBytes, err := CopyWithBuffer(f, r)
|
2022-06-15 06:56:43 +00:00
|
|
|
if err != nil {
|
2022-09-10 06:11:06 +00:00
|
|
|
_ = os.Remove(f.Name())
|
2023-08-07 05:44:28 +00:00
|
|
|
return nil, errs.NewErr(err, "CreateTempFile failed")
|
|
|
|
}
|
2023-09-05 05:03:29 +00:00
|
|
|
if size > 0 && readBytes != size {
|
2023-08-07 05:44:28 +00:00
|
|
|
_ = os.Remove(f.Name())
|
2023-08-11 06:23:30 +00:00
|
|
|
return nil, errs.NewErr(err, "CreateTempFile failed, incoming stream actual size= %d, expect = %d ", readBytes, size)
|
2022-06-15 06:56:43 +00:00
|
|
|
}
|
|
|
|
_, err = f.Seek(0, io.SeekStart)
|
|
|
|
if err != nil {
|
2022-09-10 06:11:06 +00:00
|
|
|
_ = os.Remove(f.Name())
|
2023-08-07 05:44:28 +00:00
|
|
|
return nil, errs.NewErr(err, "CreateTempFile failed, can't seek to 0 ")
|
2022-06-15 06:56:43 +00:00
|
|
|
}
|
|
|
|
return f, nil
|
|
|
|
}
|
2022-08-07 16:51:05 +00:00
|
|
|
|
|
|
|
// GetFileType get file type
|
|
|
|
func GetFileType(filename string) int {
|
2022-09-14 07:14:04 +00:00
|
|
|
ext := strings.ToLower(Ext(filename))
|
2022-12-20 08:08:32 +00:00
|
|
|
if SliceContains(conf.SlicesMap[conf.AudioTypes], ext) {
|
2022-08-07 16:51:05 +00:00
|
|
|
return conf.AUDIO
|
|
|
|
}
|
2022-12-20 08:08:32 +00:00
|
|
|
if SliceContains(conf.SlicesMap[conf.VideoTypes], ext) {
|
2022-08-07 16:51:05 +00:00
|
|
|
return conf.VIDEO
|
|
|
|
}
|
2022-12-20 08:08:32 +00:00
|
|
|
if SliceContains(conf.SlicesMap[conf.ImageTypes], ext) {
|
2022-08-07 16:51:05 +00:00
|
|
|
return conf.IMAGE
|
|
|
|
}
|
2022-12-20 08:08:32 +00:00
|
|
|
if SliceContains(conf.SlicesMap[conf.TextTypes], ext) {
|
2022-08-07 16:51:05 +00:00
|
|
|
return conf.TEXT
|
|
|
|
}
|
|
|
|
return conf.UNKNOWN
|
|
|
|
}
|
2022-10-09 11:29:55 +00:00
|
|
|
|
2022-11-28 05:45:25 +00:00
|
|
|
func GetObjType(filename string, isDir bool) int {
|
|
|
|
if isDir {
|
|
|
|
return conf.FOLDER
|
|
|
|
}
|
|
|
|
return GetFileType(filename)
|
|
|
|
}
|
|
|
|
|
2023-11-06 10:20:25 +00:00
|
|
|
var extraMimeTypes = map[string]string{
|
|
|
|
".apk": "application/vnd.android.package-archive",
|
|
|
|
}
|
|
|
|
|
2022-10-09 11:29:55 +00:00
|
|
|
func GetMimeType(name string) string {
|
|
|
|
ext := path.Ext(name)
|
2023-11-06 10:20:25 +00:00
|
|
|
if m, ok := extraMimeTypes[ext]; ok {
|
|
|
|
return m
|
|
|
|
}
|
2022-10-09 11:29:55 +00:00
|
|
|
m := mime.TypeByExtension(ext)
|
|
|
|
if m != "" {
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
return "application/octet-stream"
|
|
|
|
}
|
2023-08-27 13:14:23 +00:00
|
|
|
|
|
|
|
const (
|
|
|
|
KB = 1 << (10 * (iota + 1))
|
|
|
|
MB
|
|
|
|
GB
|
|
|
|
TB
|
|
|
|
)
|