You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
1003 B
47 lines
1003 B
package file
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"mime/multipart"
|
|
"os"
|
|
"path/filepath"
|
|
"recook/internal/v2/lib/common"
|
|
"recook/tools"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
var Upload = &upload{}
|
|
|
|
type upload struct {
|
|
}
|
|
|
|
func (u *upload) Upload(file *multipart.FileHeader, root, path string) (string, error) {
|
|
datePath := common.CreateDateDir(filepath.Join(root, path))
|
|
filename := fmt.Sprintf("%v%v", tools.MD5(fmt.Sprintf("%d%s", time.Now().UnixNano(), tools.RandStr(4))), strings.ToLower(filepath.Ext(file.Filename)))
|
|
savePath := filepath.Join(path, datePath, filename)
|
|
if err := u.saveFile(file, filepath.Join(root, savePath)); err != nil {
|
|
return "", err
|
|
}
|
|
return savePath, nil
|
|
}
|
|
|
|
// SaveUploadedFile uploads the form file to specific dst.
|
|
func (u *upload) saveFile(file *multipart.FileHeader, dst string) error {
|
|
src, err := file.Open()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer src.Close()
|
|
|
|
out, err := os.Create(dst)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer out.Close()
|
|
|
|
_, err = io.Copy(out, src)
|
|
return err
|
|
}
|