-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin.go
45 lines (36 loc) · 1.28 KB
/
admin.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
package main
import (
"bytes"
"crypto/sha256"
"encoding/base64"
"fmt"
"net/http"
"github.com/google/uuid"
"github.com/labstack/echo/v4"
"github.com/minio/minio-go/v6"
)
func (p *Platform) addRelease(ctx echo.Context) error {
rp := new(ReleasePublish)
if err := ctx.Bind(rp); err != nil {
return ctx.JSON(http.StatusBadRequest, map[string]string{"message": "bad request", "error": err.Error()})
}
data, err := base64.StdEncoding.DecodeString(rp.Data)
if err != nil {
return ctx.JSON(http.StatusBadRequest, map[string]string{"message": "invalid base64"})
}
hasher := sha256.New()
hasher.Write(data)
rp.Release.Hash = fmt.Sprintf("%x", hasher.Sum(nil))
reader := bytes.NewReader(data)
u := uuid.New()
rp.Release.FileName = u.String()
_, err = p.storage.PutObject(p.settings.Spaces.BucketName, u.String(), reader, reader.Size(), minio.PutObjectOptions{ContentType: "application/octet-stream"})
if err != nil {
return ctx.JSON(http.StatusInternalServerError, map[string]string{"message": "error", "error": err.Error()})
}
result := p.db.Create(&rp.Release)
if result.Error != nil {
return ctx.JSON(http.StatusInternalServerError, map[string]string{"message": "error", "error": result.Error.Error()})
}
return ctx.JSON(http.StatusOK, map[string]string{"message": "OK"})
}