-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtaskManager.js
362 lines (352 loc) · 8.71 KB
/
taskManager.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
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
import compression from 'compression';
import debug from 'debug';
import express from 'express';
import { ObjectId } from 'mongodb';
import { EventEmitter } from 'events';
import { ensure, UserInputError } from './ensure/index.js';
import { HEIGHT, WIDTH } from './constants.js';
import { Drawer } from './drawer.js';
const log = debug('drawer:task');
/**
* @typedef {{width:number,height:number,data:string}} PaintImage
*/
const ensurePaintImage = (() => {
const ensureImageFormat = ensure({
type: 'object',
entires: {
width: { type: 'integer', min: 1, max: WIDTH },
height: { type: 'integer', min: 1, max: HEIGHT },
data: { type: 'string', pattern: /^[0-9a-v.]+$/ },
}
});
/**
* @returns {PaintImage}
*/
return (/** @type {unknown} */ value) => {
const { width, height, data } = ensureImageFormat(value);
if (data.length !== width * height) {
throw new UserInputError('data.length !== width * height');
}
return { width, height, data };
};
})();
const ensureLeftTopFormat = ensure({
type: 'object',
entires: {
x: { type: 'integer', min: 0, max: WIDTH - 1 },
y: { type: 'integer', min: 0, max: HEIGHT - 1 },
}
});
/**
* @typedef {{leftTop:{x:number,y:number},weight:number}} TaskOption
* @typedef {{image:PaintImage,options:TaskOption}} Task
*/
const ensureTaskOptionsFormat = ensure({
type: 'object',
entires: {
leftTop: ensureLeftTopFormat,
weight: { type: 'real', min: 0, max: 1e290 },
}
});
/**
* @param {Task} task
*/
function doCheckValidTask(task) {
if (task.options.leftTop.x + task.image.width > WIDTH) {
throw new UserInputError('右边界超出绘板范围');
}
if (task.options.leftTop.y + task.image.height > HEIGHT) {
throw new UserInputError('下边界超出绘板范围');
}
}
const ensureTask = (() => {
const ensureTaskFormat = ensure({
type: 'object',
entires: {
image: ensurePaintImage,
options: ensureTaskOptionsFormat,
}
});
/**
* @returns {Task}
*/
return (/** @type {unknown} */ value) => {
const task = ensureTaskFormat(value);
doCheckValidTask(task);
return task;
};
})();
const ensureObjectId = (() => {
const ensureObjectIdFormat = ensure({ type: 'string', pattern: /^[0-9a-f]{24}$/ });
return (/** @type {unknown} */ value) => new ObjectId(ensureObjectIdFormat(value));
})();
/**
* @param {{ width: number; height: number; }} image
*/
export function size(image) {
return image.width * image.height;
}
export class TaskManager extends EventEmitter {
/**
* @param {Drawer} dependencies
* @param {{}} config
*/
constructor({ authManager, userManager, tokenManager, database }, { }) {
super();
this.authManager = authManager;
this.tokenManager = tokenManager;
this.userManager = userManager;
this.database = database;
/**@type {Set<string>} */
this.uploadingUsers = new Set();
}
/**
* @param {string} owner
* @param {Task} task
*/
async addTask(owner, task) {
const taskSize = size(task.image);
const release = await this.userManager.consumeResources(owner, { deltaCount: 1, deltaTotalSize: taskSize });
try {
const tasks = await this.database.tasks();
const id = new ObjectId();
await tasks.insertOne({ _id: id, owner, verified: false, ...task });
log(
'new task owner=%s size=%d*%d leftTop=(%d,%d) weight=%d id=%s',
owner,
task.image.width,
task.image.height,
task.options.leftTop.x,
task.options.leftTop.y,
task.options.weight,
id.toHexString(),
);
this.emit('add', id);
return id;
}
catch (error) {
await release();
throw error;
}
}
/**
* @param {ObjectId} id
* @param {string} uid
* @param {TaskOption} options
*/
async updateTask(id, uid, options) {
const tasks = await this.database.tasks();
// console.log({
// _id: id,
// owner: uid,
// 'image.width': { $lte: WIDTH - options.leftTop.x },
// 'image.height': { $lte: HEIGHT - options.leftTop.y },
// });
const result = await tasks.updateOne({
_id: id,
owner: uid,
'image.width': { $lte: WIDTH - options.leftTop.x },
'image.height': { $lte: HEIGHT - options.leftTop.y },
}, { $set: { options } });
log(
'update task owner=%s leftTop=(%d,%d) weight=%d id=%s',
uid,
options.leftTop.x,
options.leftTop.y,
options.weight,
id.toHexString(),
);
// console.log(result);
if (result.matchedCount === 1) {
// ok
log('updated successfully, %s', result.modifiedCount > 0 ? 'changed' : 'unchanged');
if (result.modifiedCount > 0) {
this.emit('update', id);
}
return;
}
else {
throw new UserInputError('该任务已被删除');
}
}
/**
* @param {ObjectId} id
* @param {string} uid
*/
async deleteTask(id, uid) {
const tasks = await this.database.tasks();
// console.log({
// _id: id,
// owner: uid,
// 'image.width': { $lte: WIDTH - options.leftTop.x },
// 'image.height': { $lte: HEIGHT - options.leftTop.y },
// });
const result = await tasks.findOneAndDelete({
_id: id,
owner: uid,
});
log(
'delete task owner=%s id=%s',
uid,
id.toHexString(),
);
if (result.value !== null) {
log('deleted successfully');
await this.userManager.consumeResources(uid, { deltaCount: -1, deltaTotalSize: -size(result.value.image) });
this.emit('delete', id);
}
else {
throw new UserInputError('该任务已被删除');
}
}
/**
* @returns {express.Handler[]}
*/
addTaskHandler() {
return [
...this.authManager.checkAndRequireAuth(),
express.json({ limit: '100kb' }),
(req, res, next) => {
const { uid } = res.locals.auth;
const task = ensureTask(req.body);
this.addTask(uid, task)
.then(id => {
res.status(200).json({ id: id.toHexString() }).end();
})
.catch(next);
}
];
}
/**
* @returns {express.Handler[]}
*/
updateTaskHandler() {
const ensureParams = ensure({
type: 'object',
entires: {
id: ensureObjectId,
}
});
const ensureBody = ensure({
type: 'object',
entires: {
options: ensureTaskOptionsFormat,
}
});
return [
...this.authManager.checkAndRequireAuth(),
express.json({ limit: '5kb' }),
(req, res, next) => {
const { uid } = res.locals.auth;
const { id } = ensureParams(req.params);
const { options } = ensureBody(req.body);
this.updateTask(id, uid, options)
.then(() => {
res.status(200).end();
})
.catch(next);
}
];
}
/**
* @returns {express.Handler[]}
*/
deleteTaskHandler() {
const ensureParams = ensure({
type: 'object',
entires: {
id: ensureObjectId,
}
});
return [
...this.authManager.checkAndRequireAuth(),
(req, res, next) => {
const { uid } = res.locals.auth;
const { id } = ensureParams(req.params);
this.deleteTask(id, uid)
.then(() => {
res.status(200).end();
})
.catch(next);
}
];
}
/**
* @param {string} owner
*/
async getTasks(owner) {
const tasks = await this.database.tasks();
const cursor = tasks.find({ owner }, { sort: [['weight', -1]] });
let list = [];
while (true) {
const result = await cursor.next();
if (result !== null) {
const { _id: id, ...data } = result;
list.push({ id, ...data });
}
else {
cursor.close();
break;
}
}
return list;
}
async getAllTasks() {
const tasks = await this.database.tasks();
const cursor = tasks.find({});
let items = [];
while (true) {
const result = await cursor.next();
if (result !== null) {
items.push(result);
}
else {
cursor.close();
break;
}
}
return items;
}
async getAllTaskIDs() {
const tasks = await this.database.tasks();
const cursor = tasks.aggregate([{ $project: { _id: '$_id' } }]);
let items = [];
while (true) {
const result = await cursor.next();
if (result !== null) {
items.push(result);
}
else {
cursor.close();
break;
}
}
return items;
}
/**
* @returns {express.Handler[]}
*/
getTasksHandler() {
return [
...this.authManager.checkAndRequireAuth(),
compression(),
(req, res, next) => {
const { uid } = res.locals.auth;
this.getTasks(uid)
.then(result => {
res.status(200).json(result.map(
({ id, image, options }) => ({ id: id.toHexString(), image, options }))
).end();
})
.catch(next);
}
];
}
router() {
return express.Router()
.post('/tasks', this.addTaskHandler())
.post('/task/:id', this.updateTaskHandler())
.delete('/task/:id', this.deleteTaskHandler())
.get('/tasks', this.getTasksHandler());
}
}