api.go 10.5 KB
Newer Older
Lukas Böhm's avatar
Lukas Böhm committed
1
2
3
package main

import (
Lukas Böhm's avatar
Lukas Böhm committed
4
	"archive/zip"
Lukas Böhm's avatar
Lukas Böhm committed
5
	"encoding/json"
Lukas Böhm's avatar
Lukas Böhm committed
6
	"errors"
Lukas Böhm's avatar
Lukas Böhm committed
7
	"fmt"
Lukas Böhm's avatar
Lukas Böhm committed
8
	"github.com/google/uuid"
Lukas Böhm's avatar
Lukas Böhm committed
9
	"github.com/gorilla/mux"
Lukas Böhm's avatar
Lukas Böhm committed
10
	"github.com/rs/cors"
Lukas Böhm's avatar
Lukas Böhm committed
11
	"gorm.io/gorm"
Lukas Böhm's avatar
Lukas Böhm committed
12
	"io"
Lukas Böhm's avatar
Lukas Böhm committed
13
14
15
16
17
18
19
	"io/ioutil"
	"log"
	"net/http"
	"os"
	"path/filepath"
)

Lukas Böhm's avatar
Lukas Böhm committed
20
21
22
23
24
25
26
27
28
29
30
31
32
type HTTPError struct {
	Error   error
	Message string
	Code    int
}

type endpointREST func(http.ResponseWriter, *http.Request) *HTTPError

func (fn endpointREST) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	if e := fn(w, r); e != nil { // e is *HTTPError, not os.Error.
		if e.Code == 401 {
			w.Header().Set("WWW-Authenticate", `Basic realm="Please enter the password"`)
		}
Lukas Böhm's avatar
Lukas Böhm committed
33
		http.Error(w, fmt.Sprintf("%s - %s", (*e).Message, (*e).Error.Error()), (*e).Code)
Lukas Böhm's avatar
Lukas Böhm committed
34
35
	}
}
Lukas Böhm's avatar
Lukas Böhm committed
36
37
38
39

/////////////////////////////////
//////////// routes /////////////
/////////////////////////////////
Lukas Böhm's avatar
Lukas Böhm committed
40
func AllShares(w http.ResponseWriter, _ *http.Request) *HTTPError {
Lukas Böhm's avatar
a    
Lukas Böhm committed
41
	fmt.Println("AllShares")
Lukas Böhm's avatar
Lukas Böhm committed
42
43
	db, err := GetDatabase()
	if err != nil {
Lukas Böhm's avatar
Lukas Böhm committed
44
		return &HTTPError{err, "Can't get database", 500}
Lukas Böhm's avatar
Lukas Böhm committed
45
	}
Lukas Böhm's avatar
Lukas Böhm committed
46
	var shares []Share
Lukas Böhm's avatar
Lukas Böhm committed
47
	err = db.Where("is_public = ? AND is_temporary = 0", 1).Find(&shares).Error
Lukas Böhm's avatar
Lukas Böhm committed
48
	if err != nil {
Lukas Böhm's avatar
Lukas Böhm committed
49
		return &HTTPError{err, "Can't fetch data", 500}
Lukas Böhm's avatar
Lukas Böhm committed
50
	}
Lukas Böhm's avatar
Lukas Böhm committed
51
	return SendJSON(w, shares)
Lukas Böhm's avatar
Lukas Böhm committed
52
53
}

