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.
108 lines
2.8 KiB
108 lines
2.8 KiB
package file
|
|
|
|
import (
|
|
"base/app/common"
|
|
"base/app/config"
|
|
"encoding/base64"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"mime/multipart"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
var UploadLogic = &uploadLogic{}
|
|
|
|
type uploadLogic struct {
|
|
}
|
|
|
|
// Upload @Title 上传
|
|
func (u *uploadLogic) Upload(file *multipart.FileHeader, path string) (string, error) {
|
|
root := config.Config.Upload.Root
|
|
log.Println(u.getFileMd5(file))
|
|
datePath := common.CreateDateDir(filepath.Join(root, path))
|
|
filename := fmt.Sprintf("%v%v", common.MD5(fmt.Sprintf("%d%s", time.Now().UnixNano(), common.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
|
|
}
|
|
|
|
// UploadBase64 @Title 上传
|
|
func (u *uploadLogic) UploadBase64(file string, path string) (string, error) {
|
|
root := config.Config.Upload.Root
|
|
datePath := common.CreateDateDir(filepath.Join(root, path))
|
|
filename := fmt.Sprintf("%v%v", common.MD5(fmt.Sprintf("%d%s", time.Now().UnixNano(), common.RandStr(4))), ".png")
|
|
savePath := filepath.Join(path, datePath, filename)
|
|
imgSrc, _ := base64.StdEncoding.DecodeString(file[strings.Index(file, ",")+1:])
|
|
if err := os.WriteFile(filepath.Join(root, savePath), imgSrc, 0644); err != nil {
|
|
return "", err
|
|
}
|
|
return savePath, nil
|
|
}
|
|
|
|
// WebFile @Title web下载存储
|
|
func (u *uploadLogic) WebFile(fileUrl, path, filename string) (string, error) {
|
|
root := config.Config.Upload.Root
|
|
datePath := common.CreateDateDir(filepath.Join(root, path))
|
|
if filename == "" {
|
|
filename = fmt.Sprintf("%s%s", common.MD5(fmt.Sprintf("%d%s", time.Now().UnixNano(), common.RandStr(4))), strings.ToLower(filepath.Ext(fileUrl)))
|
|
}
|
|
savePath := filepath.Join(path, datePath, filename)
|
|
resp, err := http.Get(fileUrl)
|
|
if err != nil {
|
|
return "", errors.New("文件错误")
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
out, err := os.Create(filepath.Join(root, savePath))
|
|
if err != nil {
|
|
return "", errors.New("文件错误")
|
|
}
|
|
defer out.Close()
|
|
_, err = io.Copy(out, resp.Body)
|
|
if err != nil {
|
|
return "", errors.New("文件错误")
|
|
}
|
|
return savePath, nil
|
|
}
|
|
|
|
// SaveUploadedFile uploads the form file to specific dst.
|
|
func (u *uploadLogic) 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
|
|
}
|
|
|
|
func (u *uploadLogic) getFileMd5(file *multipart.FileHeader) error {
|
|
open, err := file.Open()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer open.Close()
|
|
all, err := io.ReadAll(open)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
md5Byte := common.MD5Byte(all)
|
|
log.Println(md5Byte)
|
|
return nil
|
|
}
|