forked from readium/readium-lcp-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
publication.go
264 lines (228 loc) · 7.95 KB
/
publication.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// Copyright 2020 Readium Foundation. All rights reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file exposed on Github (readium) in the project repository.
package staticapi
import (
"encoding/json"
"log"
"net/http"
"path/filepath"
"strconv"
"github.com/gorilla/mux"
"github.com/fubaydullaev/readium-lcp-server/api"
"github.com/fubaydullaev/readium-lcp-server/frontend/webpublication"
"github.com/fubaydullaev/readium-lcp-server/problem"
)
// GetPublications returns a list of publications
func GetPublications(w http.ResponseWriter, r *http.Request, s IServer) {
var page int64
var perPage int64
var err error
if r.FormValue("page") != "" {
page, err = strconv.ParseInt((r).FormValue("page"), 10, 32)
if err != nil {
problem.Error(w, r, problem.Problem{Detail: err.Error()}, http.StatusBadRequest)
return
}
} else {
page = 1
}
if r.FormValue("per_page") != "" {
perPage, err = strconv.ParseInt((r).FormValue("per_page"), 10, 32)
if err != nil {
problem.Error(w, r, problem.Problem{Detail: err.Error()}, http.StatusBadRequest)
return
}
} else {
perPage = 100
}
if page > 0 {
page-- //pagenum starting at 0 in code, but user interface starting at 1
}
if page < 0 {
problem.Error(w, r, problem.Problem{Detail: "page must be positive integer"}, http.StatusBadRequest)
return
}
pubs := make([]webpublication.Publication, 0)
fn := s.PublicationAPI().List(int(perPage), int(page))
for it, err := fn(); err == nil; it, err = fn() {
pubs = append(pubs, it)
}
if len(pubs) > 0 {
nextPage := strconv.Itoa(int(page) + 1)
w.Header().Set("Link", "</publications/?page="+nextPage+">; rel=\"next\"; title=\"next\"")
}
if page > 1 {
previousPage := strconv.Itoa(int(page) - 1)
w.Header().Set("Link", "</publications/?page="+previousPage+">; rel=\"previous\"; title=\"previous\"")
}
w.Header().Set("Content-Type", api.ContentType_JSON)
enc := json.NewEncoder(w)
err = enc.Encode(pubs)
if err != nil {
problem.Error(w, r, problem.Problem{Detail: err.Error()}, http.StatusBadRequest)
return
}
}
// GetPublication returns a publication from its numeric id, given as part of the calling url
func GetPublication(w http.ResponseWriter, r *http.Request, s IServer) {
vars := mux.Vars(r)
var id int
var err error
if id, err = strconv.Atoi(vars["id"]); err != nil {
// id is not a number
problem.Error(w, r, problem.Problem{Detail: "The publication id must be an integer"}, http.StatusBadRequest)
}
if pub, err := s.PublicationAPI().Get(int64(id)); err == nil {
enc := json.NewEncoder(w)
if err = enc.Encode(pub); err == nil {
// send a json serialization of the publication
w.Header().Set("Content-Type", api.ContentType_JSON)
return
}
problem.Error(w, r, problem.Problem{Detail: err.Error()}, http.StatusInternalServerError)
} else {
switch err {
case webpublication.ErrNotFound:
{
problem.Error(w, r, problem.Problem{Detail: err.Error()}, http.StatusNotFound)
}
default:
{
problem.Error(w, r, problem.Problem{Detail: err.Error()}, http.StatusInternalServerError)
}
}
}
}
// CheckPublicationByTitle checks if a publication with this title exists
func CheckPublicationByTitle(w http.ResponseWriter, r *http.Request, s IServer) {
var title string
title = r.URL.Query()["title"][0]
log.Println("Check the existence of a publication named " + string(title))
if pub, err := s.PublicationAPI().CheckByTitle(string(title)); err == nil {
enc := json.NewEncoder(w)
if err = enc.Encode(pub); err == nil {
// send a json serialization of the boolean response
w.Header().Set("Content-Type", api.ContentType_JSON)
return
}
problem.Error(w, r, problem.Problem{Detail: err.Error()}, http.StatusInternalServerError)
} else {
switch err {
case webpublication.ErrNotFound:
{
log.Println("No publication stored with name " + string(title))
// problem.Error(w, r, problem.Problem{Detail: err.Error()}, http.StatusNotFound)
}
default:
{
problem.Error(w, r, problem.Problem{Detail: err.Error()}, http.StatusInternalServerError)
}
}
}
}
//DecodeJSONPublication transforms a json string to a User struct
func DecodeJSONPublication(r *http.Request) (webpublication.Publication, error) {
var dec *json.Decoder
if ctype := r.Header["Content-Type"]; len(ctype) > 0 && ctype[0] == api.ContentType_JSON {
dec = json.NewDecoder(r.Body)
}
pub := webpublication.Publication{}
err := dec.Decode(&pub)
return pub, err
}
// CreatePublication creates a publication in the database
func CreatePublication(w http.ResponseWriter, r *http.Request, s IServer) {
var pub webpublication.Publication
var err error
if pub, err = DecodeJSONPublication(r); err != nil {
problem.Error(w, r, problem.Problem{Detail: "incorrect JSON Publication " + err.Error()}, http.StatusBadRequest)
return
}
log.Println("Create a publication named " + pub.Title)
// add publication
if err := s.PublicationAPI().Add(pub); err != nil {
problem.Error(w, r, problem.Problem{Detail: err.Error()}, http.StatusBadRequest)
return
}
// publication added to db
w.WriteHeader(http.StatusCreated)
}
// UploadPublication creates a new publication via a POST request
// This function is called from the frontend test client
func UploadPublication(w http.ResponseWriter, r *http.Request, s IServer) {
var pub webpublication.Publication
// get the title of the publication from the 'title' URL query param
pub.Title = r.URL.Query()["title"][0]
if len(r.URL.Query()["uuid"]) > 0 {
pub.UUID = r.URL.Query()["uuid"][0]
}
log.Println("Upload a publication named " + pub.Title)
// get the file handle
file, header, err := r.FormFile("file")
if err != nil {
log.Println("Error in FormFile")
problem.Error(w, r, problem.Problem{Detail: err.Error()}, http.StatusBadRequest)
return
}
// get the input file extension (will be used to select the proper encryption process)
extension := filepath.Ext(header.Filename)
err = s.PublicationAPI().Upload(file, extension, pub)
if err != nil {
problem.Error(w, r, problem.Problem{Detail: err.Error()}, http.StatusBadRequest)
return
}
w.WriteHeader(http.StatusOK)
}
// UpdatePublication updates an identified publication (id) in the database
func UpdatePublication(w http.ResponseWriter, r *http.Request, s IServer) {
vars := mux.Vars(r)
var id int
var err error
var pub webpublication.Publication
if id, err = strconv.Atoi(vars["id"]); err != nil {
// id is not a number
problem.Error(w, r, problem.Problem{Detail: "Plublication ID must be an integer"}, http.StatusBadRequest)
return
}
// ID is a number, check publication (json)
if pub, err = DecodeJSONPublication(r); err != nil {
problem.Error(w, r, problem.Problem{Detail: err.Error()}, http.StatusBadRequest)
return
}
// publication ok, id is a number, search publication to update
if foundPub, err := s.PublicationAPI().Get(int64(id)); err != nil {
switch err {
case webpublication.ErrNotFound:
problem.Error(w, r, problem.Problem{Detail: err.Error()}, http.StatusNotFound)
default:
problem.Error(w, r, problem.Problem{Detail: err.Error()}, http.StatusInternalServerError)
}
} else {
// publication is found!
if err := s.PublicationAPI().Update(webpublication.Publication{
ID: foundPub.ID,
Title: pub.Title,
Status: foundPub.Status}); err != nil {
//update failed!
problem.Error(w, r, problem.Problem{Detail: err.Error()}, http.StatusInternalServerError)
}
//database update ok
w.WriteHeader(http.StatusOK)
}
}
// DeletePublication removes a publication in the database
func DeletePublication(w http.ResponseWriter, r *http.Request, s IServer) {
vars := mux.Vars(r)
id, err := strconv.ParseInt(vars["id"], 10, 64)
if err != nil {
problem.Error(w, r, problem.Problem{Detail: err.Error()}, http.StatusBadRequest)
return
}
if err := s.PublicationAPI().Delete(id); err != nil {
problem.Error(w, r, problem.Problem{Detail: err.Error()}, http.StatusBadRequest)
return
}
// publication deleted from db
w.WriteHeader(http.StatusOK)
}