forked from algolia/sup3rS3cretMes5age
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
85 lines (69 loc) · 1.81 KB
/
handlers.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
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
"encoding/base64"
"io/ioutil"
"net/http"
"github.com/labstack/echo"
)
type TokenResponse struct {
Token string `json:"token"`
FileToken string `json:"filetoken,omitempty"`
FileName string `json:"filename,omitempty"`
}
type MsgResponse struct {
Msg string `json:"msg"`
}
type SecretHandlers struct {
store SecretMsgStorer
}
func NewSecretHandlers(s SecretMsgStorer) *SecretHandlers {
return &SecretHandlers{s}
}
func (s SecretHandlers) CreateMsgHandler(ctx echo.Context) error {
var tr TokenResponse
// Upload file if any
file, err := ctx.FormFile("file")
if err == nil {
src, err := file.Open()
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err)
}
defer src.Close()
b, err := ioutil.ReadAll(src)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err)
}
if len(b) > 0 {
tr.FileName = file.Filename
encodedFile := base64.StdEncoding.EncodeToString(b)
filetoken, err := s.store.Store(encodedFile)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err)
}
tr.FileToken = filetoken
}
}
// Handle the secret message
msg := ctx.FormValue("msg")
tr.Token, err = s.store.Store(msg)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err)
}
return ctx.JSON(http.StatusOK, tr)
}
func (s SecretHandlers) GetMsgHandler(ctx echo.Context) error {
m, err := s.store.Get(ctx.QueryParam("token"))
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err)
}
r := &MsgResponse{
Msg: m,
}
return ctx.JSON(http.StatusOK, r)
}
func HealthHandler(ctx echo.Context) error {
return ctx.String(http.StatusOK, http.StatusText(http.StatusOK))
}
func redirect(ctx echo.Context) error {
return ctx.Redirect(http.StatusPermanentRedirect, "/msg")
}