chore: create temp file util

refactor/fs
Noah Hsu 2022-06-15 14:56:43 +08:00
parent 6cdd85283b
commit 066ddd3e09
2 changed files with 27 additions and 1 deletions

View File

@ -1 +1,6 @@
package message package message
type Messager interface {
Send(string, interface{}) error
Receive(string) (string, error)
}

View File

@ -1,9 +1,13 @@
package utils package utils
import ( import (
log "github.com/sirupsen/logrus" "io"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"github.com/alist-org/alist/v3/conf"
log "github.com/sirupsen/logrus"
) )
// Exists determine whether the file exists // Exists determine whether the file exists
@ -28,3 +32,20 @@ func CreatNestedFile(path string) (*os.File, error) {
} }
return os.Create(path) return os.Create(path)
} }
// 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
}