/
get_backup.go
55 lines (43 loc) · 1.33 KB
/
get_backup.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
package main
import (
"log"
"net/http"
"github.com/gin-gonic/gin"
"github.com/joeig/eee-safe/pkg/storage"
"github.com/joeig/eee-safe/pkg/threema"
)
// GetBackupPathParams contains path parameters for GetBackupHandler.
type GetBackupPathParams struct {
ThreemaSafeBackupID string `uri:"threemaSafeBackupID" binding:"required,hexadecimal,len=64"`
}
// GetBackupHandler Gin route
func (a *AppContext) GetBackupHandler(c *gin.Context) {
var pathParams GetBackupPathParams
if err := c.ShouldBindUri(&pathParams); err != nil {
log.Println(err)
c.Data(http.StatusBadRequest, "", []byte{})
return
}
threemaSafeBackupID, err := threema.ConvertToBackupID(pathParams.ThreemaSafeBackupID)
if err != nil {
log.Println(err)
c.Data(http.StatusBadRequest, "", []byte{})
return
}
backup, err := a.StorageBackend.GetBackup(c.Request.Context(), threemaSafeBackupID)
if err != nil {
log.Println(err)
switch err.(type) {
case *storage.ErrBackupIDNotFound:
c.Data(http.StatusNotFound, "", []byte{})
default:
c.Data(http.StatusInternalServerError, "", []byte{})
}
return
}
if !backup.CreationTime.IsZero() {
c.Header("Last-Modified", backup.CreationTime.Format(http.TimeFormat))
}
c.Header("Cache-Control", "no-cache, no-store, must-revalidate")
c.Data(http.StatusOK, "application/octet-stream", backup.EncryptedBackup)
}