-
Notifications
You must be signed in to change notification settings - Fork 1
/
media.go
49 lines (42 loc) · 1.15 KB
/
media.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
package main
import (
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/decred/base58"
"github.com/rkonfj/lln/config"
"github.com/rkonfj/lln/state"
"github.com/rkonfj/lln/storage"
"github.com/rs/xid"
)
func signRequest(w http.ResponseWriter, r *http.Request) {
user := currentSessionUser(r)
object := r.URL.Query().Get("object")
if len(object) == 0 {
object = base58.Encode(xid.New().Bytes())
}
if state.TodayMediaCountByUser(user) >= config.Conf.Model.Media.CountPerDayLimit {
w.WriteHeader(http.StatusForbidden)
fmt.Fprintf(w, "upload up to %d media files per day", config.Conf.Model.Media.CountPerDayLimit)
return
}
timePrefix := time.Now().Format("20060102")
ns := fmt.Sprintf("%s/%s", timePrefix, user.ID)
url, err := storage.S3SignRequest(ns, object)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, err.Error())
return
}
objectPath := fmt.Sprintf("/%s/%s", ns, object)
if err = state.SaveMedia(user, objectPath); err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, err.Error())
return
}
json.NewEncoder(w).Encode(R{V: map[string]string{
"url": url,
"path": objectPath,
}})
}