From b5cd49db4c4bc1f4c4c339230c2a36cb20965624 Mon Sep 17 00:00:00 2001 From: Thomas Miceli <27960254+thomiceli@users.noreply.github.com> Date: Wed, 26 Jul 2023 15:43:07 +0200 Subject: [PATCH] Download file, button groups, fix unknown file reading (#84) --- internal/git/commands.go | 12 ++++++++++-- internal/web/gist.go | 24 ++++++++++++++++++++++++ internal/web/run.go | 1 + public/main.ts | 2 +- templates/pages/gist.html | 28 ++++++++++++++++++++++------ 5 files changed, 58 insertions(+), 9 deletions(-) diff --git a/internal/git/commands.go b/internal/git/commands.go index b3cc687..c93b70f 100644 --- a/internal/git/commands.go +++ b/internal/git/commands.go @@ -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) { diff --git a/internal/web/gist.go b/internal/web/gist.go index e3e1d67..6080fde 100644 --- a/internal/web/gist.go +++ b/internal/web/gist.go @@ -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) diff --git a/internal/web/run.go b/internal/web/run.go index 0d92733..14e8b9c 100644 --- a/internal/web/run.go +++ b/internal/web/run.go @@ -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) diff --git a/public/main.ts b/public/main.ts index 8992039..c354e35 100644 --- a/public/main.ts +++ b/public/main.ts @@ -178,7 +178,7 @@ document.addEventListener('DOMContentLoaded', () => { document.querySelectorAll('.copy-gist-btn').forEach((e: HTMLElement) => { e.onclick = () => { - navigator.clipboard.writeText(e.parentNode!.querySelector('.gist-content')!.textContent || '').catch((err) => { + navigator.clipboard.writeText(e.parentNode!.parentNode!.querySelector('.gist-content')!.textContent || '').catch((err) => { console.error('Could not copy text: ', err); }); }; diff --git a/templates/pages/gist.html b/templates/pages/gist.html index e72cdf9..ff5932a 100644 --- a/templates/pages/gist.html +++ b/templates/pages/gist.html @@ -7,13 +7,29 @@
- - - - {{ $file.Filename }} - - Raw + + + + + {{ $file.Filename }} + + + + Raw + + + + + + + + +
{{ if $file.Truncated }}