-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
backups.go
196 lines (167 loc) · 5.89 KB
/
backups.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package controllers
import (
"bytes"
"compress/gzip"
"fmt"
"io"
"strings"
"github.com/bwmarrin/discordgo"
"github.com/gofiber/fiber/v2"
"github.com/sarulabs/di/v2"
_ "github.com/zekroTJA/shinpuru/internal/services/backup/backupmodels"
"github.com/zekroTJA/shinpuru/internal/services/database"
"github.com/zekroTJA/shinpuru/internal/services/permissions"
"github.com/zekroTJA/shinpuru/internal/services/storage"
"github.com/zekroTJA/shinpuru/internal/services/webserver/v1/models"
"github.com/zekroTJA/shinpuru/internal/util/static"
"github.com/zekroTJA/shinpuru/pkg/onetimeauth/v2"
)
type GuildBackupsController struct {
db database.Database
st storage.Storage
ota onetimeauth.OneTimeAuth
}
func (c *GuildBackupsController) Setup(container di.Container, router fiber.Router) {
c.db = container.Get(static.DiDatabase).(database.Database)
c.st = container.Get(static.DiObjectStorage).(storage.Storage)
c.ota = container.Get(static.DiOneTimeAuth).(onetimeauth.OneTimeAuth)
session := container.Get(static.DiDiscordSession).(*discordgo.Session)
pmw := container.Get(static.DiPermissions).(*permissions.Permissions)
router.Get("", c.getBackups)
router.Post("/toggle", pmw.HandleWs(session, "sp.guild.admin.backup"), c.postToggleBackups)
router.Post("/:backupid/download", pmw.HandleWs(session, "sp.guild.admin.backup"), c.postDownloadBackup)
router.Get("/:backupid/download", c.getDownloadBackup)
}
// @Summary Get Guild Backups
// @Description Returns a list of guild backups.
// @Tags Guild Backups
// @Accept json
// @Produce json
// @Param id path string true "The ID of the guild."
// @Success 200 {array} backupmodels.Entry "Wrapped in models.ListResponse"
// @Failure 401 {object} models.Error
// @Failure 404 {object} models.Error
// @Router /guilds/{id}/backups [get]
func (c *GuildBackupsController) getBackups(ctx *fiber.Ctx) error {
guildID := ctx.Params("guildid")
backupEntries, err := c.db.GetBackups(guildID)
if database.IsErrDatabaseNotFound(err) {
return fiber.ErrNotFound
} else if err != nil {
return err
}
return ctx.JSON(models.NewListResponse(backupEntries))
}
// @Summary Obtain Backup Download OTA Key
// @Description Returns an OTA key which is used to download a backup entry.
// @Tags Guild Backups
// @Accept json
// @Produce json
// @Param id path string true "The ID of the guild."
// @Param backupid path string true "The ID of the backup."
// @Success 200 {object} models.AccessTokenResponse
// @Failure 401 {object} models.Error
// @Failure 404 {object} models.Error
// @Router /guilds/{id}/backups/{backupid}/download [post]
func (c *GuildBackupsController) postDownloadBackup(ctx *fiber.Ctx) error {
guildID := ctx.Params("guildid")
backupID := ctx.Params("backupid")
ident := getBackupIdent(guildID, backupID)
token, expires, err := c.ota.GetKey(ident, ctx.Path())
if err != nil {
return err
}
return ctx.JSON(&models.AccessTokenResponse{
Token: token,
Expires: expires,
})
}
// @Summary Download Backup File
// @Description Download a single gziped backup file.
// @Tags Guild Backups
// @Accept json
// @Produce application/gzip
// @Param id path string true "The ID of the guild."
// @Param backupid path string true "The ID of the backup."
// @Param ota_token query string true "The previously obtained OTA token to authorize the download."
// @Success 200 {file} gziped bakcup file
// @Failure 401 {object} models.Error
// @Failure 403 {object} models.Error
// @Failure 404 {object} models.Error
// @Router /guilds/{id}/backups/{backupid}/download [get]
func (c *GuildBackupsController) getDownloadBackup(ctx *fiber.Ctx) error {
guildID := ctx.Params("guildid")
backupID := ctx.Params("backupid")
ident, _ := ctx.Locals("uid").(string)
if rGuildID, rBackupID := decodeBackupIdent(ident); rGuildID != guildID || rBackupID != backupID {
return fiber.ErrForbidden
}
backupEntries, err := c.db.GetBackups(guildID)
if database.IsErrDatabaseNotFound(err) {
return fiber.ErrNotFound
} else if err != nil {
return err
}
var found bool
for _, e := range backupEntries {
if e.FileID == backupID {
found = true
break
}
}
if !found {
return fiber.ErrNotFound
}
f, size, err := c.st.GetObject(static.StorageBucketBackups, backupID)
if err != nil {
return err
}
defer f.Close()
buff := bytes.NewBuffer([]byte{})
zf := gzip.NewWriter(buff)
zf.Name = fmt.Sprintf("backup_%s_%s.json", guildID, backupID)
_, err = io.CopyN(zf, f, size)
if err != nil {
return err
}
zf.Close()
// 24 hours browser caching
ctx.Set("Cache-Control", "public, max-age=86400, immutable")
ctx.Set("Content-Type", "application/gzip")
ctx.Set("Content-Disposition", fmt.Sprintf(`filename="backup_%s_%s.gz"`, guildID, backupID))
return ctx.SendStream(buff)
}
// @Summary Toggle Guild Backup Enable
// @Description Toggle guild backup enable state.
// @Tags Guild Backups
// @Accept json
// @Produce json
// @Param id path string true "The ID of the guild."
// @Param payload body models.EnableStatus true "Enable state payload."
// @Success 200 {object} models.Status
// @Failure 401 {object} models.Error
// @Failure 404 {object} models.Error
// @Router /guilds/{id}/backups/toggle [post]
func (c *GuildBackupsController) postToggleBackups(ctx *fiber.Ctx) error {
guildID := ctx.Params("guildid")
var data models.EnableStatus
if err := ctx.BodyParser(&data); err != nil {
return err
}
if err := c.db.SetGuildBackup(guildID, data.Enabled); err != nil {
return err
}
return ctx.JSON(models.Ok)
}
// --- HELPERS ---
func getBackupIdent(guildID, backupID string) string {
return fmt.Sprintf("%s#%s", guildID, backupID)
}
func decodeBackupIdent(ident string) (guildID, backupID string) {
split := strings.Split(ident, "#")
if len(split) == 2 {
guildID = split[0]
backupID = split[1]
}
return
}