Lukas Böhm's avatar
Lukas Böhm committed
54
func GetShare(w http.ResponseWriter, r *http.Request) *HTTPError {
Lukas Böhm's avatar
Lukas Böhm committed
55
	fmt.Println("Get Share")
Lukas Böhm's avatar
Lukas Böhm committed
56
	vars := mux.Vars(r)
Lukas Böhm's avatar
Lukas Böhm committed
57
58
	shareID, err := uuid.Parse(vars["id"])
	if err != nil {
Lukas Böhm's avatar
Lukas Böhm committed
59
		return &HTTPError{err, "invalid URL param", 400}
Lukas Böhm's avatar
Lukas Böhm committed
60
	}
Lukas Böhm's avatar
Lukas Böhm committed
61

Lukas Böhm's avatar
Lukas Böhm committed
62
63
	db, err := GetDatabase()
	if err != nil {
Lukas Böhm's avatar
Lukas Böhm committed
64
		return &HTTPError{err, "Can't get database", 500}
Lukas Böhm's avatar
Lukas Böhm committed
65
	}
Lukas Böhm's avatar
Lukas Böhm committed
66

Lukas Böhm's avatar
Lukas Böhm committed
67
	var share Share
Lukas Böhm's avatar
Lukas Böhm committed
68
	err = db.Preload("Attachments").Where("ID = ?", shareID).First(&share).Error
Lukas Böhm's avatar
Lukas Böhm committed
69
	if errors.Is(err, gorm.ErrRecordNotFound) {
Lukas Böhm's avatar
Lukas Böhm committed
70
		return &HTTPError{err, "Record not found", 404}
Lukas Böhm's avatar
Lukas Böhm committed
71
72
	}
	if err != nil {
Lukas Böhm's avatar
Lukas Böhm committed
73
		return &HTTPError{err, "Can't fetch data", 500}
Lukas Böhm's avatar
Lukas Böhm committed
74
75
	}
	if share.IsTemporary == true {
Lukas Böhm's avatar
Lukas Böhm committed
76
		return &HTTPError{errors.New("share is not finalized"), "Share is not finalized", 403}
Lukas Böhm's avatar
Lukas Böhm committed
77
	}
Lukas Böhm's avatar
Lukas Böhm committed
78

79
	// auth
Lukas Böhm's avatar
Lukas Böhm committed
80
81
82
	if share.Password != "" {
		sid, pass, _ := r.BasicAuth()
		if sid != share.ID.String() {
Lukas Böhm's avatar
Lukas Böhm committed
83
			return &HTTPError{errors.New("Unauthorized"), "wrong username", 401}
Lukas Böhm's avatar
Lukas Böhm committed
84
85
		}
		if pass != share.Password {
Lukas Böhm's avatar
Lukas Böhm committed
86
			return &HTTPError{errors.New("Unauthorized"), "wrong password", 401}
Lukas Böhm's avatar
Lukas Böhm committed
87
		}
Lukas Böhm's avatar
Lukas Böhm committed
88
89
	}

Lukas Böhm's avatar
Lukas Böhm committed
90
	return SendJSON(w, share)
Lukas Böhm's avatar
Lukas Böhm committed
91
92
}

