From 4fafc7605245ef175a8fb8558eb7c30c4f7b0400 Mon Sep 17 00:00:00 2001 From: slene Date: Wed, 16 Apr 2014 00:27:29 +0800 Subject: zip archive download --- modules/middleware/context.go | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) (limited to 'modules/middleware/context.go') diff --git a/modules/middleware/context.go b/modules/middleware/context.go index 619a13b1ac..1330172fde 100644 --- a/modules/middleware/context.go +++ b/modules/middleware/context.go @@ -10,8 +10,10 @@ import ( "encoding/base64" "fmt" "html/template" + "io" "net/http" "net/url" + "path/filepath" "strconv" "strings" "time" @@ -62,7 +64,7 @@ type Context struct { HTTPS string Git string } - *models.Mirror + Mirror *models.Mirror } } @@ -243,6 +245,41 @@ func (ctx *Context) CsrfTokenValid() bool { return true } +func (ctx *Context) ServeFile(file string, names ...string) { + var name string + if len(names) > 0 { + name = names[0] + } else { + name = filepath.Base(file) + } + ctx.Res.Header().Set("Content-Description", "File Transfer") + ctx.Res.Header().Set("Content-Type", "application/octet-stream") + ctx.Res.Header().Set("Content-Disposition", "attachment; filename="+name) + ctx.Res.Header().Set("Content-Transfer-Encoding", "binary") + ctx.Res.Header().Set("Expires", "0") + ctx.Res.Header().Set("Cache-Control", "must-revalidate") + ctx.Res.Header().Set("Pragma", "public") + http.ServeFile(ctx.Res, ctx.Req, file) +} + +func (ctx *Context) ServeContent(name string, r io.ReadSeeker, params ...interface{}) { + modtime := time.Now() + for _, p := range params { + switch v := p.(type) { + case time.Time: + modtime = v + } + } + ctx.Res.Header().Set("Content-Description", "File Transfer") + ctx.Res.Header().Set("Content-Type", "application/octet-stream") + ctx.Res.Header().Set("Content-Disposition", "attachment; filename="+name) + ctx.Res.Header().Set("Content-Transfer-Encoding", "binary") + ctx.Res.Header().Set("Expires", "0") + ctx.Res.Header().Set("Cache-Control", "must-revalidate") + ctx.Res.Header().Set("Pragma", "public") + http.ServeContent(ctx.Res, ctx.Req, name, modtime, r) +} + type Flash struct { url.Values ErrorMsg, SuccessMsg string -- cgit v1.2.3