Skip to content
Permalink
Browse files

Allow ViewCode in unencrypted mode

  • Loading branch information...
tribut committed Feb 16, 2018
1 parent 4808ee1 commit c0887fa5908b30f373c3802c4eb16680c893eee1
Showing with 45 additions and 0 deletions.
  1. +1 −0 server/router/router.go
  2. +44 −0 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)
@@ -1,6 +1,7 @@
package views

import (
"bytes"
"bufio"
"fmt"
"io"
@@ -149,3 +150,46 @@ func Head(c *gin.Context) {
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
}
if conf.C.AlwaysDownload {
c.Header("Content-Type", "application/octet-stream")
}
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 c0887fa

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