forked from readium/readium-lcp-server
/
webpublication.go
345 lines (291 loc) · 8.66 KB
/
webpublication.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// 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 webpublication
import (
"database/sql"
"errors"
"io"
"io/ioutil"
"log"
"mime/multipart"
"os"
"path/filepath"
"strings"
"github.com/fubaydullaev/readium-lcp-server/config"
"github.com/fubaydullaev/readium-lcp-server/encrypt"
apilcp "github.com/fubaydullaev/readium-lcp-server/lcpserver/api"
)
// Publication status
const (
StatusDraft string = "draft"
StatusEncrypting string = "encrypting"
StatusError string = "error"
StatusOk string = "ok"
)
// ErrNotFound error trown when publication is not found
var ErrNotFound = errors.New("Publication not found")
// WebPublication interface for publication db interaction
type WebPublication interface {
Get(id int64) (Publication, error)
GetByUUID(uuid string) (Publication, error)
Add(publication Publication) error
Update(publication Publication) error
Delete(id int64) error
List(page int, pageNum int) func() (Publication, error)
Upload(multipart.File, string, Publication) error
CheckByTitle(title string) (int64, error)
}
// Publication struct defines a publication
type Publication struct {
ID int64 `json:"id"`
UUID string `json:"uuid"`
Status string `json:"status"`
Title string `json:"title,omitempty"`
MasterFilename string `json:"masterFilename,omitempty"`
}
// PublicationManager helper
type PublicationManager struct {
config config.Configuration
db *sql.DB
}
// Get gets a publication by its ID
func (pubManager PublicationManager) Get(id int64) (Publication, error) {
dbGetByID, err := pubManager.db.Prepare("SELECT id, uuid, title, status FROM publication WHERE id = ? LIMIT 1")
if err != nil {
return Publication{}, err
}
defer dbGetByID.Close()
records, _ := dbGetByID.Query(id)
if records.Next() {
var pub Publication
err = records.Scan(
&pub.ID,
&pub.UUID,
&pub.Title,
&pub.Status)
records.Close()
return pub, err
}
return Publication{}, ErrNotFound
}
// GetByUUID returns a publication by its uuid
func (pubManager PublicationManager) GetByUUID(uuid string) (Publication, error) {
dbGetByUUID, err := pubManager.db.Prepare("SELECT id, uuid, title, status FROM publication WHERE uuid = ? LIMIT 1")
if err != nil {
return Publication{}, err
}
defer dbGetByUUID.Close()
records, _ := dbGetByUUID.Query(uuid)
if records.Next() {
var pub Publication
err = records.Scan(
&pub.ID,
&pub.UUID,
&pub.Title,
&pub.Status)
records.Close()
return pub, err
}
return Publication{}, ErrNotFound
}
// CheckByTitle checks if the title of a publication exists or not in the db
func (pubManager PublicationManager) CheckByTitle(title string) (int64, error) {
dbGetByTitle, err := pubManager.db.Prepare("SELECT COUNT(1) FROM publication WHERE title = ?")
if err != nil {
return -1, err
}
defer dbGetByTitle.Close()
records, _ := dbGetByTitle.Query(title)
if records.Next() {
var res int64
err = records.Scan(&res)
records.Close()
// returns 1 or 0
return res, err
}
// only if the db query fails
return -1, ErrNotFound
}
// encryptPublication encrypts a publication, notifies the License Server
// and inserts a record in the database.
func encryptPublication(inputPath string, pub Publication, pubManager PublicationManager) error {
var notification *apilcp.LcpPublication
// encrypt the publication
// FIXME: work on a direct storage of the output file.
outputRepo := pubManager.config.FrontendServer.EncryptedRepository
notification, err := encrypt.ProcessPublication("", inputPath, "", outputRepo, "", "")
if err != nil {
return err
}
// send a notification to the License server
err = encrypt.NotifyLcpServer(
notification,
pubManager.config.LcpServer.PublicBaseUrl,
pubManager.config.LcpUpdateAuth.Username,
pubManager.config.LcpUpdateAuth.Password)
if err != nil {
return err
}
// store the new publication in the db
// the publication uuid is the lcp db content id.
pub.UUID = notification.ContentID
pub.Status = StatusOk
dbAdd, err := pubManager.db.Prepare("INSERT INTO publication (uuid, title, status) VALUES ( ?, ?, ?)")
if err != nil {
return err
}
defer dbAdd.Close()
_, err = dbAdd.Exec(
pub.UUID,
pub.Title,
pub.Status)
return err
}
// Add adds a new publication
// Encrypts a master File and notifies the License server
func (pubManager PublicationManager) Add(pub Publication) error {
// get the path to the master file
inputPath := filepath.Join(
pubManager.config.FrontendServer.MasterRepository, pub.MasterFilename)
if _, err := os.Stat(inputPath); err != nil {
// the master file does not exist
return err
}
// encrypt the publication and send a notification to the License server
err := encryptPublication(inputPath, pub, pubManager)
if err != nil {
return err
}
// delete the master file
err = os.Remove(inputPath)
if err != nil {
return err
}
return nil
}
// Upload creates a new publication, named after a POST form parameter.
// Encrypts a master File and notifies the License server
func (pubManager PublicationManager) Upload(file multipart.File, extension string, pub Publication) error {
// create a temp file in the default directory
tmpfile, err := ioutil.TempFile("", "uploaded-*"+extension)
if err != nil {
return err
}
defer os.Remove(tmpfile.Name())
// copy the request payload to the temp file
if _, err = io.Copy(tmpfile, file); err != nil {
return err
}
// close the temp file
if err = tmpfile.Close(); err != nil {
return err
}
// encrypt the publication and send a notification to the License server
return encryptPublication(tmpfile.Name(), pub, pubManager)
}
// Update updates a publication
// Only the title is updated
func (pubManager PublicationManager) Update(pub Publication) error {
dbUpdate, err := pubManager.db.Prepare("UPDATE publication SET title=?, status=? WHERE id = ?")
if err != nil {
return err
}
defer dbUpdate.Close()
_, err = dbUpdate.Exec(
pub.Title,
pub.Status,
pub.ID)
if err != nil {
return err
}
return err
}
// Delete deletes a publication, selected by its numeric id
func (pubManager PublicationManager) Delete(id int64) error {
var title string
dbGetMasterFile, err := pubManager.db.Prepare("SELECT title FROM publication WHERE id = ?")
if err != nil {
return err
}
defer dbGetMasterFile.Close()
result, err := dbGetMasterFile.Query(id)
if err != nil {
return err
}
if result.Next() {
err = result.Scan(&title)
if err != nil {
return err
}
}
result.Close()
// delete all purchases relative to this publication
delPurchases, err := pubManager.db.Prepare(`DELETE FROM purchase WHERE publication_id=?`)
if err != nil {
return err
}
defer delPurchases.Close()
if _, err := delPurchases.Exec(id); err != nil {
return err
}
// delete the publication
dbDelete, err := pubManager.db.Prepare("DELETE FROM publication WHERE id = ?")
if err != nil {
return err
}
defer dbDelete.Close()
_, err = dbDelete.Exec(id)
return err
}
// List lists publications within a given range
// Parameters: page = number of items per page; pageNum = page offset (0 for the first page)
func (pubManager PublicationManager) List(page int, pageNum int) func() (Publication, error) {
dbList, err := pubManager.db.Prepare("SELECT id, uuid, title, status FROM publication ORDER BY id desc LIMIT ? OFFSET ?")
if err != nil {
return func() (Publication, error) { return Publication{}, err }
}
defer dbList.Close()
records, err := dbList.Query(page, pageNum*page)
if err != nil {
return func() (Publication, error) { return Publication{}, err }
}
return func() (Publication, error) {
var pub Publication
if records.Next() {
err := records.Scan(
&pub.ID,
&pub.UUID,
&pub.Title,
&pub.Status)
if err != nil {
return pub, err
}
} else {
records.Close()
err = ErrNotFound
}
return pub, err
}
}
// Init initializes the publication manager
// Creates the publication db table.
func Init(config config.Configuration, db *sql.DB) (i WebPublication, err error) {
// if sqlite, create the content table in the frontend db if it does not exist
if strings.HasPrefix(config.FrontendServer.Database, "sqlite") {
_, err = db.Exec(tableDef)
if err != nil {
log.Println("Error creating publication table")
return
}
}
i = PublicationManager{config, db}
return
}
const tableDef = "CREATE TABLE IF NOT EXISTS publication (" +
"id integer NOT NULL PRIMARY KEY," +
"uuid varchar(255) NOT NULL," +
"title varchar(255) NOT NULL," +
"status varchar(255) NOT NULL" +
");" +
"CREATE INDEX IF NOT EXISTS uuid_index ON publication (uuid);"