2023-03-14 15:22:52 +00:00
|
|
|
package web
|
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/zip"
|
|
|
|
"bytes"
|
2023-03-14 22:26:39 +00:00
|
|
|
"errors"
|
2023-03-14 15:22:52 +00:00
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/labstack/echo/v4"
|
2023-05-15 19:07:29 +00:00
|
|
|
"github.com/thomiceli/opengist/internal/config"
|
|
|
|
"github.com/thomiceli/opengist/internal/models"
|
2023-03-14 22:26:39 +00:00
|
|
|
"gorm.io/gorm"
|
2023-03-14 15:22:52 +00:00
|
|
|
"html/template"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
func gistInit(next echo.HandlerFunc) echo.HandlerFunc {
|
|
|
|
return func(ctx echo.Context) error {
|
|
|
|
userName := ctx.Param("user")
|
|
|
|
gistName := ctx.Param("gistname")
|
|
|
|
|
2023-04-26 21:13:11 +00:00
|
|
|
gistName = strings.TrimSuffix(gistName, ".git")
|
2023-03-14 15:22:52 +00:00
|
|
|
|
|
|
|
gist, err := models.GetGist(userName, gistName)
|
|
|
|
if err != nil {
|
|
|
|
return notFound("Gist not found")
|
|
|
|
}
|
|
|
|
setData(ctx, "gist", gist)
|
|
|
|
|
2023-04-06 23:52:56 +00:00
|
|
|
if config.C.SshGit {
|
|
|
|
var sshDomain string
|
|
|
|
|
|
|
|
if config.C.SshExternalDomain != "" {
|
|
|
|
sshDomain = config.C.SshExternalDomain
|
|
|
|
} else {
|
|
|
|
sshDomain = strings.Split(ctx.Request().Host, ":")[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.C.SshPort == "22" {
|
|
|
|
setData(ctx, "sshCloneUrl", sshDomain+":"+userName+"/"+gistName+".git")
|
2023-03-15 10:47:17 +00:00
|
|
|
} else {
|
2023-04-06 23:52:56 +00:00
|
|
|
setData(ctx, "sshCloneUrl", "ssh://"+sshDomain+":"+config.C.SshPort+"/"+userName+"/"+gistName+".git")
|
2023-03-15 10:47:17 +00:00
|
|
|
}
|
2023-03-14 15:22:52 +00:00
|
|
|
}
|
|
|
|
|
2023-03-15 10:47:17 +00:00
|
|
|
httpProtocol := "http"
|
2023-04-10 20:45:52 +00:00
|
|
|
if ctx.Request().TLS != nil || ctx.Request().Header.Get("X-Forwarded-Proto") == "https" {
|
2023-03-15 10:47:17 +00:00
|
|
|
httpProtocol = "https"
|
|
|
|
}
|
|
|
|
setData(ctx, "httpProtocol", strings.ToUpper(httpProtocol))
|
|
|
|
|
2023-04-06 23:52:56 +00:00
|
|
|
var baseHttpUrl string
|
|
|
|
// if a custom external url is set, use it
|
|
|
|
if config.C.ExternalUrl != "" {
|
|
|
|
baseHttpUrl = config.C.ExternalUrl
|
|
|
|
} else {
|
|
|
|
baseHttpUrl = httpProtocol + "://" + ctx.Request().Host
|
2023-03-23 15:00:48 +00:00
|
|
|
}
|
|
|
|
|
2023-04-06 23:52:56 +00:00
|
|
|
if config.C.HttpGit {
|
|
|
|
setData(ctx, "httpCloneUrl", baseHttpUrl+"/"+userName+"/"+gistName+".git")
|
2023-03-15 10:47:17 +00:00
|
|
|
}
|
2023-03-14 15:22:52 +00:00
|
|
|
|
2023-04-06 23:52:56 +00:00
|
|
|
setData(ctx, "httpCopyUrl", baseHttpUrl+"/"+userName+"/"+gistName)
|
2023-03-14 15:22:52 +00:00
|
|
|
setData(ctx, "currentUrl", template.URL(ctx.Request().URL.Path))
|
|
|
|
|
2023-03-18 15:18:24 +00:00
|
|
|
nbCommits, err := gist.NbCommits()
|
2023-03-14 15:22:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return errorRes(500, "Error fetching number of commits", err)
|
|
|
|
}
|
|
|
|
setData(ctx, "nbCommits", nbCommits)
|
|
|
|
|
|
|
|
if currUser := getUserLogged(ctx); currUser != nil {
|
2023-03-17 13:56:39 +00:00
|
|
|
hasLiked, err := currUser.HasLiked(gist)
|
2023-03-14 15:22:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return errorRes(500, "Cannot get user like status", err)
|
|
|
|
}
|
|
|
|
setData(ctx, "hasLiked", hasLiked)
|
|
|
|
}
|
|
|
|
|
|
|
|
return next(ctx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func allGists(ctx echo.Context) error {
|
|
|
|
var err error
|
2023-03-20 12:30:25 +00:00
|
|
|
fromUserStr := ctx.Param("user")
|
|
|
|
|
2023-03-14 15:22:52 +00:00
|
|
|
userLogged := getUserLogged(ctx)
|
|
|
|
|
|
|
|
pageInt := getPage(ctx)
|
|
|
|
|
|
|
|
sort := "created"
|
|
|
|
order := "desc"
|
|
|
|
orderText := "Recently"
|
|
|
|
|
|
|
|
if ctx.QueryParam("sort") == "updated" {
|
|
|
|
sort = "updated"
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.QueryParam("order") == "asc" {
|
|
|
|
order = "asc"
|
|
|
|
orderText = "Least recently"
|
|
|
|
}
|
|
|
|
|
|
|
|
setData(ctx, "sort", sort)
|
|
|
|
setData(ctx, "order", orderText)
|
|
|
|
|
|
|
|
var gists []*models.Gist
|
|
|
|
var currentUserId uint
|
|
|
|
if userLogged != nil {
|
|
|
|
currentUserId = userLogged.ID
|
|
|
|
} else {
|
|
|
|
currentUserId = 0
|
|
|
|
}
|
2023-03-20 12:30:25 +00:00
|
|
|
if fromUserStr == "" {
|
2023-03-14 15:22:52 +00:00
|
|
|
setData(ctx, "htmlTitle", "All gists")
|
2023-03-20 12:30:25 +00:00
|
|
|
fromUserStr = "all"
|
2023-03-14 15:22:52 +00:00
|
|
|
gists, err = models.GetAllGistsForCurrentUser(currentUserId, pageInt-1, sort, order)
|
|
|
|
} else {
|
2023-03-20 12:30:25 +00:00
|
|
|
setData(ctx, "htmlTitle", "All gists from "+fromUserStr)
|
2023-03-14 15:22:52 +00:00
|
|
|
|
2023-04-26 21:13:11 +00:00
|
|
|
var fromUser *models.User
|
|
|
|
fromUser, err = models.GetUserByUsername(fromUserStr)
|
2023-03-20 12:30:25 +00:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
return notFound("User not found")
|
|
|
|
}
|
2023-03-14 15:22:52 +00:00
|
|
|
return errorRes(500, "Error fetching user", err)
|
|
|
|
}
|
2023-03-20 12:30:25 +00:00
|
|
|
setData(ctx, "fromUser", fromUser)
|
2023-03-14 15:22:52 +00:00
|
|
|
|
2023-03-20 12:30:25 +00:00
|
|
|
gists, err = models.GetAllGistsFromUser(fromUserStr, currentUserId, pageInt-1, sort, order)
|
2023-03-14 15:22:52 +00:00
|
|
|
}
|
2023-03-20 12:30:25 +00:00
|
|
|
|
2023-03-14 15:22:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return errorRes(500, "Error fetching gists", err)
|
|
|
|
}
|
|
|
|
|
2023-03-20 12:30:25 +00:00
|
|
|
if err = paginate(ctx, gists, pageInt, 10, "gists", fromUserStr, 2, "&sort="+sort+"&order="+order); err != nil {
|
2023-03-14 15:22:52 +00:00
|
|
|
return errorRes(404, "Page not found", nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
return html(ctx, "all.html")
|
|
|
|
}
|
|
|
|
|
2023-03-17 13:56:39 +00:00
|
|
|
func gistIndex(ctx echo.Context) error {
|
2023-03-14 15:22:52 +00:00
|
|
|
gist := getData(ctx, "gist").(*models.Gist)
|
|
|
|
revision := ctx.Param("revision")
|
|
|
|
|
|
|
|
if revision == "" {
|
|
|
|
revision = "HEAD"
|
|
|
|
}
|
|
|
|
|
2023-03-18 15:18:24 +00:00
|
|
|
files, err := gist.Files(revision)
|
|
|
|
if err != nil {
|
|
|
|
return errorRes(500, "Error fetching files", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(files) == 0 {
|
|
|
|
return notFound("Revision not found")
|
2023-03-14 15:22:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
setData(ctx, "page", "code")
|
|
|
|
setData(ctx, "commit", revision)
|
|
|
|
setData(ctx, "files", files)
|
|
|
|
setData(ctx, "revision", revision)
|
|
|
|
setData(ctx, "htmlTitle", gist.Title)
|
|
|
|
return html(ctx, "gist.html")
|
|
|
|
}
|
|
|
|
|
|
|
|
func revisions(ctx echo.Context) error {
|
|
|
|
gist := getData(ctx, "gist").(*models.Gist)
|
|
|
|
userName := gist.User.Username
|
|
|
|
gistName := gist.Uuid
|
|
|
|
|
|
|
|
pageInt := getPage(ctx)
|
|
|
|
|
2023-04-16 14:14:12 +00:00
|
|
|
commits, err := gist.Log((pageInt - 1) * 10)
|
2023-03-18 22:18:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return errorRes(500, "Error fetching commits log", err)
|
2023-03-14 15:22:52 +00:00
|
|
|
}
|
|
|
|
|
2023-03-15 00:26:56 +00:00
|
|
|
if err := paginate(ctx, commits, pageInt, 10, "commits", userName+"/"+gistName+"/revisions", 2); err != nil {
|
2023-03-14 15:22:52 +00:00
|
|
|
return errorRes(404, "Page not found", nil)
|
|
|
|
}
|
|
|
|
|
2023-05-26 07:15:37 +00:00
|
|
|
emailsSet := map[string]struct{}{}
|
|
|
|
for _, commit := range commits {
|
|
|
|
if commit.AuthorEmail == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
emailsSet[strings.ToLower(commit.AuthorEmail)] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
emailsUsers, err := models.GetUsersFromEmails(emailsSet)
|
|
|
|
if err != nil {
|
|
|
|
return errorRes(500, "Error fetching users emails", err)
|
|
|
|
}
|
|
|
|
|
2023-03-14 15:22:52 +00:00
|
|
|
setData(ctx, "page", "revisions")
|
|
|
|
setData(ctx, "revision", "HEAD")
|
2023-05-26 07:15:37 +00:00
|
|
|
setData(ctx, "emails", emailsUsers)
|
2023-03-14 15:22:52 +00:00
|
|
|
setData(ctx, "htmlTitle", "Revision of "+gist.Title)
|
|
|
|
|
|
|
|
return html(ctx, "revisions.html")
|
|
|
|
}
|
|
|
|
|
|
|
|
func create(ctx echo.Context) error {
|
|
|
|
setData(ctx, "htmlTitle", "Create a new gist")
|
|
|
|
return html(ctx, "create.html")
|
|
|
|
}
|
|
|
|
|
|
|
|
func processCreate(ctx echo.Context) error {
|
|
|
|
isCreate := false
|
|
|
|
if ctx.Request().URL.Path == "/" {
|
|
|
|
isCreate = true
|
|
|
|
}
|
|
|
|
|
|
|
|
err := ctx.Request().ParseForm()
|
|
|
|
if err != nil {
|
|
|
|
return errorRes(400, "Bad request", err)
|
|
|
|
}
|
|
|
|
|
2023-03-17 13:56:39 +00:00
|
|
|
dto := new(models.GistDTO)
|
2023-03-14 15:22:52 +00:00
|
|
|
var gist *models.Gist
|
|
|
|
|
|
|
|
if isCreate {
|
|
|
|
setData(ctx, "htmlTitle", "Create a new gist")
|
|
|
|
} else {
|
|
|
|
gist = getData(ctx, "gist").(*models.Gist)
|
|
|
|
setData(ctx, "htmlTitle", "Edit "+gist.Title)
|
|
|
|
}
|
|
|
|
|
2023-03-17 13:56:39 +00:00
|
|
|
if err := ctx.Bind(dto); err != nil {
|
2023-03-14 15:22:52 +00:00
|
|
|
return errorRes(400, "Cannot bind data", err)
|
|
|
|
}
|
|
|
|
|
2023-03-18 22:23:23 +00:00
|
|
|
dto.Files = make([]models.FileDTO, 0)
|
2023-03-20 12:49:28 +00:00
|
|
|
fileCounter := 0
|
2023-03-14 15:22:52 +00:00
|
|
|
for i := 0; i < len(ctx.Request().PostForm["content"]); i++ {
|
|
|
|
name := ctx.Request().PostForm["name"][i]
|
|
|
|
content := ctx.Request().PostForm["content"][i]
|
|
|
|
|
|
|
|
if name == "" {
|
2023-03-20 12:49:28 +00:00
|
|
|
fileCounter += 1
|
|
|
|
name = "gistfile" + strconv.Itoa(fileCounter) + ".txt"
|
2023-03-14 15:22:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
escapedValue, err := url.QueryUnescape(content)
|
|
|
|
if err != nil {
|
|
|
|
return errorRes(400, "Invalid character unescaped", err)
|
|
|
|
}
|
|
|
|
|
2023-03-18 22:23:23 +00:00
|
|
|
dto.Files = append(dto.Files, models.FileDTO{
|
2023-04-28 18:41:12 +00:00
|
|
|
Filename: strings.Trim(name, " "),
|
2023-03-14 15:22:52 +00:00
|
|
|
Content: escapedValue,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-03-17 13:56:39 +00:00
|
|
|
err = ctx.Validate(dto)
|
2023-03-14 15:22:52 +00:00
|
|
|
if err != nil {
|
|
|
|
addFlash(ctx, validationMessages(&err), "error")
|
|
|
|
if isCreate {
|
|
|
|
return html(ctx, "create.html")
|
|
|
|
} else {
|
2023-03-18 15:18:24 +00:00
|
|
|
files, err := gist.Files("HEAD")
|
2023-03-14 15:22:52 +00:00
|
|
|
if err != nil {
|
2023-03-18 15:18:24 +00:00
|
|
|
return errorRes(500, "Error fetching files", err)
|
2023-03-14 15:22:52 +00:00
|
|
|
}
|
|
|
|
setData(ctx, "files", files)
|
|
|
|
return html(ctx, "edit.html")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-17 13:56:39 +00:00
|
|
|
if isCreate {
|
|
|
|
gist = dto.ToGist()
|
|
|
|
} else {
|
|
|
|
gist = dto.ToExistingGist(gist)
|
|
|
|
}
|
|
|
|
|
|
|
|
user := getUserLogged(ctx)
|
2023-03-18 15:18:24 +00:00
|
|
|
gist.NbFiles = len(dto.Files)
|
2023-03-17 13:56:39 +00:00
|
|
|
|
|
|
|
if isCreate {
|
|
|
|
uuidGist, err := uuid.NewRandom()
|
|
|
|
if err != nil {
|
|
|
|
return errorRes(500, "Error creating an UUID", err)
|
|
|
|
}
|
|
|
|
gist.Uuid = strings.Replace(uuidGist.String(), "-", "", -1)
|
|
|
|
|
|
|
|
gist.UserID = user.ID
|
2023-03-18 15:18:24 +00:00
|
|
|
gist.User = *user
|
2023-03-17 13:56:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if gist.Title == "" {
|
|
|
|
if ctx.Request().PostForm["name"][0] == "" {
|
|
|
|
gist.Title = "gist:" + gist.Uuid
|
|
|
|
} else {
|
|
|
|
gist.Title = ctx.Request().PostForm["name"][0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-18 15:18:24 +00:00
|
|
|
if len(dto.Files) > 0 {
|
|
|
|
split := strings.Split(dto.Files[0].Content, "\n")
|
2023-03-14 15:22:52 +00:00
|
|
|
if len(split) > 10 {
|
|
|
|
gist.Preview = strings.Join(split[:10], "\n")
|
|
|
|
} else {
|
2023-03-18 15:18:24 +00:00
|
|
|
gist.Preview = dto.Files[0].Content
|
2023-03-14 15:22:52 +00:00
|
|
|
}
|
|
|
|
|
2023-03-18 15:18:24 +00:00
|
|
|
gist.PreviewFilename = dto.Files[0].Filename
|
2023-03-14 15:22:52 +00:00
|
|
|
}
|
|
|
|
|
2023-03-18 15:18:24 +00:00
|
|
|
if err = gist.InitRepository(); err != nil {
|
2023-03-14 15:22:52 +00:00
|
|
|
return errorRes(500, "Error creating the repository", err)
|
|
|
|
}
|
|
|
|
|
2023-03-18 15:18:24 +00:00
|
|
|
if err = gist.AddAndCommitFiles(&dto.Files); err != nil {
|
|
|
|
return errorRes(500, "Error adding and commiting files", err)
|
2023-03-14 15:22:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if isCreate {
|
2023-03-17 13:56:39 +00:00
|
|
|
if err = gist.Create(); err != nil {
|
2023-03-14 15:22:52 +00:00
|
|
|
return errorRes(500, "Error creating the gist", err)
|
|
|
|
}
|
|
|
|
} else {
|
2023-03-17 13:56:39 +00:00
|
|
|
if err = gist.Update(); err != nil {
|
2023-03-14 15:22:52 +00:00
|
|
|
return errorRes(500, "Error updating the gist", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return redirect(ctx, "/"+user.Username+"/"+gist.Uuid)
|
|
|
|
}
|
|
|
|
|
|
|
|
func toggleVisibility(ctx echo.Context) error {
|
|
|
|
var gist = getData(ctx, "gist").(*models.Gist)
|
|
|
|
|
|
|
|
gist.Private = !gist.Private
|
2023-03-17 13:56:39 +00:00
|
|
|
if err := gist.Update(); err != nil {
|
2023-03-14 15:22:52 +00:00
|
|
|
return errorRes(500, "Error updating this gist", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
addFlash(ctx, "Gist visibility has been changed", "success")
|
|
|
|
return redirect(ctx, "/"+gist.User.Username+"/"+gist.Uuid)
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteGist(ctx echo.Context) error {
|
|
|
|
var gist = getData(ctx, "gist").(*models.Gist)
|
|
|
|
|
2023-03-18 15:18:24 +00:00
|
|
|
err := gist.DeleteRepository()
|
2023-03-14 15:22:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return errorRes(500, "Error deleting the repository", err)
|
|
|
|
}
|
|
|
|
|
2023-03-17 13:56:39 +00:00
|
|
|
if err := gist.Delete(); err != nil {
|
2023-03-14 15:22:52 +00:00
|
|
|
return errorRes(500, "Error deleting this gist", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
addFlash(ctx, "Gist has been deleted", "success")
|
|
|
|
return redirect(ctx, "/")
|
|
|
|
}
|
|
|
|
|
|
|
|
func like(ctx echo.Context) error {
|
|
|
|
var gist = getData(ctx, "gist").(*models.Gist)
|
|
|
|
currentUser := getUserLogged(ctx)
|
|
|
|
|
2023-03-17 13:56:39 +00:00
|
|
|
hasLiked, err := currentUser.HasLiked(gist)
|
2023-03-14 15:22:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return errorRes(500, "Error checking if user has liked a gist", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if hasLiked {
|
2023-03-17 13:56:39 +00:00
|
|
|
err = gist.RemoveUserLike(getUserLogged(ctx))
|
2023-03-14 15:22:52 +00:00
|
|
|
} else {
|
2023-03-17 13:56:39 +00:00
|
|
|
err = gist.AppendUserLike(getUserLogged(ctx))
|
2023-03-14 15:22:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return errorRes(500, "Error liking/dislking this gist", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
redirectTo := "/" + gist.User.Username + "/" + gist.Uuid
|
|
|
|
if r := ctx.QueryParam("redirecturl"); r != "" {
|
|
|
|
redirectTo = r
|
|
|
|
}
|
|
|
|
return redirect(ctx, redirectTo)
|
|
|
|
}
|
|
|
|
|
2023-03-14 22:26:39 +00:00
|
|
|
func fork(ctx echo.Context) error {
|
|
|
|
var gist = getData(ctx, "gist").(*models.Gist)
|
|
|
|
currentUser := getUserLogged(ctx)
|
|
|
|
|
2023-03-17 13:56:39 +00:00
|
|
|
alreadyForked, err := gist.GetForkParent(currentUser)
|
2023-03-14 22:26:39 +00:00
|
|
|
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
return errorRes(500, "Error checking if gist is already forked", err)
|
|
|
|
}
|
|
|
|
|
2023-03-15 00:01:42 +00:00
|
|
|
if gist.User.ID == currentUser.ID {
|
|
|
|
addFlash(ctx, "Unable to fork own gists", "error")
|
|
|
|
return redirect(ctx, "/"+gist.User.Username+"/"+gist.Uuid)
|
|
|
|
}
|
|
|
|
|
2023-03-14 22:26:39 +00:00
|
|
|
if alreadyForked.ID != 0 {
|
|
|
|
return redirect(ctx, "/"+alreadyForked.User.Username+"/"+alreadyForked.Uuid)
|
|
|
|
}
|
|
|
|
|
|
|
|
uuidGist, err := uuid.NewRandom()
|
|
|
|
if err != nil {
|
|
|
|
return errorRes(500, "Error creating an UUID", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
newGist := &models.Gist{
|
|
|
|
Uuid: strings.Replace(uuidGist.String(), "-", "", -1),
|
|
|
|
Title: gist.Title,
|
|
|
|
Preview: gist.Preview,
|
|
|
|
PreviewFilename: gist.PreviewFilename,
|
|
|
|
Description: gist.Description,
|
|
|
|
Private: gist.Private,
|
|
|
|
UserID: currentUser.ID,
|
|
|
|
ForkedID: gist.ID,
|
2023-03-18 23:52:09 +00:00
|
|
|
NbFiles: gist.NbFiles,
|
2023-03-14 22:26:39 +00:00
|
|
|
}
|
|
|
|
|
2023-03-17 13:56:39 +00:00
|
|
|
if err = newGist.CreateForked(); err != nil {
|
2023-03-14 22:26:39 +00:00
|
|
|
return errorRes(500, "Error forking the gist in database", err)
|
|
|
|
}
|
|
|
|
|
2023-03-18 15:18:24 +00:00
|
|
|
if err = gist.ForkClone(currentUser.Username, newGist.Uuid); err != nil {
|
2023-03-14 22:26:39 +00:00
|
|
|
return errorRes(500, "Error cloning the repository while forking", err)
|
|
|
|
}
|
2023-03-17 13:56:39 +00:00
|
|
|
if err = gist.IncrementForkCount(); err != nil {
|
2023-03-14 22:26:39 +00:00
|
|
|
return errorRes(500, "Error incrementing the fork count", err)
|
|
|
|
}
|
|
|
|
|
2023-03-15 00:01:42 +00:00
|
|
|
addFlash(ctx, "Gist has been forked", "success")
|
|
|
|
|
2023-03-14 22:26:39 +00:00
|
|
|
return redirect(ctx, "/"+currentUser.Username+"/"+newGist.Uuid)
|
|
|
|
}
|
|
|
|
|
2023-03-14 15:22:52 +00:00
|
|
|
func rawFile(ctx echo.Context) error {
|
|
|
|
gist := getData(ctx, "gist").(*models.Gist)
|
2023-03-18 15:18:24 +00:00
|
|
|
file, err := gist.File(ctx.Param("revision"), ctx.Param("file"), false)
|
|
|
|
|
2023-03-14 15:22:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return errorRes(500, "Error getting file content", err)
|
|
|
|
}
|
|
|
|
|
2023-03-18 15:18:24 +00:00
|
|
|
if file == nil {
|
2023-03-14 15:22:52 +00:00
|
|
|
return notFound("File not found")
|
|
|
|
}
|
|
|
|
|
2023-03-18 15:18:24 +00:00
|
|
|
return plainText(ctx, 200, file.Content)
|
2023-03-14 15:22:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func edit(ctx echo.Context) error {
|
|
|
|
var gist = getData(ctx, "gist").(*models.Gist)
|
|
|
|
|
2023-03-18 15:18:24 +00:00
|
|
|
files, err := gist.Files("HEAD")
|
2023-03-14 15:22:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return errorRes(500, "Error fetching files from repository", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
setData(ctx, "files", files)
|
|
|
|
setData(ctx, "htmlTitle", "Edit "+gist.Title)
|
|
|
|
|
|
|
|
return html(ctx, "edit.html")
|
|
|
|
}
|
|
|
|
|
|
|
|
func downloadZip(ctx echo.Context) error {
|
|
|
|
var gist = getData(ctx, "gist").(*models.Gist)
|
|
|
|
var revision = ctx.Param("revision")
|
|
|
|
|
2023-03-18 15:18:24 +00:00
|
|
|
files, err := gist.Files(revision)
|
2023-03-14 15:22:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return errorRes(500, "Error fetching files from repository", err)
|
|
|
|
}
|
2023-03-18 15:18:24 +00:00
|
|
|
if len(files) == 0 {
|
|
|
|
return notFound("No files found in this revision")
|
2023-03-14 15:22:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
zipFile := new(bytes.Buffer)
|
|
|
|
|
|
|
|
zipWriter := zip.NewWriter(zipFile)
|
|
|
|
|
2023-03-18 15:18:24 +00:00
|
|
|
for _, file := range files {
|
2023-03-23 15:27:38 +00:00
|
|
|
fh := &zip.FileHeader{
|
|
|
|
Name: file.Filename,
|
|
|
|
Method: zip.Deflate,
|
|
|
|
}
|
|
|
|
f, err := zipWriter.CreateHeader(fh)
|
2023-03-14 15:22:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return errorRes(500, "Error adding a file the to the zip archive", err)
|
|
|
|
}
|
2023-03-18 15:18:24 +00:00
|
|
|
_, err = f.Write([]byte(file.Content))
|
2023-03-14 15:22:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return errorRes(500, "Error adding file content the to the zip archive", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err = zipWriter.Close()
|
|
|
|
if err != nil {
|
|
|
|
return errorRes(500, "Error closing the zip archive", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Response().Header().Set("Content-Type", "application/zip")
|
|
|
|
ctx.Response().Header().Set("Content-Disposition", "attachment; filename="+gist.Uuid+".zip")
|
|
|
|
ctx.Response().Header().Set("Content-Length", strconv.Itoa(len(zipFile.Bytes())))
|
|
|
|
_, err = ctx.Response().Write(zipFile.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return errorRes(500, "Error writing the zip archive", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func likes(ctx echo.Context) error {
|
|
|
|
var gist = getData(ctx, "gist").(*models.Gist)
|
|
|
|
|
|
|
|
pageInt := getPage(ctx)
|
|
|
|
|
2023-03-17 13:56:39 +00:00
|
|
|
likers, err := gist.GetUsersLikes(pageInt - 1)
|
2023-03-14 15:22:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return errorRes(500, "Error getting users who liked this gist", err)
|
|
|
|
}
|
|
|
|
|
2023-03-15 00:26:56 +00:00
|
|
|
if err = paginate(ctx, likers, pageInt, 30, "likers", gist.User.Username+"/"+gist.Uuid+"/likes", 1); err != nil {
|
2023-03-14 15:22:52 +00:00
|
|
|
return errorRes(404, "Page not found", nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
setData(ctx, "htmlTitle", "Likes for "+gist.Title)
|
|
|
|
setData(ctx, "revision", "HEAD")
|
|
|
|
return html(ctx, "likes.html")
|
|
|
|
}
|
2023-03-14 22:26:39 +00:00
|
|
|
|
|
|
|
func forks(ctx echo.Context) error {
|
|
|
|
var gist = getData(ctx, "gist").(*models.Gist)
|
|
|
|
pageInt := getPage(ctx)
|
|
|
|
|
|
|
|
currentUser := getUserLogged(ctx)
|
|
|
|
var fromUserID uint = 0
|
|
|
|
if currentUser != nil {
|
|
|
|
fromUserID = currentUser.ID
|
|
|
|
}
|
|
|
|
|
2023-03-17 13:56:39 +00:00
|
|
|
forks, err := gist.GetForks(fromUserID, pageInt-1)
|
2023-03-14 22:26:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return errorRes(500, "Error getting users who liked this gist", err)
|
|
|
|
}
|
|
|
|
|
2023-03-15 00:26:56 +00:00
|
|
|
if err = paginate(ctx, forks, pageInt, 30, "forks", gist.User.Username+"/"+gist.Uuid+"/forks", 2); err != nil {
|
2023-03-14 22:26:39 +00:00
|
|
|
return errorRes(404, "Page not found", nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
setData(ctx, "htmlTitle", "Forks for "+gist.Title)
|
|
|
|
setData(ctx, "revision", "HEAD")
|
|
|
|
return html(ctx, "forks.html")
|
|
|
|
}
|