2019-04-12 23:29:42 +05:00
|
|
|
package pastes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2022-08-14 16:02:16 +05:00
|
|
|
"github.com/alecthomas/chroma/v2/lexers"
|
2021-06-14 23:48:34 +05:00
|
|
|
"github.com/labstack/echo"
|
2019-10-12 14:18:35 +05:00
|
|
|
"go.dev.pztrn.name/fastpastebin/internal/captcha"
|
2021-06-14 23:48:34 +05:00
|
|
|
"go.dev.pztrn.name/fastpastebin/internal/database/dialects/flatfiles"
|
2019-10-12 14:18:35 +05:00
|
|
|
"go.dev.pztrn.name/fastpastebin/internal/structs"
|
|
|
|
"go.dev.pztrn.name/fastpastebin/internal/templater"
|
2019-04-12 23:29:42 +05:00
|
|
|
)
|
|
|
|
|
2021-06-14 23:48:34 +05:00
|
|
|
const KeepPastesForever = "forever"
|
|
|
|
|
2019-04-12 23:29:42 +05:00
|
|
|
// POST for "/paste/" which will create new paste and redirect to
|
|
|
|
// "/pastes/CREATED_PASTE_ID". This handler will do all the job for
|
|
|
|
// requests comes from browsers via web interface.
|
2021-11-20 22:19:58 +05:00
|
|
|
func pastePOSTWebInterface(ectx echo.Context) error {
|
2019-04-12 23:29:42 +05:00
|
|
|
// We should check if database connection available.
|
2022-08-19 21:52:49 +05:00
|
|
|
dbConn := app.Database.GetDatabaseConnection()
|
|
|
|
if app.Config.Database.Type != flatfiles.FlatFileDialect && dbConn == nil {
|
2022-08-14 15:49:05 +05:00
|
|
|
//nolint:wrapcheck
|
2021-11-20 22:19:58 +05:00
|
|
|
return ectx.Redirect(http.StatusFound, "/database_not_available")
|
2019-04-12 23:29:42 +05:00
|
|
|
}
|
|
|
|
|
2021-11-20 22:19:58 +05:00
|
|
|
params, err := ectx.FormParams()
|
2019-04-12 23:29:42 +05:00
|
|
|
if err != nil {
|
2022-08-19 21:52:49 +05:00
|
|
|
app.Log.Error().Msg("Passed paste form is empty")
|
2019-12-22 01:17:18 +05:00
|
|
|
|
2021-11-20 22:19:58 +05:00
|
|
|
errtpl := templater.GetErrorTemplate(ectx, "Cannot create empty paste")
|
2019-12-22 01:17:18 +05:00
|
|
|
|
2022-08-14 15:49:05 +05:00
|
|
|
//nolint:wrapcheck
|
2021-11-20 22:19:58 +05:00
|
|
|
return ectx.HTML(http.StatusBadRequest, errtpl)
|
2019-04-12 23:29:42 +05:00
|
|
|
}
|
2019-12-22 01:17:18 +05:00
|
|
|
|
2022-08-19 21:52:49 +05:00
|
|
|
app.Log.Debug().Msgf("Received parameters: %+v", params)
|
2019-04-12 23:29:42 +05:00
|
|
|
|
|
|
|
// Do nothing if paste contents is empty.
|
|
|
|
if len(params["paste-contents"][0]) == 0 {
|
2022-08-19 21:52:49 +05:00
|
|
|
app.Log.Debug().Msg("Empty paste submitted, ignoring")
|
2019-12-22 01:17:18 +05:00
|
|
|
|
2021-11-20 22:19:58 +05:00
|
|
|
errtpl := templater.GetErrorTemplate(ectx, "Empty pastes aren't allowed.")
|
2019-12-22 01:17:18 +05:00
|
|
|
|
2022-08-14 15:49:05 +05:00
|
|
|
//nolint:wrapcheck
|
2021-11-20 22:19:58 +05:00
|
|
|
return ectx.HTML(http.StatusBadRequest, errtpl)
|
2019-04-12 23:29:42 +05:00
|
|
|
}
|
|
|
|
|
2021-06-14 23:48:34 +05:00
|
|
|
if !strings.ContainsAny(params["paste-keep-for"][0], "Mmhd") && params["paste-keep-for"][0] != KeepPastesForever {
|
2022-08-19 21:52:49 +05:00
|
|
|
app.Log.Debug().Str("field value", params["paste-keep-for"][0]).Msg("'Keep paste for' field have invalid value")
|
2020-02-29 22:41:06 +05:00
|
|
|
|
2021-11-20 22:19:58 +05:00
|
|
|
errtpl := templater.GetErrorTemplate(ectx, "Invalid 'Paste should be available for' parameter passed. Please do not try to hack us ;).")
|
2019-12-22 01:17:18 +05:00
|
|
|
|
2022-08-14 15:49:05 +05:00
|
|
|
//nolint:wrapcheck
|
2021-11-20 22:19:58 +05:00
|
|
|
return ectx.HTML(http.StatusBadRequest, errtpl)
|
2019-04-12 23:29:42 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Verify captcha.
|
|
|
|
if !captcha.Verify(params["paste-captcha-id"][0], params["paste-captcha-solution"][0]) {
|
2022-08-19 21:52:49 +05:00
|
|
|
app.Log.Debug().Str("captcha ID", params["paste-captcha-id"][0]).Str("captcha solution", params["paste-captcha-solution"][0]).Msg("Invalid captcha solution")
|
2019-12-22 01:17:18 +05:00
|
|
|
|
2021-11-20 22:19:58 +05:00
|
|
|
errtpl := templater.GetErrorTemplate(ectx, "Invalid captcha solution.")
|
2019-12-22 01:17:18 +05:00
|
|
|
|
2022-08-14 15:49:05 +05:00
|
|
|
//nolint:wrapcheck
|
2021-11-20 22:19:58 +05:00
|
|
|
return ectx.HTML(http.StatusBadRequest, errtpl)
|
2019-04-12 23:29:42 +05:00
|
|
|
}
|
|
|
|
|
2022-08-14 15:49:05 +05:00
|
|
|
//nolint:exhaustruct
|
2019-04-12 23:29:42 +05:00
|
|
|
paste := &structs.Paste{
|
|
|
|
Title: params["paste-title"][0],
|
|
|
|
Data: params["paste-contents"][0],
|
|
|
|
Language: params["paste-language"][0],
|
|
|
|
}
|
|
|
|
|
|
|
|
// Paste creation time in UTC.
|
|
|
|
createdAt := time.Now().UTC()
|
|
|
|
paste.CreatedAt = &createdAt
|
|
|
|
|
|
|
|
// Parse "keep for" field.
|
|
|
|
// Defaulting to "forever".
|
|
|
|
keepFor := 0
|
|
|
|
keepForUnit := 0
|
2019-12-22 01:17:18 +05:00
|
|
|
|
2021-06-14 23:48:34 +05:00
|
|
|
if params["paste-keep-for"][0] != KeepPastesForever {
|
2019-04-12 23:29:42 +05:00
|
|
|
keepForUnitRegex := regexp.MustCompile("[Mmhd]")
|
|
|
|
|
|
|
|
keepForRaw := regexInts.FindAllString(params["paste-keep-for"][0], 1)[0]
|
2019-12-22 01:17:18 +05:00
|
|
|
|
2019-04-12 23:29:42 +05:00
|
|
|
var err error
|
2019-12-22 01:17:18 +05:00
|
|
|
|
2019-04-12 23:29:42 +05:00
|
|
|
keepFor, err = strconv.Atoi(keepForRaw)
|
|
|
|
if err != nil {
|
2021-06-14 23:48:34 +05:00
|
|
|
if params["paste-keep-for"][0] == KeepPastesForever {
|
2022-08-19 21:52:49 +05:00
|
|
|
app.Log.Debug().Msg("Keeping paste forever!")
|
2019-12-22 01:17:18 +05:00
|
|
|
|
2019-04-12 23:29:42 +05:00
|
|
|
keepFor = 0
|
|
|
|
} else {
|
2022-08-19 21:52:49 +05:00
|
|
|
app.Log.Debug().Err(err).Msg("Failed to parse 'Keep for' integer")
|
2019-12-22 01:17:18 +05:00
|
|
|
|
2021-11-20 22:19:58 +05:00
|
|
|
errtpl := templater.GetErrorTemplate(ectx, "Invalid 'Paste should be available for' parameter passed. Please do not try to hack us ;).")
|
2019-12-22 01:17:18 +05:00
|
|
|
|
2022-08-14 15:49:05 +05:00
|
|
|
//nolint:wrapcheck
|
2021-11-20 22:19:58 +05:00
|
|
|
return ectx.HTML(http.StatusBadRequest, errtpl)
|
2019-04-12 23:29:42 +05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
keepForUnitRaw := keepForUnitRegex.FindAllString(params["paste-keep-for"][0], 1)[0]
|
2021-01-08 12:00:38 +05:00
|
|
|
keepForUnit = structs.PasteKeepsCorrelation[keepForUnitRaw]
|
2019-04-12 23:29:42 +05:00
|
|
|
}
|
2019-12-22 01:17:18 +05:00
|
|
|
|
2019-04-12 23:29:42 +05:00
|
|
|
paste.KeepFor = keepFor
|
|
|
|
paste.KeepForUnitType = keepForUnit
|
|
|
|
|
|
|
|
// Try to autodetect if it was selected.
|
|
|
|
if params["paste-language"][0] == "autodetect" {
|
|
|
|
lexer := lexers.Analyse(params["paste-language"][0])
|
|
|
|
if lexer != nil {
|
|
|
|
paste.Language = lexer.Config().Name
|
|
|
|
} else {
|
|
|
|
paste.Language = "text"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Private paste?
|
|
|
|
paste.Private = false
|
|
|
|
privateCheckbox, privateCheckboxFound := params["paste-private"]
|
|
|
|
pastePassword, pastePasswordFound := params["paste-password"]
|
2019-12-22 01:17:18 +05:00
|
|
|
|
2019-04-12 23:29:42 +05:00
|
|
|
if privateCheckboxFound && privateCheckbox[0] == "on" || pastePasswordFound && pastePassword[0] != "" {
|
|
|
|
paste.Private = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if pastePassword[0] != "" {
|
2019-10-13 16:26:52 +05:00
|
|
|
_ = paste.CreatePassword(pastePassword[0])
|
2019-04-12 23:29:42 +05:00
|
|
|
}
|
|
|
|
|
2022-08-19 21:52:49 +05:00
|
|
|
pasteID, err2 := app.Database.SavePaste(paste)
|
2019-04-12 23:29:42 +05:00
|
|
|
if err2 != nil {
|
2022-08-19 21:52:49 +05:00
|
|
|
app.Log.Error().Err(err2).Msg("Failed to save paste")
|
2019-12-22 01:17:18 +05:00
|
|
|
|
2021-11-20 22:19:58 +05:00
|
|
|
errtpl := templater.GetErrorTemplate(ectx, "Failed to save paste. Please, try again later.")
|
2019-12-22 01:17:18 +05:00
|
|
|
|
2022-08-14 15:49:05 +05:00
|
|
|
//nolint:wrapcheck
|
2021-11-20 22:19:58 +05:00
|
|
|
return ectx.HTML(http.StatusBadRequest, errtpl)
|
2019-04-12 23:29:42 +05:00
|
|
|
}
|
|
|
|
|
2021-11-20 22:19:58 +05:00
|
|
|
newPasteIDAsString := strconv.FormatInt(pasteID, 10)
|
2022-08-19 21:52:49 +05:00
|
|
|
app.Log.Debug().Msg("Paste saved, URL: /paste/" + newPasteIDAsString)
|
2019-04-12 23:29:42 +05:00
|
|
|
|
|
|
|
// Private pastes have it's timestamp in URL.
|
|
|
|
if paste.Private {
|
2022-08-14 15:49:05 +05:00
|
|
|
//nolint:wrapcheck
|
2021-11-20 22:19:58 +05:00
|
|
|
return ectx.Redirect(http.StatusFound, "/paste/"+newPasteIDAsString+"/"+strconv.FormatInt(paste.CreatedAt.Unix(), 10))
|
2019-04-12 23:29:42 +05:00
|
|
|
}
|
|
|
|
|
2022-08-14 15:49:05 +05:00
|
|
|
//nolint:wrapcheck
|
2021-11-20 22:19:58 +05:00
|
|
|
return ectx.Redirect(http.StatusFound, "/paste/"+newPasteIDAsString)
|
2019-04-12 23:29:42 +05:00
|
|
|
}
|