1
0
mirror of https://github.com/thomiceli/opengist synced 2024-11-08 12:55:50 +01:00

Update preview and file count when git push with http/ssh

This commit is contained in:
Thomas Miceli 2023-03-19 00:27:30 +01:00
parent b505f7147e
commit 8998e46fea
No known key found for this signature in database
GPG Key ID: D86C6F6390AF050F
3 changed files with 35 additions and 1 deletions

@ -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 {

@ -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

@ -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
}