-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
hastebin_support.go
72 lines (62 loc) · 1.76 KB
/
hastebin_support.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package v1
import (
"encoding/json"
"time"
"github.com/lus/pasty/internal/config"
"github.com/lus/pasty/internal/shared"
"github.com/lus/pasty/internal/storage"
"github.com/lus/pasty/internal/utils"
"github.com/valyala/fasthttp"
)
// HastebinSupportHandler handles the legacy hastebin requests
func HastebinSupportHandler(ctx *fasthttp.RequestCtx) {
// 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
}
// Define the paste content
var content string
if string(ctx.Request.Header.ContentType()) == "multipart/form-data" {
content = string(ctx.FormValue("data"))
} else {
content = string(ctx.PostBody())
}
// Acquire the paste ID
id, err := storage.AcquireID()
if err != nil {
ctx.SetStatusCode(fasthttp.StatusInternalServerError)
ctx.SetBodyString(err.Error())
return
}
// Create the paste object
paste := &shared.Paste{
ID: id,
Content: content,
Created: time.Now().Unix(),
}
// Set a modification token
if config.Current.ModificationTokens {
paste.ModificationToken = utils.RandomString(config.Current.ModificationTokenCharacters, config.Current.ModificationTokenLength)
err = paste.HashModificationToken()
if err != nil {
ctx.SetStatusCode(fasthttp.StatusInternalServerError)
ctx.SetBodyString(err.Error())
return
}
}
// 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{
"key": paste.ID,
})
ctx.SetBody(jsonData)
}