Skip to content
Permalink
Browse files

Use context.File for unencrypted downloads

This is faster than io.copy, allows the clients to show progress (emits
the Content-Length header) and also enables resuming (supports the Range
header - cf #28).

This commit also reduces the amount of duplicated code between View()
and Head().
  • Loading branch information...
tribut committed Feb 26, 2018
1 parent 573e1c2 commit 390ce075c9aa6c9371e40ae2a165f7a14c2ab276
Showing with 19 additions and 36 deletions.
  1. +19 −36 server/views/unencrypted.go
@@ -97,8 +97,8 @@ func Create(c *gin.Context) {
c.String(http.StatusCreated, "%v://%s/v/%s\n", utils.DetectScheme(c), ns, u)
}

// View handles the file views
func View(c *gin.Context) {
// HandleRequest responds to a View or Head request for unencrypted files
func HandleRequest(c *gin.Context, isView bool) {
var err error

id := c.Param("uniuri")
@@ -110,53 +110,36 @@ func View(c *gin.Context) {
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", "attachment; filename=\""+re.Name+"\"")
} else {
c.Header("Content-Disposition", "filename=\""+re.Name+"\"")
}
io.Copy(c.Writer, f)
if re.Once {
re.Delete()
re.LogDeleted(c)
}
}

// Head handles the head request for an encryptd file
func Head(c *gin.Context) {
var err error

id := c.Param("uniuri")
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 {
file := path.Join(conf.C.UploadDir, re.Key)
if _, err := os.Stat(file); 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", "attachment; filename=\""+re.Name+"\"")
} else {
c.Header("Content-Disposition", "filename=\""+re.Name+"\"")

c.File(file)

if isView && re.Once {
re.Delete()
re.LogDeleted(c)
}
io.Copy(c.Writer, f)
}

// View handles the file views
func View(c *gin.Context) {
HandleRequest(c, true)
}

// Head handles the head request
func Head(c *gin.Context) {
HandleRequest(c, false)
}

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

0 comments on commit 390ce07

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