2020-08-24 00:07:07 +03:00
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2021-04-15 21:15:42 +03:00
|
|
|
"time"
|
2021-04-15 20:26:17 +03:00
|
|
|
|
2021-04-15 21:15:42 +03:00
|
|
|
"github.com/lus/pasty/internal/config"
|
|
|
|
"github.com/lus/pasty/internal/shared"
|
2021-04-15 20:26:17 +03:00
|
|
|
"github.com/lus/pasty/internal/storage"
|
2021-04-15 21:15:42 +03:00
|
|
|
"github.com/lus/pasty/internal/utils"
|
2020-08-24 00:07:07 +03:00
|
|
|
"github.com/valyala/fasthttp"
|
|
|
|
)
|
|
|
|
|
|
|
|
// HastebinSupportHandler handles the legacy hastebin requests
|
|
|
|
func HastebinSupportHandler(ctx *fasthttp.RequestCtx) {
|
2021-05-23 21:55:16 +03:00
|
|
|
// Check content length before reading body into memory
|
|
|
|
if config.Current.LengthCap > 0 &&
|
|
|
|
ctx.Request.Header.ContentLength() > config.Current.LengthCap {
|
|
|
|
ctx.SetStatusCode(fasthttp.StatusBadRequest)
|
|
|
|
ctx.SetBodyString("request body length overflow")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-24 00:07:07 +03:00
|
|
|
// Define the paste content
|
|
|
|
var content string
|
|
|
|
switch string(ctx.Request.Header.ContentType()) {
|
|
|
|
case "text/plain":
|
|
|
|
content = string(ctx.PostBody())
|
|
|
|
case "multipart/form-data":
|
|
|
|
content = string(ctx.FormValue("data"))
|
|
|
|
default:
|
|
|
|
ctx.SetStatusCode(fasthttp.StatusBadRequest)
|
|
|
|
ctx.SetBodyString("invalid content type")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-24 21:22:53 +03:00
|
|
|
// Acquire the paste ID
|
|
|
|
id, err := storage.AcquireID()
|
|
|
|
if err != nil {
|
|
|
|
ctx.SetStatusCode(fasthttp.StatusInternalServerError)
|
|
|
|
ctx.SetBodyString(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-24 00:07:07 +03:00
|
|
|
// Create the paste object
|
2021-04-15 21:15:42 +03:00
|
|
|
paste := &shared.Paste{
|
2021-07-30 22:51:33 +03:00
|
|
|
ID: id,
|
|
|
|
Content: content,
|
|
|
|
Created: time.Now().Unix(),
|
2020-08-24 00:07:07 +03:00
|
|
|
}
|
|
|
|
|
2021-07-22 23:26:21 +03:00
|
|
|
// Set a modification token
|
|
|
|
if config.Current.ModificationTokens {
|
2021-07-24 18:00:29 +03:00
|
|
|
paste.ModificationToken = utils.RandomString(config.Current.ModificationTokenCharacters, config.Current.ModificationTokenLength)
|
2021-04-20 17:38:00 +03:00
|
|
|
|
2021-07-22 23:26:21 +03:00
|
|
|
err = paste.HashModificationToken()
|
2021-04-20 17:38:00 +03:00
|
|
|
if err != nil {
|
|
|
|
ctx.SetStatusCode(fasthttp.StatusInternalServerError)
|
|
|
|
ctx.SetBodyString(err.Error())
|
|
|
|
return
|
|
|
|
}
|
2020-08-24 00:07:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Save the paste
|
|
|
|
err = storage.Current.Save(paste)
|
|
|
|
if err != nil {
|
|
|
|
ctx.SetStatusCode(fasthttp.StatusInternalServerError)
|
|
|
|
ctx.SetBodyString(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Respond with the paste key
|
|
|
|
jsonData, _ := json.Marshal(map[string]string{
|
2020-08-24 21:22:53 +03:00
|
|
|
"key": paste.ID,
|
2020-08-24 00:07:07 +03:00
|
|
|
})
|
|
|
|
ctx.SetBody(jsonData)
|
|
|
|
}
|