From e7f1664f766c34440d2c15fe4bd066cbacac85dc Mon Sep 17 00:00:00 2001 From: xieyp <20084040@qq.com> Date: Sun, 11 Nov 2018 20:14:03 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=9B=BE=E7=89=87=E6=94=BE?= =?UTF-8?q?=E7=BC=A9=E5=A4=84=E7=90=86=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/Img.go | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++++ core/Web.go | 2 ++ readme.md | 4 ++-- 3 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 core/Img.go diff --git a/core/Img.go b/core/Img.go new file mode 100644 index 0000000..77d4525 --- /dev/null +++ b/core/Img.go @@ -0,0 +1,54 @@ +package core + +import ( + "goserver/utils" + "github.com/gin-gonic/gin" + "strings" + "goserver/webs/upload" + "strconv" + "io/ioutil" + "github.com/cihub/seelog" +) + +type ImgResize struct { + dir string + size []string +} + +func (ir *ImgResize) DoResize(context *gin.Context) { + url := context.Query("url") + if len(url) > 1 { + dp := strings.LastIndex(url, ".") + pp := strings.LastIndex(url, "_") + if dp > pp && pp > 0 { + w := url[pp+1 : dp] + for _, ss := range ir.size { + if w == ss { + target := ir.dir + url + source := ir.dir + url[0:pp] + url[dp:] + if strings.Index(w, "x") < 0 { + tw, twe := strconv.Atoi(w) + if twe == nil { + upload.ImgResize(source, target, tw) + bs, err := ioutil.ReadFile(target) + if err == nil { + context.Header("Content-Type", "image/"+url[dp+1:]) + context.Writer.Write(bs) + } else { + seelog.Error("target file error:", target) + } + return + } + } + } + } + seelog.Error("生成的宽度不支持,", w) + } + } +} + +func (app *Module) ImgResize(ele *utils.Element) { + ir := &ImgResize{dir: ele.MustAttr("Dir"), + size: strings.Split(ele.MustAttr("Size"), ",")} + getGin(ele).GET(ele.MustAttr("Url"), ir.DoResize) +} diff --git a/core/Web.go b/core/Web.go index 7b75ff3..607396e 100644 --- a/core/Web.go +++ b/core/Web.go @@ -31,6 +31,7 @@ func (app *Module) Sp(ele *utils.Element) { put(ele, sp) } + //文件上传 /** TmpDir,MainWidth,ImgWidth(多个用,分隔) 可选 @@ -43,6 +44,7 @@ TmpDir,MainWidth,ImgWidth(多个用,分隔) 可选 */ + func (app *Module) Upload(ele *utils.Element) { sp, spExt := ele.AttrValue("SpRef") var nameFun func(c *webs.Param, tmpFileName string) (string, error) diff --git a/readme.md b/readme.md index 5ffb4e2..80e99c1 100644 --- a/readme.md +++ b/readme.md @@ -1,6 +1,6 @@ -### 项目介绍 -goserver 把存储过程映射成JSON接口. 其它功能 +### goserver 功能介绍 +* 存储过程映射成JSON接口 * 认证码 * 定时任务(执行SQL,爬虫) * 模版