Lukas Böhm's avatar
Lukas Böhm committed
93
func DownloadFile(w http.ResponseWriter, r *http.Request) *HTTPError {
Lukas Böhm's avatar
Lukas Böhm committed
94
95
	fmt.Println("Download file")

Lukas Böhm's avatar
fix api    
Lukas Böhm committed
96
	vars := mux.Vars(r)
Lukas Böhm's avatar
Lukas Böhm committed
97
98
	shareID, err := uuid.Parse(vars["id"])
	if err != nil {
Lukas Böhm's avatar
Lukas Böhm committed
99
		return &HTTPError{err, "invalid URL param", 400}
Lukas Böhm's avatar
Lukas Böhm committed
100
	}
Lukas Böhm's avatar
Lukas Böhm committed
101
	attID, err := uuid.Parse(vars["att"])
Lukas Böhm's avatar
Lukas Böhm committed
102
	if err != nil {
Lukas Böhm's avatar
Lukas Böhm committed
103
		return &HTTPError{err, "invalid URL param", 400}
Lukas Böhm's avatar
Lukas Böhm committed
104
105
	}

Lukas Böhm's avatar
Lukas Böhm committed
106
107
	db, err := GetDatabase()
	if err != nil {
Lukas Böhm's avatar
Lukas Böhm committed
108
		return &HTTPError{err, "Can't get database", 500}
Lukas Böhm's avatar
Lukas Böhm committed
109
	}
Lukas Böhm's avatar
Lukas Böhm committed
110
111

	var att Attachment
Lukas Böhm's avatar
Lukas Böhm committed
112
	err = db.Where("id = ?", attID.String()).First(&att).Error
Lukas Böhm's avatar
Lukas Böhm committed
113
	if errors.Is(err, gorm.ErrRecordNotFound) {
Lukas Böhm's avatar
Lukas Böhm committed
114
		return &HTTPError{err, "Record not found", 404}
Lukas Böhm's avatar
Lukas Böhm committed
115
116
	}
	if err != nil {
Lukas Böhm's avatar
Lukas Böhm committed
117
		return &HTTPError{err, "Can't fetch data", 500}
Lukas Böhm's avatar
Lukas Böhm committed
118
	}
Lukas Böhm's avatar
Lukas Böhm committed
119

Lukas Böhm's avatar
Lukas Böhm committed
120
	var share Share
Lukas Böhm's avatar
Lukas Böhm committed
121
	err = db.Where("id = ?", att.ShareID.String()).First(&share).Error
Lukas Böhm's avatar
Lukas Böhm committed
122
123
124
	if errors.Is(err, gorm.ErrRecordNotFound) {
		return &HTTPError{err, "Record not found", 404}
	}
Lukas Böhm's avatar
Lukas Böhm committed
125
	if err != nil {
Lukas Böhm's avatar
Lukas Böhm committed
126
		return &HTTPError{err, "Can't fetch data", 500}
Lukas Böhm's avatar
Lukas Böhm committed
127
128
	}

129
	// auth
Lukas Böhm's avatar
Lukas Böhm committed
130
131
132
	if share.Password != "" {
		sid, pass, _ := r.BasicAuth()
		if sid != share.ID.String() {
Lukas Böhm's avatar
Lukas Böhm committed
133
			return &HTTPError{errors.New("Unauthorized"), "wrong username", 401}
Lukas Böhm's avatar
Lukas Böhm committed
134
135
		}
		if pass != share.Password {
Lukas Böhm's avatar
Lukas Böhm committed
136
			return &HTTPError{errors.New("Unauthorized"), "wrong password", 401}
Lukas Böhm's avatar
Lukas Böhm committed
137
		}
138
	}
Lukas Böhm's avatar
Lukas Böhm committed
139

Lukas Böhm's avatar
Lukas Böhm committed
140
	w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s", att.Filename))
Lukas Böhm's avatar
Lukas Böhm committed
141
	http.ServeFile(w, r, filepath.Join(config.mediaDir, "data", shareID.String(), attID.String()))
Lukas Böhm's avatar
Lukas Böhm committed
142
	return nil
Lukas Böhm's avatar
fix api    
Lukas Böhm committed
143
144
}

Lukas Böhm's avatar
Lukas Böhm committed
145
func OpenShare(w http.ResponseWriter, r *http.Request) *HTTPError {
146
	fmt.Println("OpenShare")
Lukas Böhm's avatar
Lukas Böhm committed
147

Lukas Böhm's avatar
Lukas Böhm committed
148
149
	db, err := GetDatabase()
	if err != nil {
Lukas Böhm's avatar
Lukas Böhm committed
150
		return &HTTPError{err, "Can't get database", 500}
Lukas Böhm's avatar
Lukas Böhm committed
151
	}
Lukas Böhm's avatar
Lukas Böhm committed
152

Lukas Böhm's avatar
Lukas Böhm committed
153
	// parse body
154
155
	reqBody, err := ioutil.ReadAll(r.Body)
	if err != nil {
Lukas Böhm's avatar
Lukas Böhm committed
156
		return &HTTPError{err, "Request does not contain a valid body", 400}
157
	}
Lukas Böhm's avatar
Lukas Böhm committed
158
	var newShare Share
Lukas Böhm's avatar
Lukas Böhm committed
159
160
	err = json.Unmarshal(reqBody, &newShare)
	if err != nil {
Lukas Böhm's avatar
Lukas Böhm committed
161
		return &HTTPError{err, "Can't parse body", 400}
Lukas Böhm's avatar
Lukas Böhm committed
162
	}
Lukas Böhm's avatar
Lukas Böhm committed
163
	newShare.Attachments = nil // dont want attachments yet
Lukas Böhm's avatar
Lukas Böhm committed
164

Lukas Böhm's avatar
Lukas Böhm committed
165
	// create temporary db entrie
Lukas Böhm's avatar
Lukas Böhm committed
166
	newShare.IsTemporary = true
167
	err = db.Create(&newShare).Error
Lukas Böhm's avatar
Lukas Böhm committed
168
	if err != nil {
169
		return &HTTPError{err, "Can't create data", 500}
Lukas Böhm's avatar
Lukas Böhm committed
170
	}
Lukas Böhm's avatar
Lukas Böhm committed
171

Lukas Böhm's avatar
Lukas Böhm committed
172
	return SendJSON(w, newShare)
Lukas Böhm's avatar
Lukas Böhm committed
173
174
}

