...
|
...
|
@@ -2,6 +2,7 @@ package upload |
|
|
|
|
|
import (
|
|
|
"fmt"
|
|
|
"github.com/disintegration/imaging"
|
|
|
"io"
|
|
|
"mime/multipart"
|
|
|
"os"
|
...
|
...
|
@@ -69,8 +70,9 @@ func UploadFile(request *protocol.FileRequest) (rsp *protocol.FileResponse, err |
|
|
virtualPath = beego.AppConfig.String("source_host") + filepath.Join(virtualPath, request.FileType, date)
|
|
|
for i := range request.Files {
|
|
|
f := request.Files[i]
|
|
|
prefix := fmt.Sprintf("%v_%v", time.Now().Unix(), common.RandomString(32))
|
|
|
subfix := path.Ext(f.Filename)
|
|
|
filename = fmt.Sprintf("%v_%v%v", time.Now().Unix(), common.RandomString(32), subfix)
|
|
|
filename = fmt.Sprintf("%v%v", prefix, subfix)
|
|
|
src, err = f.Open()
|
|
|
if err != nil {
|
|
|
log.Error(err)
|
...
|
...
|
@@ -88,10 +90,35 @@ func UploadFile(request *protocol.FileRequest) (rsp *protocol.FileResponse, err |
|
|
return
|
|
|
}
|
|
|
rsp.Paths = append(rsp.Paths, filepath.Join(virtualPath, filename))
|
|
|
ResizeImage(request.FileType, sourcePath, prefix, subfix, f)
|
|
|
}
|
|
|
return
|
|
|
}
|
|
|
|
|
|
func ResizeImage(fileType int, fileName string, file *multipart.FileHeader) (err error) {
|
|
|
func ResizeImage(fileType, sourcePath, prefix, subfix string, file *multipart.FileHeader) (err error) {
|
|
|
var (
|
|
|
src multipart.File
|
|
|
thumbName = "_thumb"
|
|
|
)
|
|
|
if fileType != protocol.FileImage {
|
|
|
return
|
|
|
}
|
|
|
filename := fmt.Sprintf("%v%v%v", prefix, thumbName, subfix)
|
|
|
filename = filepath.Join(sourcePath, filename)
|
|
|
if src, err = file.Open(); err != nil {
|
|
|
log.Error(err)
|
|
|
return
|
|
|
}
|
|
|
image, err := imaging.Decode(src)
|
|
|
if err != nil {
|
|
|
fmt.Println(err)
|
|
|
return
|
|
|
}
|
|
|
image = imaging.Resize(image, 0, 200, imaging.Lanczos)
|
|
|
err = imaging.Save(image, filename)
|
|
|
if err != nil {
|
|
|
log.Error(err)
|
|
|
}
|
|
|
log.Debug(fmt.Sprintf("resize iamge:%v", filename))
|
|
|
return
|
|
|
} |
...
|
...
|
|