From 8998e46fea73302e9db4e2f92e14ce323daad41c Mon Sep 17 00:00:00 2001 From: Thomas Miceli Date: Sun, 19 Mar 2023 00:27:30 +0100 Subject: [PATCH] Update preview and file count when git push with http/ssh --- internal/models/gist.go | 31 +++++++++++++++++++++++++++++++ internal/ssh/git_ssh.go | 1 + internal/web/git_http.go | 4 +++- 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/internal/models/gist.go b/internal/models/gist.go index 421eaf1..db8d570 100644 --- a/internal/models/gist.go +++ b/internal/models/gist.go @@ -4,6 +4,7 @@ import ( "gorm.io/gorm" "opengist/internal/git" "os/exec" + "strings" "time" ) @@ -260,6 +261,36 @@ func (gist *Gist) RPC(service string) ([]byte, error) { return git.RPC(gist.User.Username, gist.Uuid, service) } +func (gist *Gist) UpdatePreviewAndCount() error { + filesStr, err := git.GetFilesOfRepository(gist.User.Username, gist.Uuid, "HEAD") + if err != nil { + return err + } + gist.NbFiles = len(filesStr) + + if len(filesStr) == 0 { + gist.Preview = "" + gist.PreviewFilename = "" + } else { + file, err := gist.File("HEAD", filesStr[0], true) + if err != nil { + return err + } + + split := strings.Split(file.Content, "\n") + if len(split) > 10 { + gist.Preview = strings.Join(split[:10], "\n") + } else { + gist.Preview = file.Content + } + + gist.Preview = file.Content + gist.PreviewFilename = file.Filename + } + + return gist.Update() +} + // -- DTO -- // type GistDTO struct { diff --git a/internal/ssh/git_ssh.go b/internal/ssh/git_ssh.go index 02a876a..dadf937 100644 --- a/internal/ssh/git_ssh.go +++ b/internal/ssh/git_ssh.go @@ -83,6 +83,7 @@ func runGitCommand(ch ssh.Channel, gitCmd string, keyID uint) error { // updatedAt is updated only if serviceType is receive-pack if verb == "receive-pack" { _ = gist.SetLastActiveNow() + _ = gist.UpdatePreviewAndCount() } return nil diff --git a/internal/web/git_http.go b/internal/web/git_http.go index bd49079..1b2acbf 100644 --- a/internal/web/git_http.go +++ b/internal/web/git_http.go @@ -134,7 +134,9 @@ func pack(ctx echo.Context, serviceType string) error { // updatedAt is updated only if serviceType is receive-pack if serviceType == "receive-pack" { - _ = getData(ctx, "gist").(*models.Gist).SetLastActiveNow() + gist := getData(ctx, "gist").(*models.Gist) + _ = gist.SetLastActiveNow() + _ = gist.UpdatePreviewAndCount() } return nil }