Lukas Böhm's avatar
Lukas Böhm committed
175
func CloseShare(w http.ResponseWriter, r *http.Request) *HTTPError {
176
	fmt.Println("CloseShare")
Lukas Böhm's avatar
Lukas Böhm committed
177

Lukas Böhm's avatar
Lukas Böhm committed
178
	vars := mux.Vars(r)
Lukas Böhm's avatar
Lukas Böhm committed
179
180
	shareID, err := uuid.Parse(vars["id"])
	if err != nil {
Lukas Böhm's avatar
Lukas Böhm committed
181
		return &HTTPError{err, "invalid URL param", 400}
Lukas Böhm's avatar
Lukas Böhm committed
182
183
	}

Lukas Böhm's avatar
Lukas Böhm committed
184
185
	db, err := GetDatabase()
	if err != nil {
Lukas Böhm's avatar
Lukas Böhm committed
186
		return &HTTPError{err, "Can't get database", 500}
Lukas Böhm's avatar
Lukas Böhm committed
187
	}
Lukas Böhm's avatar
Lukas Böhm committed
188

Lukas Böhm's avatar
Lukas Böhm committed
189
190
	// get stuff
	var share Share
Lukas Böhm's avatar
Lukas Böhm committed
191
	err = db.Where("id = ?", shareID.String()).First(&share).Error
Lukas Böhm's avatar
Lukas Böhm committed
192
193
	if errors.Is(err, gorm.ErrRecordNotFound) {
		return &HTTPError{err, "Record not found", 404}
Lukas Böhm's avatar
Lukas Böhm committed
194
	}
Lukas Böhm's avatar
Lukas Böhm committed
195
196
	if err != nil {
		return &HTTPError{err, "Can't fetch data", 500}
Lukas Böhm's avatar
Lukas Böhm committed
197
198
	}

Lukas Böhm's avatar
Lukas Böhm committed
199
	// move files to permanent location
Lukas Böhm's avatar
Lukas Böhm committed
200
201
	oldPath := filepath.Join(config.mediaDir, "temp", shareID.String())
	newPath := filepath.Join(config.mediaDir, "data", shareID.String())
Lukas Böhm's avatar
Lukas Böhm committed
202
203
	err = os.Rename(oldPath, newPath)
	if err != nil {
Lukas Böhm's avatar
Lukas Böhm committed
204
		return &HTTPError{err, "Can't move directory", 500}
Lukas Böhm's avatar
Lukas Böhm committed
205
	}
Lukas Böhm's avatar
Lukas Böhm committed
206

Lukas Böhm's avatar
Lukas Böhm committed
207
208
	// set stuff permanent
	share.IsTemporary = false
Lukas Böhm's avatar
Lukas Böhm committed
209
210
	err = db.Save(&share).Error
	if err != nil {
Lukas Böhm's avatar
Lukas Böhm committed
211
		return &HTTPError{err, "Can't edit data", 500}
Lukas Böhm's avatar
Lukas Böhm committed
212
	}
Lukas Böhm's avatar
Lukas Böhm committed
213

Lukas Böhm's avatar
Lukas Böhm committed
214
215
216
217
218
219
220
221
222
	// background job
	{
		job, err := enqueuer.Enqueue("DeleteShare", nil)
		////deleteIn := time.Now().Sub(*share.Expires)
		//deleteIn := 1
		//job, err := enqueuer.EnqueueIn("DeleteShare", int64(deleteIn), map[string]interface{}{
		//	"ShareID": share.ID,
		//})
		if err != nil {
Lukas Böhm's avatar
Lukas Böhm committed
223
			return &HTTPError{err, "Error creating background job", 500}
Lukas Böhm's avatar
Lukas Böhm committed
224
225
226
227
		}
		PrettyPrint(job)
	}

Lukas Böhm's avatar
Lukas Böhm committed
228
	return SendJSON(w, share)
Lukas Böhm's avatar
Lukas Böhm committed
229
}
Lukas Böhm's avatar
Lukas Böhm committed
230

