This repository has been archived by the owner on Oct 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
archives.js
277 lines (242 loc) · 8.06 KB
/
archives.js
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
const sse = require('express-server-sent-events')
const {DAT_KEY_REGEX, COHORT_STATE_ACTIVE, NotFoundError, UnauthorizedError, ForbiddenError, NotImplementedError} = require('../const')
const {wait} = require('../helpers')
const lock = require('../lock')
// exported api
// =
module.exports = class ArchivesAPI {
constructor (cloud) {
this.config = cloud.config
this.usersDB = cloud.usersDB
this.archivesDB = cloud.archivesDB
this.activityDB = cloud.activityDB
this.archiver = cloud.archiver
}
async add (req, res) {
// validate session
if (!res.locals.session) throw new UnauthorizedError()
if (!res.locals.session.scopes.includes('user')) throw new ForbiddenError()
// validate & sanitize input
req.checkBody('key').optional().isDatHash()
req.checkBody('url').optional().isDatURL()
req.checkBody('name').optional()
.isDatName().withMessage('Names must only contain characters, numbers, and dashes.')
.isLength({ min: 3, max: 63 }).withMessage('Names must be 3-63 characters long.')
;(await req.getValidationResult()).throw()
if (req.body.url) req.sanitizeBody('url').toDatDomain()
var { key, url, name } = req.body
// only allow one or the other
if ((!key && !url) || (key && url)) {
return res.status(422).json({
message: 'Must provide a key or url',
invalidInputs: true
})
}
if (url) {
key = DAT_KEY_REGEX.exec(url)[1]
}
var release = await Promise.all([lock('users'), lock('archives')])
try {
// fetch user's record
var userRecord = await this.usersDB.getByID(res.locals.session.id)
// check that the user has the available quota
if (this.config.getUserDiskQuotaPct(userRecord) >= 1) {
return res.status(422).json({
message: 'You have exceeded your disk usage',
outOfSpace: true
})
}
if (name && userRecord.archives.find(a => a.name === name)) {
return res.status(422).json({
message: 'There were errors in your submission',
details: {
name: {
msg: `You already have an archive named ${name}. Please select a new name.`
}
}
})
}
// update the records
// TEMPORARY we have to do addHostingUser first, and cancel if that fails
// await Promise.all([
// this.usersDB.addArchive(userRecord.id, key, name),
// this.archivesDB.addHostingUser(key, userRecord.id)
// ])
try {
await this.archivesDB.addHostingUser(key, userRecord.id)
} catch (e) {
return res.status(422).json({
message: 'This archive is already being hosted by someone else'
})
}
await this.usersDB.addArchive(userRecord.id, key, name)
} finally {
release[0]()
release[1]()
}
// record the event
/* dont await */ req.logAnalytics('add-archive', {user: userRecord.id, key, name})
/* dont await */ this.usersDB.updateCohort(userRecord, COHORT_STATE_ACTIVE)
/* dont await */ this.activityDB.writeGlobalEvent({
userid: userRecord.id,
username: userRecord.username,
action: 'add-archive',
params: {key, name}
})
// add to the swarm
this.archiver.loadArchive(key).then(() => {
this.archiver._swarmArchive(key, {upload: true, download: true})
})
// respond
res.status(200).end()
}
async remove (req, res) {
// validate session
if (!res.locals.session) throw new UnauthorizedError()
if (!res.locals.session.scopes.includes('user')) throw new ForbiddenError()
// validate & sanitize input
req.checkBody('key').optional().isDatHash()
req.checkBody('url').optional().isDatURL()
;(await req.getValidationResult()).throw()
if (req.body.url) req.sanitizeBody('url').toDatDomain()
var { key, url } = req.body
// only allow one or the other
if ((!key && !url) || (key && url)) {
return res.status(422).json({
message: 'Must provide a key or url',
invalidInputs: true
})
}
if (url) {
key = DAT_KEY_REGEX.exec(url)[1]
}
var release = await Promise.all([lock('users'), lock('archives')])
try {
// fetch the user
var userRecord = await this.usersDB.getByID(res.locals.session.id)
// find the archive name
var archiveRecord = await this.archivesDB.getExtraByKey(key)
var name = archiveRecord.name
// update the records
await Promise.all([
this.usersDB.removeArchive(res.locals.session.id, key),
this.archivesDB.removeHostingUser(key, res.locals.session.id)
])
} finally {
release[0]()
release[1]()
}
// record the event
/* dont await */ req.logAnalytics('remove-archive', {user: userRecord.id, key, name})
/* dont await */ this.activityDB.writeGlobalEvent({
userid: userRecord.id,
username: userRecord.username,
action: 'del-archive',
params: {key, name}
})
// remove from the swarm
var archive = await this.archivesDB.getByKey(key)
if (!archive.hostingUsers.length) {
/* dont await */ this.archiver.closeArchive(key)
}
// respond
res.status(200).end()
}
async get (req, res) {
if (req.query.view === 'status') {
return this.archiveStatus(req, res)
}
// give info about the archive
// TODO
throw NotImplementedError()
}
async getByName (req, res) {
// validate & sanitize input
req.checkParams('username').isAlphanumeric().isLength({ min: 3, max: 16 })
req.checkParams('archivename').isDatName().isLength({ min: 3, max: 64 })
;(await req.getValidationResult()).throw()
var { username, archivename } = req.params
// lookup user
var userRecord = await this.usersDB.getByUsername(username)
if (!userRecord) throw new NotFoundError()
// lookup archive
const findFn = (DAT_KEY_REGEX.test(archivename))
? a => a.key === archivename
: a => a.name === archivename
var archive = userRecord.archives.find(findFn)
if (!archive) throw new NotFoundError()
// lookup manifest
var manifest = await this.archiver.getManifest(archive.key)
// respond
res.status(200).json({
user: username,
key: archive.key,
name: archive.name,
title: manifest ? manifest.title : '',
description: manifest ? manifest.description : ''
})
}
async archiveStatus (req, res) {
var type = req.accepts(['json', 'text/event-stream'])
if (type === 'text/event-stream') {
sse(req, res, () => this._getArchiveProgressEventStream(req, res))
} else {
let progress = await this._getArchiveProgress(req.params.key)
res.status(200).json({ progress })
}
}
async _getArchive (key) {
var archive = this.archiver.getArchive(key)
if (!archive) {
if (!this.archiver.isLoadingArchive(key)) {
throw new NotFoundError()
}
archive = await this.archiver.loadArchive(key)
}
return archive
}
_getArchiveProgressEventStream (req, res) {
let to
let done = false
let self = this
async function send () {
try {
var progress = await self._getArchiveProgress(req.params.key)
} catch (e) {
return // not found
}
if (done) return
res.sse('data: ' + progress + '\n\n')
to = setTimeout(send, 1e3)
}
res.once('close', () => {
done = true
clearTimeout(to)
})
send()
}
async _getArchiveProgress (key) {
// fetch the archive
var archive = await Promise.race([
this._getArchive(key),
wait(5e3, false)
])
if (!archive) return 0
var {metadata, content} = archive
// some data missing, report progress at zero
if (!metadata || !metadata.length || !content || !content.length) {
return 0
}
// calculate & respond
var need = metadata.length + content.length
var remaining = blocksRemaining(metadata) + blocksRemaining(content)
return (need - remaining) / need
}
}
function blocksRemaining (feed) {
var remaining = 0
for (var i = 0; i < feed.length; i++) {
if (!feed.has(i)) remaining++
}
return remaining
}