diff options
author | slene <vslene@gmail.com> | 2014-04-16 00:27:29 +0800 |
---|---|---|
committer | slene <vslene@gmail.com> | 2014-04-16 00:29:03 +0800 |
commit | 4fafc7605245ef175a8fb8558eb7c30c4f7b0400 (patch) | |
tree | 4479b8792564c088931fd025ce45d31746bf064e /modules | |
parent | 5378bb326b14192cbbc1309c3142cfc49c74a882 (diff) | |
download | gitea-4fafc7605245ef175a8fb8558eb7c30c4f7b0400.tar.gz gitea-4fafc7605245ef175a8fb8558eb7c30c4f7b0400.zip |
zip archive download
Diffstat (limited to 'modules')
-rw-r--r-- | modules/middleware/context.go | 39 |
1 files changed, 38 insertions, 1 deletions
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 |