Lukas Böhm's avatar
Lukas Böhm committed
231
func UploadAttachment(w http.ResponseWriter, r *http.Request) *HTTPError {
232
	fmt.Println("UploadTest")
Lukas Böhm's avatar
Lukas Böhm committed
233

Lukas Böhm's avatar
Lukas Böhm committed
234
	vars := mux.Vars(r)
Lukas Böhm's avatar
Lukas Böhm committed
235
236
	shareID, err := uuid.Parse(vars["id"])
	if err != nil {
Lukas Böhm's avatar
Lukas Böhm committed
237
		return &HTTPError{err, "invalid URL param", 400}
Lukas Böhm's avatar
Lukas Böhm committed
238
239
	}

Lukas Böhm's avatar
Lukas Böhm committed
240
241
	db, err := GetDatabase()
	if err != nil {
Lukas Böhm's avatar
Lukas Böhm committed
242
		return &HTTPError{err, "Can't get database", 500}
Lukas Böhm's avatar
Lukas Böhm committed
243
	}
Lukas Böhm's avatar
Lukas Böhm committed
244

Lukas Böhm's avatar
Lukas Böhm committed
245
246
247
248
249
250
251
252
253
254
	// get share
	var share Share
	err = db.Where("id = ?", shareID.String()).First(&share).Error
	if errors.Is(err, gorm.ErrRecordNotFound) {
		return &HTTPError{err, "Record not found", 404}
	}
	if err != nil {
		return &HTTPError{err, "Can't fetch data", 500}
	}
	if share.IsTemporary != true {
Lukas Böhm's avatar
Lukas Böhm committed
255
		return &HTTPError{errors.New("share is not finalized"), "Can't upload to finalized Shares.", 403}
Lukas Böhm's avatar
Lukas Böhm committed
256
257
	}

Lukas Böhm's avatar
Lukas Böhm committed
258
	// Parse file from body
Lukas Böhm's avatar
Lukas Böhm committed
259
	err = r.ParseMultipartForm(config.chunkSize) // Maximum 10 MB in RAM
Lukas Böhm's avatar
Lukas Böhm committed
260
	if err != nil {
Lukas Böhm's avatar
Lukas Böhm committed
261
		return &HTTPError{err, "Request does not contain a valid body (parsing form)", 400}
Lukas Böhm's avatar
Lukas Böhm committed
262
	}
Lukas Böhm's avatar
Lukas Böhm committed
263
	file, handler, err := r.FormFile("file")
264
	if err != nil {
Lukas Böhm's avatar
Lukas Böhm committed
265
		return &HTTPError{err, "Request does not contain a valid body (parsing file)", 400}
266
	}
Lukas Böhm's avatar
Lukas Böhm committed
267
	defer file.Close()
Lukas Böhm's avatar
Lukas Böhm committed
268
269

	var att Attachment
Lukas Böhm's avatar
Lukas Böhm committed
270
271
272
	{
		// add db entry TODO fehlerbehandlung für die ganze transaction
		db.Begin()
Lukas Böhm's avatar
Lukas Böhm committed
273
		sid, err := uuid.Parse(shareID.String())
Lukas Böhm's avatar
Lukas Böhm committed
274
		if err != nil {
Lukas Böhm's avatar
Lukas Böhm committed
275
			return &HTTPError{err, "foreign key shareID not valid", 500}
Lukas Böhm's avatar
Lukas Böhm committed
276
277
278
279
280
281
282
283
284
		}
		att.ShareID = sid
		att.Filename = handler.Filename
		att.Filesize = handler.Size
		db.Create(&att)

		// save file
		fileBytes, err := ioutil.ReadAll(file)
		if err != nil {
Lukas Böhm's avatar
Lukas Böhm committed
285
			return &HTTPError{err, "cant read file", 500}
Lukas Böhm's avatar
Lukas Böhm committed
286
		}
Lukas Böhm's avatar
Lukas Böhm committed
287
		err = ioutil.WriteFile(filepath.Join(config.mediaDir, "temp", sid.String(), att.ID.String()), fileBytes, os.ModePerm)
Lukas Böhm's avatar
Lukas Böhm committed
288
289
		if err != nil {
			db.Rollback()
Lukas Böhm's avatar
Lukas Böhm committed
290
			return &HTTPError{err, "cant save file", 500}
Lukas Böhm's avatar
Lukas Böhm committed
291
292
		}
		db.Commit()
Lukas Böhm's avatar
Lukas Böhm committed
293
294
	}

Lukas Böhm's avatar
Lukas Böhm committed
295
	return SendJSON(w, att)
Lukas Böhm's avatar
Lukas Böhm committed
296
297
}

