Download file, button groups, fix unknown file reading (#84)

This commit is contained in:
Thomas Miceli
2023-07-26 15:43:07 +02:00
committed by GitHub
parent 89685bfac6
commit b5cd49db4c
5 changed files with 58 additions and 9 deletions

View File

@ -99,9 +99,17 @@ func GetFileContent(user string, gist string, revision string, filename string,
if err != nil {
return "", false, err
}
defer cmd.Wait()
return truncateCommandOutput(stdout, maxBytes)
output, truncated, err := truncateCommandOutput(stdout, maxBytes)
if err != nil {
return "", false, err
}
if err := cmd.Wait(); err != nil {
return "", false, err
}
return output, truncated, nil
}
func GetLog(user string, gist string, skip int) ([]*Commit, error) {

View File

@ -517,6 +517,30 @@ func rawFile(ctx echo.Context) error {
return plainText(ctx, 200, file.Content)
}
func downloadFile(ctx echo.Context) error {
gist := getData(ctx, "gist").(*models.Gist)
file, err := gist.File(ctx.Param("revision"), ctx.Param("file"), false)
if err != nil {
return errorRes(500, "Error getting file content", err)
}
if file == nil {
return notFound("File not found")
}
ctx.Response().Header().Set("Content-Type", "text/plain")
ctx.Response().Header().Set("Content-Disposition", "attachment; filename="+file.Filename)
ctx.Response().Header().Set("Content-Length", strconv.Itoa(len(file.Content)))
_, err = ctx.Response().Write([]byte(file.Content))
if err != nil {
return errorRes(500, "Error downloading the file", err)
}
return nil
}
func edit(ctx echo.Context) error {
var gist = getData(ctx, "gist").(*models.Gist)

View File

@ -213,6 +213,7 @@ func Start() {
g3.POST("/visibility", toggleVisibility, logged, writePermission)
g3.POST("/delete", deleteGist, logged, writePermission)
g3.GET("/raw/:revision/:file", rawFile)
g3.GET("/download/:revision/:file", downloadFile)
g3.GET("/edit", edit, logged, writePermission)
g3.POST("/edit", processCreate, logged, writePermission)
g3.POST("/like", like, logged)