2023-03-14 15:22:52 +00:00
|
|
|
package web
|
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/zip"
|
2023-12-21 19:00:04 +00:00
|
|
|
"bufio"
|
2023-03-14 15:22:52 +00:00
|
|
|
"bytes"
|
2023-03-14 22:26:39 +00:00
|
|
|
"errors"
|
2023-12-21 19:00:04 +00:00
|
|
|
"fmt"
|
2023-12-19 01:47:14 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2023-12-27 11:11:02 +00:00
|
|
|
"github.com/thomiceli/opengist/internal/git"
|
2024-05-04 22:24:25 +00:00
|
|
|
"github.com/thomiceli/opengist/internal/i18n"
|
2024-01-04 02:38:15 +00:00
|
|
|
"github.com/thomiceli/opengist/internal/index"
|
2023-12-19 01:47:14 +00:00
|
|
|
"github.com/thomiceli/opengist/internal/render"
|
2024-01-30 00:02:28 +00:00
|
|
|
"github.com/thomiceli/opengist/internal/utils"
|
2023-03-14 15:22:52 +00:00
|
|
|
"html/template"
|
|
|
|
"net/url"
|
2023-12-21 19:00:04 +00:00
|
|
|
"path/filepath"
|
2023-06-21 16:19:17 +00:00
|
|
|
"regexp"
|
2023-03-14 15:22:52 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2023-12-21 19:00:04 +00:00
|
|
|
"time"
|
2023-12-15 01:14:59 +00:00
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
"github.com/thomiceli/opengist/internal/config"
|
|
|
|
"github.com/thomiceli/opengist/internal/db"
|
|
|
|
"gorm.io/gorm"
|
2023-03-14 15:22:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func gistInit(next echo.HandlerFunc) echo.HandlerFunc {
|
|
|
|
return func(ctx echo.Context) error {
|
2023-10-04 16:47:50 +00:00
|
|
|
currUser := getUserLogged(ctx)
|
|
|
|
|
2023-03-14 15:22:52 +00:00
|
|
|
userName := ctx.Param("user")
|
|
|
|
gistName := ctx.Param("gistname")
|
|
|
|
|
2023-12-21 19:00:04 +00:00
|
|
|
switch filepath.Ext(gistName) {
|
|
|
|
case ".js":
|
|
|
|
setData(ctx, "gistpage", "js")
|
|
|
|
gistName = strings.TrimSuffix(gistName, ".js")
|
|
|
|
case ".json":
|
|
|
|
setData(ctx, "gistpage", "json")
|
|
|
|
gistName = strings.TrimSuffix(gistName, ".json")
|
|
|
|
case ".git":
|
|
|
|
setData(ctx, "gistpage", "git")
|
|
|
|
gistName = strings.TrimSuffix(gistName, ".git")
|
|
|
|
}
|
2023-03-14 15:22:52 +00:00
|
|
|
|
2023-09-02 22:30:57 +00:00
|
|
|
gist, err := db.GetGist(userName, gistName)
|
2023-03-14 15:22:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return notFound("Gist not found")
|
|
|
|
}
|
2023-10-04 16:47:50 +00:00
|
|
|
|
2023-12-15 01:14:59 +00:00
|
|
|
if gist.Private == db.PrivateVisibility {
|
2023-10-04 16:47:50 +00:00
|
|
|
if currUser == nil || currUser.ID != gist.UserID {
|
|
|
|
return notFound("Gist not found")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-14 15:22:52 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-04-02 19:27:15 +00:00
|
|
|
baseHttpUrl := getData(ctx, "baseHttpUrl").(string)
|
2023-12-21 19:00:04 +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-12-21 19:00:04 +00:00
|
|
|
setData(ctx, "embedScript", fmt.Sprintf(`<script src="%s"></script>`, baseHttpUrl+"/"+userName+"/"+gistName+".js"))
|
2023-03-14 15:22:52 +00:00
|
|
|
|
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)
|
|
|
|
|
2023-10-04 16:47:50 +00:00
|
|
|
if 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)
|
|
|
|
}
|
|
|
|
|
2023-09-02 01:58:37 +00:00
|
|
|
if gist.Private > 0 {
|
2023-07-17 01:58:45 +00:00
|
|
|
setData(ctx, "NoIndex", true)
|
|
|
|
}
|
|
|
|
|
2023-03-14 15:22:52 +00:00
|
|
|
return next(ctx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-02 01:58:37 +00:00
|
|
|
// gistSoftInit try to load a gist (same as gistInit) but does not return a 404 if the gist is not found
|
|
|
|
// useful for git clients using HTTP to obfuscate the existence of a private gist
|
|
|
|
func gistSoftInit(next echo.HandlerFunc) echo.HandlerFunc {
|
|
|
|
return func(ctx echo.Context) error {
|
|
|
|
userName := ctx.Param("user")
|
|
|
|
gistName := ctx.Param("gistname")
|
|
|
|
|
|
|
|
gistName = strings.TrimSuffix(gistName, ".git")
|
|
|
|
|
2023-09-02 22:30:57 +00:00
|
|
|
gist, _ := db.GetGist(userName, gistName)
|
2023-09-02 01:58:37 +00:00
|
|
|
setData(ctx, "gist", gist)
|
|
|
|
|
|
|
|
return next(ctx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-09 17:39:57 +00:00
|
|
|
// gistNewPushInit has the same behavior as gistSoftInit but create a new gist empty instead
|
|
|
|
func gistNewPushSoftInit(next echo.HandlerFunc) echo.HandlerFunc {
|
|
|
|
return func(c echo.Context) error {
|
|
|
|
setData(c, "gist", new(db.Gist))
|
|
|
|
return next(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-14 15:22:52 +00:00
|
|
|
func allGists(ctx echo.Context) error {
|
|
|
|
var err error
|
2023-06-21 16:19:17 +00:00
|
|
|
var urlPage string
|
2023-03-20 12:30:25 +00:00
|
|
|
|
2023-06-21 16:19:17 +00:00
|
|
|
fromUserStr := ctx.Param("user")
|
2023-03-14 15:22:52 +00:00
|
|
|
userLogged := getUserLogged(ctx)
|
|
|
|
pageInt := getPage(ctx)
|
|
|
|
|
|
|
|
sort := "created"
|
2024-05-04 22:24:25 +00:00
|
|
|
sortText := trH(ctx, "gist.list.sort-by-created")
|
2023-03-14 15:22:52 +00:00
|
|
|
order := "desc"
|
2024-05-04 22:24:25 +00:00
|
|
|
orderText := trH(ctx, "gist.list.order-by-desc")
|
2023-03-14 15:22:52 +00:00
|
|
|
|
|
|
|
if ctx.QueryParam("sort") == "updated" {
|
|
|
|
sort = "updated"
|
2024-05-04 22:24:25 +00:00
|
|
|
sortText = trH(ctx, "gist.list.sort-by-updated")
|
2023-03-14 15:22:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.QueryParam("order") == "asc" {
|
|
|
|
order = "asc"
|
2024-05-04 22:24:25 +00:00
|
|
|
orderText = trH(ctx, "gist.list.order-by-asc")
|
2023-03-14 15:22:52 +00:00
|
|
|
}
|
|
|
|
|
2023-09-22 15:26:09 +00:00
|
|
|
setData(ctx, "sort", sortText)
|
2023-03-14 15:22:52 +00:00
|
|
|
setData(ctx, "order", orderText)
|
|
|
|
|
2023-09-02 22:30:57 +00:00
|
|
|
var gists []*db.Gist
|
2023-03-14 15:22:52 +00:00
|
|
|
var currentUserId uint
|
|
|
|
if userLogged != nil {
|
|
|
|
currentUserId = userLogged.ID
|
|
|
|
} else {
|
|
|
|
currentUserId = 0
|
|
|
|
}
|
2023-06-21 16:19:17 +00:00
|
|
|
|
2023-03-20 12:30:25 +00:00
|
|
|
if fromUserStr == "" {
|
2023-06-21 16:19:17 +00:00
|
|
|
urlctx := ctx.Request().URL.Path
|
|
|
|
if strings.HasSuffix(urlctx, "search") {
|
2024-05-04 22:24:25 +00:00
|
|
|
setData(ctx, "htmlTitle", trH(ctx, "gist.list.search-results"))
|
2023-06-21 16:19:17 +00:00
|
|
|
setData(ctx, "mode", "search")
|
2023-06-23 11:59:07 +00:00
|
|
|
setData(ctx, "searchQuery", ctx.QueryParam("q"))
|
|
|
|
setData(ctx, "searchQueryUrl", template.URL("&q="+ctx.QueryParam("q")))
|
2023-06-21 16:19:17 +00:00
|
|
|
urlPage = "search"
|
2023-09-02 22:30:57 +00:00
|
|
|
gists, err = db.GetAllGistsFromSearch(currentUserId, ctx.QueryParam("q"), pageInt-1, sort, order)
|
2023-06-21 16:19:17 +00:00
|
|
|
} else if strings.HasSuffix(urlctx, "all") {
|
2024-05-04 22:24:25 +00:00
|
|
|
setData(ctx, "htmlTitle", trH(ctx, "gist.list.all"))
|
2023-06-21 16:19:17 +00:00
|
|
|
setData(ctx, "mode", "all")
|
|
|
|
urlPage = "all"
|
2023-09-02 22:30:57 +00:00
|
|
|
gists, err = db.GetAllGistsForCurrentUser(currentUserId, pageInt-1, sort, order)
|
2023-06-21 16:19:17 +00:00
|
|
|
}
|
2023-03-14 15:22:52 +00:00
|
|
|
} else {
|
2023-06-21 16:19:17 +00:00
|
|
|
liked := false
|
|
|
|
forked := false
|
|
|
|
|
|
|
|
liked, err = regexp.MatchString(`/[^/]*/liked`, ctx.Request().URL.Path)
|
|
|
|
if err != nil {
|
|
|
|
return errorRes(500, "Error matching regexp", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
forked, err = regexp.MatchString(`/[^/]*/forked`, ctx.Request().URL.Path)
|
|
|
|
if err != nil {
|
|
|
|
return errorRes(500, "Error matching regexp", err)
|
|
|
|
}
|
2023-03-14 15:22:52 +00:00
|
|
|
|
2023-09-02 22:30:57 +00:00
|
|
|
var fromUser *db.User
|
2023-06-21 16:19:17 +00:00
|
|
|
|
2023-09-02 22:30:57 +00:00
|
|
|
fromUser, err = db.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-09-02 22:30:57 +00:00
|
|
|
if countFromUser, err := db.CountAllGistsFromUser(fromUser.ID, currentUserId); err != nil {
|
2023-06-21 16:19:17 +00:00
|
|
|
return errorRes(500, "Error counting gists", err)
|
|
|
|
} else {
|
|
|
|
setData(ctx, "countFromUser", countFromUser)
|
|
|
|
}
|
|
|
|
|
2023-09-02 22:30:57 +00:00
|
|
|
if countLiked, err := db.CountAllGistsLikedByUser(fromUser.ID, currentUserId); err != nil {
|
2023-06-21 16:19:17 +00:00
|
|
|
return errorRes(500, "Error counting liked gists", err)
|
|
|
|
} else {
|
|
|
|
setData(ctx, "countLiked", countLiked)
|
|
|
|
}
|
|
|
|
|
2023-09-02 22:30:57 +00:00
|
|
|
if countForked, err := db.CountAllGistsForkedByUser(fromUser.ID, currentUserId); err != nil {
|
2023-06-21 16:19:17 +00:00
|
|
|
return errorRes(500, "Error counting forked gists", err)
|
|
|
|
} else {
|
|
|
|
setData(ctx, "countForked", countForked)
|
|
|
|
}
|
|
|
|
|
|
|
|
if liked {
|
|
|
|
urlPage = fromUserStr + "/liked"
|
2024-05-04 22:24:25 +00:00
|
|
|
setData(ctx, "htmlTitle", trH(ctx, "gist.list.all-liked-by", fromUserStr))
|
2023-06-21 16:19:17 +00:00
|
|
|
setData(ctx, "mode", "liked")
|
2023-09-02 22:30:57 +00:00
|
|
|
gists, err = db.GetAllGistsLikedByUser(fromUser.ID, currentUserId, pageInt-1, sort, order)
|
2023-06-21 16:19:17 +00:00
|
|
|
} else if forked {
|
|
|
|
urlPage = fromUserStr + "/forked"
|
2024-05-04 22:24:25 +00:00
|
|
|
setData(ctx, "htmlTitle", trH(ctx, "gist.list.all-forked-by", fromUserStr))
|
2023-06-21 16:19:17 +00:00
|
|
|
setData(ctx, "mode", "forked")
|
2023-09-02 22:30:57 +00:00
|
|
|
gists, err = db.GetAllGistsForkedByUser(fromUser.ID, currentUserId, pageInt-1, sort, order)
|
2023-06-21 16:19:17 +00:00
|
|
|
} else {
|
|
|
|
urlPage = fromUserStr
|
2024-05-04 22:24:25 +00:00
|
|
|
setData(ctx, "htmlTitle", trH(ctx, "gist.list.all-from", fromUserStr))
|
2023-06-21 16:19:17 +00:00
|
|
|
setData(ctx, "mode", "fromUser")
|
2023-09-02 22:30:57 +00:00
|
|
|
gists, err = db.GetAllGistsFromUser(fromUser.ID, currentUserId, pageInt-1, sort, order)
|
2023-06-21 16:19:17 +00:00
|
|
|
}
|
2023-03-14 15:22:52 +00:00
|
|
|
}
|
2023-03-20 12:30:25 +00:00
|
|
|
|
2024-01-04 02:38:15 +00:00
|
|
|
renderedGists := make([]*render.RenderedGist, 0, len(gists))
|
2023-12-19 01:47:14 +00:00
|
|
|
for _, gist := range gists {
|
|
|
|
rendered, err := render.HighlightGistPreview(gist)
|
|
|
|
if err != nil {
|
2024-01-04 02:38:15 +00:00
|
|
|
log.Error().Err(err).Msg("Error rendering gist preview for " + gist.Identifier() + " - " + gist.PreviewFilename)
|
2023-12-19 01:47:14 +00:00
|
|
|
}
|
2024-01-04 02:38:15 +00:00
|
|
|
renderedGists = append(renderedGists, &rendered)
|
2023-12-19 01:47:14 +00:00
|
|
|
}
|
|
|
|
|
2023-03-14 15:22:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return errorRes(500, "Error fetching gists", err)
|
|
|
|
}
|
|
|
|
|
2024-01-04 02:38:15 +00:00
|
|
|
if err = paginate(ctx, renderedGists, pageInt, 10, "gists", fromUserStr, 2, "&sort="+sort+"&order="+order); err != nil {
|
2024-05-04 22:24:25 +00:00
|
|
|
return errorRes(404, tr(ctx, "error.page-not-found"), nil)
|
2023-03-14 15:22:52 +00:00
|
|
|
}
|
|
|
|
|
2023-06-21 16:19:17 +00:00
|
|
|
setData(ctx, "urlPage", urlPage)
|
2023-03-14 15:22:52 +00:00
|
|
|
return html(ctx, "all.html")
|
|
|
|
}
|
|
|
|
|
2024-01-04 02:38:15 +00:00
|
|
|
func search(ctx echo.Context) error {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
content, meta := parseSearchQueryStr(ctx.QueryParam("q"))
|
|
|
|
pageInt := getPage(ctx)
|
|
|
|
|
|
|
|
var currentUserId uint
|
|
|
|
userLogged := getUserLogged(ctx)
|
|
|
|
if userLogged != nil {
|
|
|
|
currentUserId = userLogged.ID
|
|
|
|
} else {
|
|
|
|
currentUserId = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
var visibleGistsIds []uint
|
|
|
|
visibleGistsIds, err = db.GetAllGistsVisibleByUser(currentUserId)
|
|
|
|
if err != nil {
|
|
|
|
return errorRes(500, "Error fetching gists", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
gistsIds, nbHits, langs, err := index.SearchGists(content, index.SearchGistMetadata{
|
|
|
|
Username: meta["user"],
|
|
|
|
Title: meta["title"],
|
|
|
|
Filename: meta["filename"],
|
|
|
|
Extension: meta["extension"],
|
|
|
|
Language: meta["language"],
|
|
|
|
}, visibleGistsIds, pageInt)
|
|
|
|
if err != nil {
|
|
|
|
return errorRes(500, "Error searching gists", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
gists, err := db.GetAllGistsByIds(gistsIds)
|
|
|
|
if err != nil {
|
|
|
|
return errorRes(500, "Error fetching gists", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
renderedGists := make([]*render.RenderedGist, 0, len(gists))
|
|
|
|
for _, gist := range gists {
|
|
|
|
rendered, err := render.HighlightGistPreview(gist)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("Error rendering gist preview for " + gist.Identifier() + " - " + gist.PreviewFilename)
|
|
|
|
}
|
|
|
|
renderedGists = append(renderedGists, &rendered)
|
|
|
|
}
|
|
|
|
|
|
|
|
if pageInt > 1 && len(renderedGists) != 0 {
|
|
|
|
setData(ctx, "prevPage", pageInt-1)
|
|
|
|
}
|
|
|
|
if 10*pageInt < int(nbHits) {
|
|
|
|
setData(ctx, "nextPage", pageInt+1)
|
|
|
|
}
|
2024-05-04 22:24:25 +00:00
|
|
|
setData(ctx, "prevLabel", trH(ctx, "pagination.previous"))
|
|
|
|
setData(ctx, "nextLabel", trH(ctx, "pagination.next"))
|
2024-01-04 02:38:15 +00:00
|
|
|
setData(ctx, "urlPage", "search")
|
|
|
|
setData(ctx, "urlParams", template.URL("&q="+ctx.QueryParam("q")))
|
2024-05-04 22:24:25 +00:00
|
|
|
setData(ctx, "htmlTitle", trH(ctx, "gist.list.search-results"))
|
2024-01-04 02:38:15 +00:00
|
|
|
setData(ctx, "nbHits", nbHits)
|
|
|
|
setData(ctx, "gists", renderedGists)
|
|
|
|
setData(ctx, "langs", langs)
|
|
|
|
setData(ctx, "searchQuery", ctx.QueryParam("q"))
|
|
|
|
return html(ctx, "search.html")
|
|
|
|
}
|
|
|
|
|
2023-03-17 13:56:39 +00:00
|
|
|
func gistIndex(ctx echo.Context) error {
|
2023-12-21 19:00:04 +00:00
|
|
|
if getData(ctx, "gistpage") == "js" {
|
|
|
|
return gistJs(ctx)
|
|
|
|
} else if getData(ctx, "gistpage") == "json" {
|
|
|
|
return gistJson(ctx)
|
|
|
|
}
|
|
|
|
|
2023-09-02 22:30:57 +00:00
|
|
|
gist := getData(ctx, "gist").(*db.Gist)
|
2023-03-14 15:22:52 +00:00
|
|
|
revision := ctx.Param("revision")
|
|
|
|
|
|
|
|
if revision == "" {
|
|
|
|
revision = "HEAD"
|
|
|
|
}
|
|
|
|
|
2023-12-27 11:11:02 +00:00
|
|
|
files, err := gist.Files(revision, true)
|
|
|
|
if _, ok := err.(*git.RevisionNotFoundError); ok {
|
2023-03-18 15:18:24 +00:00
|
|
|
return notFound("Revision not found")
|
2023-12-27 11:11:02 +00:00
|
|
|
} else if err != nil {
|
|
|
|
return errorRes(500, "Error fetching files", err)
|
2023-03-14 15:22:52 +00:00
|
|
|
}
|
|
|
|
|
2024-01-01 22:40:56 +00:00
|
|
|
renderedFiles := render.HighlightFiles(files)
|
2023-12-19 01:47:14 +00:00
|
|
|
|
2023-03-14 15:22:52 +00:00
|
|
|
setData(ctx, "page", "code")
|
|
|
|
setData(ctx, "commit", revision)
|
2023-12-19 01:47:14 +00:00
|
|
|
setData(ctx, "files", renderedFiles)
|
2023-03-14 15:22:52 +00:00
|
|
|
setData(ctx, "revision", revision)
|
|
|
|
setData(ctx, "htmlTitle", gist.Title)
|
|
|
|
return html(ctx, "gist.html")
|
|
|
|
}
|
|
|
|
|
2023-12-21 19:00:04 +00:00
|
|
|
func gistJson(ctx echo.Context) error {
|
|
|
|
gist := getData(ctx, "gist").(*db.Gist)
|
2023-12-27 11:11:02 +00:00
|
|
|
files, err := gist.Files("HEAD", true)
|
2023-12-21 19:00:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return errorRes(500, "Error fetching files", err)
|
|
|
|
}
|
|
|
|
|
2024-01-01 22:40:56 +00:00
|
|
|
renderedFiles := render.HighlightFiles(files)
|
2023-12-21 19:00:04 +00:00
|
|
|
setData(ctx, "files", renderedFiles)
|
|
|
|
|
|
|
|
htmlbuf := bytes.Buffer{}
|
|
|
|
w := bufio.NewWriter(&htmlbuf)
|
|
|
|
if err = ctx.Echo().Renderer.Render(w, "gist_embed.html", dataMap(ctx), ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_ = w.Flush()
|
|
|
|
|
2023-12-26 02:24:04 +00:00
|
|
|
jsUrl, err := url.JoinPath(getData(ctx, "baseHttpUrl").(string), gist.User.Username, gist.Identifier()+".js")
|
2023-12-21 19:00:04 +00:00
|
|
|
if err != nil {
|
2024-01-02 04:11:49 +00:00
|
|
|
return errorRes(500, "Error joining js url", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cssUrl, err := url.JoinPath(getData(ctx, "baseHttpUrl").(string), manifestEntries["embed.css"].File)
|
|
|
|
if err != nil {
|
|
|
|
return errorRes(500, "Error joining css url", err)
|
2023-12-21 19:00:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ctx.JSON(200, map[string]interface{}{
|
|
|
|
"owner": gist.User.Username,
|
2023-12-26 02:24:04 +00:00
|
|
|
"id": gist.Identifier(),
|
|
|
|
"uuid": gist.Uuid,
|
2023-12-21 19:00:04 +00:00
|
|
|
"title": gist.Title,
|
|
|
|
"description": gist.Description,
|
|
|
|
"created_at": time.Unix(gist.CreatedAt, 0).Format(time.RFC3339),
|
|
|
|
"visibility": gist.VisibilityStr(),
|
|
|
|
"files": renderedFiles,
|
|
|
|
"embed": map[string]string{
|
|
|
|
"html": htmlbuf.String(),
|
2024-01-02 04:11:49 +00:00
|
|
|
"css": cssUrl,
|
2023-12-21 19:00:04 +00:00
|
|
|
"js": jsUrl,
|
|
|
|
"js_dark": jsUrl + "?dark",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func gistJs(ctx echo.Context) error {
|
|
|
|
if _, exists := ctx.QueryParams()["dark"]; exists {
|
|
|
|
setData(ctx, "dark", "dark")
|
|
|
|
}
|
|
|
|
|
|
|
|
gist := getData(ctx, "gist").(*db.Gist)
|
2023-12-27 11:11:02 +00:00
|
|
|
files, err := gist.Files("HEAD", true)
|
2023-12-21 19:00:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return errorRes(500, "Error fetching files", err)
|
|
|
|
}
|
|
|
|
|
2024-01-01 22:40:56 +00:00
|
|
|
renderedFiles := render.HighlightFiles(files)
|
2023-12-21 19:00:04 +00:00
|
|
|
setData(ctx, "files", renderedFiles)
|
|
|
|
|
|
|
|
htmlbuf := bytes.Buffer{}
|
|
|
|
w := bufio.NewWriter(&htmlbuf)
|
|
|
|
if err = ctx.Echo().Renderer.Render(w, "gist_embed.html", dataMap(ctx), ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_ = w.Flush()
|
|
|
|
|
2024-01-02 04:11:49 +00:00
|
|
|
cssUrl, err := url.JoinPath(getData(ctx, "baseHttpUrl").(string), manifestEntries["embed.css"].File)
|
|
|
|
if err != nil {
|
|
|
|
return errorRes(500, "Error joining css url", err)
|
|
|
|
}
|
|
|
|
|
2023-12-21 19:00:04 +00:00
|
|
|
js := `document.write('<link rel="stylesheet" href="%s">')
|
|
|
|
document.write('%s')
|
|
|
|
`
|
2024-04-02 15:12:29 +00:00
|
|
|
content := strings.Replace(htmlbuf.String(), `\n`, `\\n`, -1)
|
|
|
|
content = strings.Replace(content, "\n", `\n`, -1)
|
|
|
|
js = fmt.Sprintf(js, cssUrl, content)
|
2023-12-21 19:00:04 +00:00
|
|
|
ctx.Response().Header().Set("Content-Type", "application/javascript")
|
|
|
|
return plainText(ctx, 200, js)
|
|
|
|
}
|
|
|
|
|
2023-03-14 15:22:52 +00:00
|
|
|
func revisions(ctx echo.Context) error {
|
2023-09-02 22:30:57 +00:00
|
|
|
gist := getData(ctx, "gist").(*db.Gist)
|
2023-03-14 15:22:52 +00:00
|
|
|
userName := gist.User.Username
|
2023-12-26 02:24:04 +00:00
|
|
|
gistName := gist.Identifier()
|
2023-03-14 15:22:52 +00:00
|
|
|
|
|
|
|
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 {
|
2024-05-04 22:24:25 +00:00
|
|
|
return errorRes(404, tr(ctx, "error.page-not-found"), nil)
|
2023-03-14 15:22:52 +00:00
|
|
|
}
|
|
|
|
|
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{}{}
|
|
|
|
}
|
|
|
|
|
2023-09-02 22:30:57 +00:00
|
|
|
emailsUsers, err := db.GetUsersFromEmails(emailsSet)
|
2023-05-26 07:15:37 +00:00
|
|
|
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)
|
2024-05-04 22:24:25 +00:00
|
|
|
setData(ctx, "htmlTitle", trH(ctx, "gist.revision-of", gist.Title))
|
2023-03-14 15:22:52 +00:00
|
|
|
|
|
|
|
return html(ctx, "revisions.html")
|
|
|
|
}
|
|
|
|
|
|
|
|
func create(ctx echo.Context) error {
|
2024-05-04 22:24:25 +00:00
|
|
|
setData(ctx, "htmlTitle", trH(ctx, "gist.new.create-a-new-gist"))
|
2023-03-14 15:22:52 +00:00
|
|
|
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 {
|
2024-05-04 22:24:25 +00:00
|
|
|
return errorRes(400, tr(ctx, "error.bad-request"), err)
|
2023-03-14 15:22:52 +00:00
|
|
|
}
|
|
|
|
|
2023-09-02 22:30:57 +00:00
|
|
|
dto := new(db.GistDTO)
|
|
|
|
var gist *db.Gist
|
2023-03-14 15:22:52 +00:00
|
|
|
|
|
|
|
if isCreate {
|
2024-05-04 22:24:25 +00:00
|
|
|
setData(ctx, "htmlTitle", trH(ctx, "gist.new.create-a-new-gist"))
|
2023-03-14 15:22:52 +00:00
|
|
|
} else {
|
2023-09-02 22:30:57 +00:00
|
|
|
gist = getData(ctx, "gist").(*db.Gist)
|
2024-05-04 22:24:25 +00:00
|
|
|
setData(ctx, "htmlTitle", trH(ctx, "gist.edit.edit-gist", gist.Title))
|
2023-03-14 15:22:52 +00:00
|
|
|
}
|
|
|
|
|
2023-03-17 13:56:39 +00:00
|
|
|
if err := ctx.Bind(dto); err != nil {
|
2024-05-04 22:24:25 +00:00
|
|
|
return errorRes(400, tr(ctx, "error.cannot-bind-data"), err)
|
2023-03-14 15:22:52 +00:00
|
|
|
}
|
|
|
|
|
2023-09-02 22:30:57 +00:00
|
|
|
dto.Files = make([]db.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 {
|
2024-05-04 22:24:25 +00:00
|
|
|
return errorRes(400, tr(ctx, "error.invalid-character-unescaped"), err)
|
2023-03-14 15:22:52 +00:00
|
|
|
}
|
|
|
|
|
2023-09-02 22:30:57 +00:00
|
|
|
dto.Files = append(dto.Files, db.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 {
|
2024-05-04 22:24:25 +00:00
|
|
|
addFlash(ctx, utils.ValidationMessages(&err, getData(ctx, "locale").(*i18n.Locale)), "error")
|
2023-03-14 15:22:52 +00:00
|
|
|
if isCreate {
|
|
|
|
return html(ctx, "create.html")
|
|
|
|
} else {
|
2023-12-27 11:11:02 +00:00
|
|
|
files, err := gist.Files("HEAD", false)
|
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 {
|
2023-05-27 18:20:20 +00:00
|
|
|
return errorRes(500, "Error adding and committing 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-04 02:38:15 +00:00
|
|
|
gist.AddInIndex()
|
|
|
|
|
2023-12-26 02:24:04 +00:00
|
|
|
return redirect(ctx, "/"+user.Username+"/"+gist.Identifier())
|
2023-03-14 15:22:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func toggleVisibility(ctx echo.Context) error {
|
2023-12-15 01:14:59 +00:00
|
|
|
gist := getData(ctx, "gist").(*db.Gist)
|
2023-03-14 15:22:52 +00:00
|
|
|
|
2023-09-02 01:58:37 +00:00
|
|
|
gist.Private = (gist.Private + 1) % 3
|
2024-01-02 03:01:45 +00:00
|
|
|
if err := gist.UpdateNoTimestamps(); err != nil {
|
2023-03-14 15:22:52 +00:00
|
|
|
return errorRes(500, "Error updating this gist", err)
|
|
|
|
}
|
|
|
|
|
2024-05-04 22:24:25 +00:00
|
|
|
addFlash(ctx, tr(ctx, "flash.gist.visibility-changed"), "success")
|
2023-12-26 02:24:04 +00:00
|
|
|
return redirect(ctx, "/"+gist.User.Username+"/"+gist.Identifier())
|
2023-03-14 15:22:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func deleteGist(ctx echo.Context) error {
|
2023-12-15 01:14:59 +00:00
|
|
|
gist := getData(ctx, "gist").(*db.Gist)
|
2023-03-14 15:22:52 +00:00
|
|
|
|
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)
|
|
|
|
}
|
2024-01-04 02:38:15 +00:00
|
|
|
gist.RemoveFromIndex()
|
2023-03-14 15:22:52 +00:00
|
|
|
|
2024-05-04 22:24:25 +00:00
|
|
|
addFlash(ctx, tr(ctx, "flash.gist.deleted"), "success")
|
2023-03-14 15:22:52 +00:00
|
|
|
return redirect(ctx, "/")
|
|
|
|
}
|
|
|
|
|
|
|
|
func like(ctx echo.Context) error {
|
2023-12-15 01:14:59 +00:00
|
|
|
gist := getData(ctx, "gist").(*db.Gist)
|
2023-03-14 15:22:52 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-12-26 02:24:04 +00:00
|
|
|
redirectTo := "/" + gist.User.Username + "/" + gist.Identifier()
|
2023-03-14 15:22:52 +00:00
|
|
|
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 {
|
2023-12-15 01:14:59 +00:00
|
|
|
gist := getData(ctx, "gist").(*db.Gist)
|
2023-03-14 22:26:39 +00:00
|
|
|
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 {
|
2024-05-04 22:24:25 +00:00
|
|
|
addFlash(ctx, tr(ctx, "flash.gist.fork-own-gist"), "error")
|
2023-12-26 02:24:04 +00:00
|
|
|
return redirect(ctx, "/"+gist.User.Username+"/"+gist.Identifier())
|
2023-03-15 00:01:42 +00:00
|
|
|
}
|
|
|
|
|
2023-03-14 22:26:39 +00:00
|
|
|
if alreadyForked.ID != 0 {
|
2023-12-26 02:24:04 +00:00
|
|
|
return redirect(ctx, "/"+alreadyForked.User.Username+"/"+alreadyForked.Identifier())
|
2023-03-14 22:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uuidGist, err := uuid.NewRandom()
|
|
|
|
if err != nil {
|
|
|
|
return errorRes(500, "Error creating an UUID", err)
|
|
|
|
}
|
|
|
|
|
2023-09-02 22:30:57 +00:00
|
|
|
newGist := &db.Gist{
|
2023-03-14 22:26:39 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2024-05-04 22:24:25 +00:00
|
|
|
addFlash(ctx, tr(ctx, "flash.gist.forked"), "success")
|
2023-03-15 00:01:42 +00:00
|
|
|
|
2023-12-26 02:24:04 +00:00
|
|
|
return redirect(ctx, "/"+currentUser.Username+"/"+newGist.Identifier())
|
2023-03-14 22:26:39 +00:00
|
|
|
}
|
|
|
|
|
2023-03-14 15:22:52 +00:00
|
|
|
func rawFile(ctx echo.Context) error {
|
2023-09-02 22:30:57 +00:00
|
|
|
gist := getData(ctx, "gist").(*db.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
|
|
|
}
|
|
|
|
|
2023-07-26 13:43:07 +00:00
|
|
|
func downloadFile(ctx echo.Context) error {
|
2023-09-02 22:30:57 +00:00
|
|
|
gist := getData(ctx, "gist").(*db.Gist)
|
2023-07-26 13:43:07 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-03-14 15:22:52 +00:00
|
|
|
func edit(ctx echo.Context) error {
|
2023-12-15 01:14:59 +00:00
|
|
|
gist := getData(ctx, "gist").(*db.Gist)
|
2023-03-14 15:22:52 +00:00
|
|
|
|
2023-12-27 11:11:02 +00:00
|
|
|
files, err := gist.Files("HEAD", false)
|
2023-03-14 15:22:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return errorRes(500, "Error fetching files from repository", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
setData(ctx, "files", files)
|
2024-05-04 22:24:25 +00:00
|
|
|
setData(ctx, "htmlTitle", trH(ctx, "gist.edit.edit-gist", gist.Title))
|
2023-03-14 15:22:52 +00:00
|
|
|
|
|
|
|
return html(ctx, "edit.html")
|
|
|
|
}
|
|
|
|
|
|
|
|
func downloadZip(ctx echo.Context) error {
|
2023-12-15 01:14:59 +00:00
|
|
|
gist := getData(ctx, "gist").(*db.Gist)
|
|
|
|
revision := ctx.Param("revision")
|
2023-03-14 15:22:52 +00:00
|
|
|
|
2023-12-30 22:46:14 +00:00
|
|
|
files, err := gist.Files(revision, false)
|
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")
|
2023-12-26 02:24:04 +00:00
|
|
|
ctx.Response().Header().Set("Content-Disposition", "attachment; filename="+gist.Identifier()+".zip")
|
2023-03-14 15:22:52 +00:00
|
|
|
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 {
|
2023-12-15 01:14:59 +00:00
|
|
|
gist := getData(ctx, "gist").(*db.Gist)
|
2023-03-14 15:22:52 +00:00
|
|
|
|
|
|
|
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-12-26 02:24:04 +00:00
|
|
|
if err = paginate(ctx, likers, pageInt, 30, "likers", gist.User.Username+"/"+gist.Identifier()+"/likes", 1); err != nil {
|
2024-05-04 22:24:25 +00:00
|
|
|
return errorRes(404, tr(ctx, "error.page-not-found"), nil)
|
2023-03-14 15:22:52 +00:00
|
|
|
}
|
|
|
|
|
2024-05-04 22:24:25 +00:00
|
|
|
setData(ctx, "htmlTitle", trH(ctx, "gist.likes.for", gist.Title))
|
2023-03-14 15:22:52 +00:00
|
|
|
setData(ctx, "revision", "HEAD")
|
|
|
|
return html(ctx, "likes.html")
|
|
|
|
}
|
2023-03-14 22:26:39 +00:00
|
|
|
|
|
|
|
func forks(ctx echo.Context) error {
|
2023-12-15 01:14:59 +00:00
|
|
|
gist := getData(ctx, "gist").(*db.Gist)
|
2023-03-14 22:26:39 +00:00
|
|
|
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-12-26 02:24:04 +00:00
|
|
|
if err = paginate(ctx, forks, pageInt, 30, "forks", gist.User.Username+"/"+gist.Identifier()+"/forks", 2); err != nil {
|
2024-05-04 22:24:25 +00:00
|
|
|
return errorRes(404, tr(ctx, "error.page-not-found"), nil)
|
2023-03-14 22:26:39 +00:00
|
|
|
}
|
|
|
|
|
2024-05-04 22:24:25 +00:00
|
|
|
setData(ctx, "htmlTitle", trH(ctx, "gist.forks.for: Forks for %s", gist.Title))
|
2023-03-14 22:26:39 +00:00
|
|
|
setData(ctx, "revision", "HEAD")
|
|
|
|
return html(ctx, "forks.html")
|
|
|
|
}
|
2023-12-26 02:23:47 +00:00
|
|
|
|
|
|
|
func checkbox(ctx echo.Context) error {
|
|
|
|
filename := ctx.FormValue("file")
|
|
|
|
checkboxNb := ctx.FormValue("checkbox")
|
|
|
|
|
|
|
|
i, err := strconv.Atoi(checkboxNb)
|
|
|
|
if err != nil {
|
2024-05-04 22:24:25 +00:00
|
|
|
return errorRes(400, tr(ctx, "error.invalid-number"), nil)
|
2023-12-26 02:23:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gist := getData(ctx, "gist").(*db.Gist)
|
|
|
|
file, err := gist.File("HEAD", filename, false)
|
|
|
|
if err != nil {
|
|
|
|
return errorRes(500, "Error getting file content", err)
|
|
|
|
} else if file == nil {
|
|
|
|
return notFound("File not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
markdown, err := render.Checkbox(file.Content, i)
|
|
|
|
if err != nil {
|
|
|
|
return errorRes(500, "Error checking checkbox", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = gist.AddAndCommitFile(&db.FileDTO{
|
|
|
|
Filename: filename,
|
|
|
|
Content: markdown,
|
|
|
|
}); err != nil {
|
|
|
|
return errorRes(500, "Error adding and committing files", err)
|
|
|
|
}
|
|
|
|
|
2024-01-02 03:01:45 +00:00
|
|
|
if err = gist.UpdatePreviewAndCount(true); err != nil {
|
2023-12-26 02:23:47 +00:00
|
|
|
return errorRes(500, "Error updating the gist", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return plainText(ctx, 200, "ok")
|
|
|
|
}
|
2024-02-24 17:09:23 +00:00
|
|
|
|
|
|
|
func preview(ctx echo.Context) error {
|
|
|
|
content := ctx.FormValue("content")
|
|
|
|
|
|
|
|
previewStr, err := render.MarkdownString(content)
|
|
|
|
if err != nil {
|
|
|
|
return errorRes(500, "Error rendering markdown", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return plainText(ctx, 200, previewStr)
|
|
|
|
}
|