Lukas Böhm's avatar
Lukas Böhm committed
298
299
300
301
302
303
func DownloadZip(w http.ResponseWriter, r *http.Request) *HTTPError {
	fmt.Println("DownloadZip")

	vars := mux.Vars(r)
	shareID, err := uuid.Parse(vars["id"])
	if err != nil {
Lukas Böhm's avatar
Lukas Böhm committed
304
		return &HTTPError{err, "invalid URL param", 400}
Lukas Böhm's avatar
Lukas Böhm committed
305
306
307
308
309
310
311
312
313
314
	}

	share, er := RetrieveShare(shareID, true)
	if er != nil {
		return er
	}

	zipWriter := zip.NewWriter(w)
	for _, file := range share.Attachments {
		filePath := filepath.Join(config.mediaDir, "data", file.ShareID.String(), file.ID.String())
Lukas Böhm's avatar
Lukas Böhm committed
315
316
317

		fileToZip, err := os.Open(filePath)
		if err != nil {
Lukas Böhm's avatar
Lukas Böhm committed
318
			return &HTTPError{err, "error opening file", 500}
Lukas Böhm's avatar
Lukas Böhm committed
319
320
321
		}

		info, err := fileToZip.Stat()
Lukas Böhm's avatar
Lukas Böhm committed
322
		if err != nil {
Lukas Böhm's avatar
Lukas Böhm committed
323
			return &HTTPError{err, "error getting file info", 500}
Lukas Böhm's avatar
Lukas Böhm committed
324
325
		}

Lukas Böhm's avatar
Lukas Böhm committed
326
		header, err := zip.FileInfoHeader(info)
Lukas Böhm's avatar
Lukas Böhm committed
327
		if err != nil {
Lukas Böhm's avatar
Lukas Böhm committed
328
			return &HTTPError{err, "error creating file header", 500}
Lukas Böhm's avatar
Lukas Böhm committed
329
		}
Lukas Böhm's avatar
Lukas Böhm committed
330
331
332
333
334

		header.Name = file.Filename
		header.Method = zip.Deflate

		writer, err := zipWriter.CreateHeader(header)
Lukas Böhm's avatar
Lukas Böhm committed
335
		if err != nil {
Lukas Böhm's avatar
Lukas Böhm committed
336
			return &HTTPError{err, "error creating header", 500}
Lukas Böhm's avatar
Lukas Böhm committed
337
		}
Lukas Böhm's avatar
Lukas Böhm committed
338
339
		if _, err := io.Copy(writer, fileToZip); err != nil {
			return &HTTPError{err, "error copying file into zip archive", 500}
Lukas Böhm's avatar
Lukas Böhm committed
340
341
		}
		if err := fileToZip.Close(); err != nil {
Lukas Böhm's avatar
Lukas Böhm committed
342
			return &HTTPError{err, "error closing zip archive", 500}
Lukas Böhm's avatar
Lukas Böhm committed
343
344
		}

Lukas Böhm's avatar
Lukas Böhm committed
345
346
347
	}
	err = zipWriter.Close()
	if err != nil {
Lukas Böhm's avatar
Lukas Böhm committed
348
		return &HTTPError{err, "error when closing zip", 500}
Lukas Böhm's avatar
Lukas Böhm committed
349
350
351
352
353
	}

	return nil
}

