forked from SecurityBrewery/catalyst
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.go
191 lines (152 loc) · 4.53 KB
/
file.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
package catalyst
import (
"context"
"errors"
"fmt"
"io"
"log"
"net/http"
"strconv"
"github.com/arangodb/go-driver"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/go-chi/chi/v5"
maut "github.com/jonas-plum/maut/auth"
tusd "github.com/tus/tusd/pkg/handler"
"github.com/tus/tusd/pkg/s3store"
"github.com/sarcb/catalyst-sp24/bus"
"github.com/sarcb/catalyst-sp24/database"
"github.com/sarcb/catalyst-sp24/generated/api"
"github.com/sarcb/catalyst-sp24/generated/model"
"github.com/sarcb/catalyst-sp24/storage"
)
func tusdUpload(db *database.Database, catalystBus *bus.Bus, client *s3.S3, external string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ticketID := chi.URLParam(r, "ticketID")
if ticketID == "" {
api.JSONErrorStatus(w, http.StatusBadRequest, errors.New("ticketID not given"))
return
}
if err := storage.CreateBucket(client, ticketID); err != nil {
api.JSONErrorStatus(w, http.StatusBadRequest, fmt.Errorf("could not create bucket: %w", err))
return
}
store := s3store.New("catalyst-"+ticketID, client)
composer := tusd.NewStoreComposer()
store.UseIn(composer)
handler, err := tusd.NewUnroutedHandler(tusd.Config{
BasePath: external + "/api/files/" + ticketID + "/tusd/",
StoreComposer: composer,
NotifyCompleteUploads: true,
})
if err != nil {
api.JSONErrorStatus(w, http.StatusBadRequest, fmt.Errorf("could not create tusd handler: %w", err))
return
}
userID := "unknown"
user, _, ok := maut.UserFromContext(r.Context())
if ok {
userID = user.ID
}
go func() {
event := <-handler.CompleteUploads
id, err := strconv.ParseInt(ticketID, 10, 64)
if err != nil {
return
}
file := &model.File{Key: event.Upload.Storage["Key"], Name: event.Upload.MetaData["filename"]}
ctx := context.Background()
doc, err := db.AddFile(ctx, id, file)
if err != nil {
log.Println(err)
return
}
catalystBus.RequestChannel.Publish(&bus.RequestMsg{
User: userID,
Function: "LinkFiles",
IDs: []driver.DocumentID{driver.DocumentID(fmt.Sprintf("tickets/%d", doc.ID))},
})
}()
switch r.Method {
case http.MethodHead:
handler.HeadFile(w, r)
case http.MethodPost:
handler.PostFile(w, r)
case http.MethodPatch:
handler.PatchFile(w, r)
default:
api.JSONErrorStatus(w, http.StatusInternalServerError, errors.New("unknown method"))
}
}
}
func upload(db *database.Database, client *s3.S3, uploader *s3manager.Uploader) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ticketID := chi.URLParam(r, "ticketID")
if ticketID == "" {
api.JSONErrorStatus(w, http.StatusBadRequest, errors.New("ticketID not given"))
return
}
file, header, err := r.FormFile("file")
if err != nil {
api.JSONErrorStatus(w, http.StatusBadRequest, err)
return
}
defer file.Close()
if err := storage.CreateBucket(client, ticketID); err != nil {
api.JSONErrorStatus(w, http.StatusBadRequest, fmt.Errorf("could not create bucket: %w", err))
return
}
_, err = uploader.Upload(&s3manager.UploadInput{
Bucket: aws.String("catalyst-" + ticketID),
Key: aws.String(header.Filename),
Body: file,
})
if err != nil {
api.JSONErrorStatus(w, http.StatusBadRequest, err)
return
}
id, err := strconv.ParseInt(ticketID, 10, 64)
if err != nil {
api.JSONErrorStatus(w, http.StatusBadRequest, err)
return
}
_, err = db.AddFile(r.Context(), id, &model.File{
Key: header.Filename,
Name: header.Filename,
})
if err != nil {
api.JSONErrorStatus(w, http.StatusBadRequest, err)
return
}
}
}
func download(downloader *s3manager.Downloader) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ticketID := chi.URLParam(r, "ticketID")
if ticketID == "" {
api.JSONErrorStatus(w, http.StatusBadRequest, errors.New("ticketID not given"))
return
}
key := chi.URLParam(r, "key")
if key == "" {
api.JSONErrorStatus(w, http.StatusBadRequest, errors.New("key not given"))
return
}
buf := sequentialWriter{w}
downloader.Concurrency = 1
_, err := downloader.Download(buf, &s3.GetObjectInput{
Bucket: aws.String("catalyst-" + ticketID),
Key: aws.String(key),
})
if err != nil {
api.JSONErrorStatus(w, http.StatusInternalServerError, err)
}
}
}
type sequentialWriter struct {
w io.Writer
}
func (fw sequentialWriter) WriteAt(p []byte, _ int64) (n int, err error) {
return fw.w.Write(p)
}