Add new feature of image cache.

This commit is contained in:
zicla
2018-11-22 23:18:36 +08:00
parent 459fa036be
commit 6c19a3d321
5 changed files with 123 additions and 74 deletions

View File

@ -3,10 +3,11 @@ package rest
import (
"fmt"
"net/http"
"os"
"regexp"
"strconv"
"time"
"strings"
"time"
)
type AlienController struct {
@ -435,17 +436,48 @@ func (this *AlienController) Download(writer http.ResponseWriter, request *http.
}
}
//缓存
var uri string = request.RequestURI
//this.imageCacheDao.FindByUri(uri)
fmt.Printf(uri)
//对图片做缩放处理。
imageProcess := request.FormValue("imageProcess")
if imageProcess == "resize" {
this.matterService.ResizeImage(writer, request, matter)
//如果是图片,那么能用缓存就用缓存
uri := request.RequestURI
imageCache := this.imageCacheDao.FindByUri(uri)
if imageCache != nil {
//直接使用缓存中的信息
this.matterService.DownloadFile(writer, request, CONFIG.MatterPath+imageCache.Path, matter.Name)
} else {
//resize图片
dstImage := this.matterService.ResizeImage(request, CONFIG.MatterPath+matter.Path)
user := this.userDao.FindByUuid(matter.UserUuid)
//获取文件应该存放在的物理路径的绝对路径和相对路径。
absolutePath, relativePath := GetUserFilePath(user.Username, true)
absolutePath = absolutePath + "/" + filename
relativePath = relativePath + "/" + filename
fileWriter, err := os.Create(absolutePath)
this.PanicError(err)
defer fileWriter.Close()
//生成的图片输出到两个writer中去
this.matterService.DownloadImage(writer, fileWriter, dstImage, matter.Name)
//相关信息写到缓存中去
imageCache = &ImageCache{
UserUuid: matter.UserUuid,
MatterUuid: matter.Uuid,
Uri: uri,
Path: relativePath,
}
this.imageCacheDao.Create(imageCache)
}
} else {
this.matterService.DownloadFile(writer, request, matter)
this.matterService.DownloadFile(writer, request, CONFIG.MatterPath+matter.Path, matter.Name)
}
}