Lukas Böhm's avatar
Lukas Böhm committed
354
355
356
/////////////////////////////////
////////// functions ////////////
/////////////////////////////////
Lukas Böhm's avatar
Lukas Böhm committed
357
358
359
360
361
362
363
364
365
func RetrieveShare(shareID uuid.UUID, withAtt bool) (*Share, *HTTPError) {
	db, err := GetDatabase()
	if err != nil {
		return nil, &HTTPError{err, "Can't get database", 500}
	}

	var share Share
	if withAtt == true {
		err = db.Preload("Attachments").Where("ID = ?", shareID).First(&share).Error
Lukas Böhm's avatar
Lukas Böhm committed
366
	} else {
Lukas Böhm's avatar
Lukas Böhm committed
367
368
369
370
371
372
373
374
375
376
		err = db.Where("ID = ?", shareID).First(&share).Error
	}

	if errors.Is(err, gorm.ErrRecordNotFound) {
		return nil, &HTTPError{err, "Record not found", 404}
	}
	if err != nil {
		return nil, &HTTPError{err, "Can't fetch data", 500}
	}
	if share.IsTemporary == true {
Lukas Böhm's avatar
Lukas Böhm committed
377
		return nil, &HTTPError{errors.New("share is not finalized"), "Share is not finalized", 403}
Lukas Böhm's avatar
Lukas Böhm committed
378
379
380
381
	}
	return &share, nil
}

Lukas Böhm's avatar
Lukas Böhm committed
382
func ConfigureRoutes() {
Lukas Böhm's avatar
Lukas Böhm committed
383
	router := mux.NewRouter().StrictSlash(true)
384
	handler := cors.Default().Handler(router)
Lukas Böhm's avatar
Lukas Böhm committed
385

Lukas Böhm's avatar
Lukas Böhm committed
386
387
	router.Handle("/shares", endpointREST(AllShares)).Methods("GET")
	router.Handle("/shares", endpointREST(OpenShare)).Methods("POST")
Lukas Böhm's avatar
Lukas Böhm committed
388

Lukas Böhm's avatar
Lukas Böhm committed
389
390
	router.Handle("/share/{id}", endpointREST(GetShare)).Methods("GET")
	router.Handle("/share/{id}", endpointREST(CloseShare)).Methods("POST")
Lukas Böhm's avatar
Lukas Böhm committed
391

Lukas Böhm's avatar
Lukas Böhm committed
392
	router.Handle("/share/{id}/attachments", endpointREST(UploadAttachment)).Methods("POST")
Lukas Böhm's avatar
Lukas Böhm committed
393

Lukas Böhm's avatar
Lukas Böhm committed
394
	router.Handle("/share/{id}/attachment/{att}", endpointREST(DownloadFile)).Methods("GET")
Lukas Böhm's avatar
Lukas Böhm committed
395
	router.Handle("/share/{id}/zip", endpointREST(DownloadZip)).Methods("GET")
Lukas Böhm's avatar
Lukas Böhm committed
396

Lukas Böhm's avatar
Lukas Böhm committed
397
	log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", config.port), handler))
Lukas Böhm's avatar
Lukas Böhm committed
398
399
}

Lukas Böhm's avatar
Lukas Böhm committed
400
func SendJSON(w http.ResponseWriter, res interface{}) *HTTPError {
Lukas Böhm's avatar
Lukas Böhm committed
401
	w.Header().Set("Content-Type", "application/json")
Lukas Böhm's avatar
Lukas Böhm committed
402
	err := json.NewEncoder(w).Encode(res)
403
404
405
406
	if err != nil {
		return &HTTPError{err, "Can't encode data", 500}
	}
	return nil
Lukas Böhm's avatar
Lukas Böhm committed
407
}