2022-06-06 13:48:53 +00:00
|
|
|
|
package utils
|
|
|
|
|
|
|
|
|
|
import (
|
2022-06-15 06:56:43 +00:00
|
|
|
|
"io"
|
|
|
|
|
"io/ioutil"
|
2022-06-06 13:48:53 +00:00
|
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
2022-06-15 06:56:43 +00:00
|
|
|
|
|
|
|
|
|
"github.com/alist-org/alist/v3/conf"
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
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
|
|
|
|
|
}
|
|
|
|
|
|
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)
|
|
|
|
|
if !Exists(basePath) {
|
|
|
|
|
err := os.MkdirAll(basePath, 0700)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Errorf("can't create foler,%s", err)
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return os.Create(path)
|
|
|
|
|
}
|
2022-06-15 06:56:43 +00:00
|
|
|
|
|
|
|
|
|
// CreateTempFile create temp file from io.ReadCloser, and seek to 0
|
|
|
|
|
func CreateTempFile(r io.ReadCloser) (*os.File, error) {
|
|
|
|
|
f, err := ioutil.TempFile(conf.Conf.TempDir, "file-*")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
_, err = io.Copy(f, r)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
_, err = f.Seek(0, io.SeekStart)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return f, nil
|
|
|
|
|
}
|