Skip to content
Permalink
Browse files

Merge pull request #60 from tribut/fix-forcedownload

Some fixes for forced download
  • Loading branch information...
Depado committed Feb 16, 2018
2 parents 4808ee1 + e798337 commit 0b032c21dd9a527013039f51dbafee4c3e5734ff
Showing with 57 additions and 7 deletions.
  1. +1 −0 server/router/router.go
  2. +9 −5 server/views/resources.go
  3. +47 −2 server/views/unencrypted.go
@@ -33,6 +33,7 @@ func Setup(tbox, abox *rice.Box) (*gin.Engine, error) {
r.POST("/", views.Create)
r.GET("/v/:uniuri", views.View)
r.HEAD("/v/:uniuri", views.Head)
r.GET("/v/:uniuri/:lang", views.ViewCode)
} else {
r.POST("/", views.CreateC)
r.GET("/v/:uniuri/:key", views.ViewC)
@@ -121,8 +121,10 @@ func ViewC(c *gin.Context) {
reader := &cipher.StreamReader{S: stream, R: f}
if conf.C.AlwaysDownload {
c.Header("Content-Type", "application/octet-stream")
c.Header("Content-Disposition", "attachment; filename=\""+re.Name+"\"")
} else {
c.Header("Content-Disposition", "filename=\""+re.Name+"\"")
}
c.Header("Content-Disposition", "filename=\""+re.Name+"\"")
io.Copy(c.Writer, reader)
if re.Once {
re.Delete()
@@ -163,9 +165,6 @@ func ViewCCode(c *gin.Context) {
var iv [aes.BlockSize]byte
stream := cipher.NewCFBDecrypter(block, iv[:])
reader := &cipher.StreamReader{S: stream, R: f}
if conf.C.AlwaysDownload {
c.Header("Content-Type", "application/octet-stream")
}
c.Header("Content-Disposition", "filename=\""+re.Name+"\"")
buf := new(bytes.Buffer)
buf.ReadFrom(reader)
@@ -218,6 +217,11 @@ func HeadC(c *gin.Context) {
var iv [aes.BlockSize]byte
stream := cipher.NewCFBDecrypter(block, iv[:])
reader := &cipher.StreamReader{S: stream, R: f}
c.Header("Content-Disposition", "filename=\""+re.Name+"\"")
if conf.C.AlwaysDownload {
c.Header("Content-Type", "application/octet-stream")
c.Header("Content-Disposition", "attachment; filename=\""+re.Name+"\"")
} else {
c.Header("Content-Disposition", "filename=\""+re.Name+"\"")
}
io.Copy(c.Writer, reader)
}
@@ -1,6 +1,7 @@
package views

import (
"bytes"
"bufio"
"fmt"
"io"
@@ -115,8 +116,10 @@ func View(c *gin.Context) {
}
if conf.C.AlwaysDownload {
c.Header("Content-Type", "application/octet-stream")
c.Header("Content-Disposition", "attachment; filename=\""+re.Name+"\"")
} else {
c.Header("Content-Disposition", "filename=\""+re.Name+"\"")
}
c.Header("Content-Disposition", "filename=\""+re.Name+"\"")
io.Copy(c.Writer, f)
if re.Once {
re.Delete()
@@ -145,7 +148,49 @@ func Head(c *gin.Context) {
}
if conf.C.AlwaysDownload {
c.Header("Content-Type", "application/octet-stream")
c.Header("Content-Disposition", "attachment; filename=\""+re.Name+"\"")
} else {
c.Header("Content-Disposition", "filename=\""+re.Name+"\"")
}
c.Header("Content-Disposition", "filename=\""+re.Name+"\"")
io.Copy(c.Writer, f)
}


// ViewCode allows to see the file with syntax highliting and extra options
func ViewCode(c *gin.Context) {
var err error

id := c.Param("uniuri")
lang := c.Param("lang")
theme := c.DefaultQuery("theme", "dark")
lines := c.Query("lines") == "true"
re := models.Resource{}

if err = re.Get(id); err != nil || re.Key == "" {
logger.InfoC(c, "server", "Not found", id)
c.AbortWithStatus(http.StatusNotFound)
return
}
re.LogFetched(c)
f, err := os.Open(path.Join(conf.C.UploadDir, re.Key))
if err != nil {
logger.ErrC(c, "server", fmt.Sprintf("Couldn't open %s", re.Key), err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
c.Header("Content-Disposition", "filename=\""+re.Name+"\"")
buf := new(bytes.Buffer)
buf.ReadFrom(f)
bb := buf.Bytes()
c.HTML(http.StatusOK, "code.tmpl", gin.H{
"code": string(bb),
"lang": lang,
"theme": theme,
"lines": lines,
"name": re.Name,
})
if re.Once {
re.Delete()
re.LogDeleted(c)
}
}

0 comments on commit 0b032c2

Please sign in to comment.
You can’t perform that action at this time.