-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathfinish.js
More file actions
151 lines (126 loc) · 4.16 KB
/
Copy pathfinish.js
File metadata and controls
151 lines (126 loc) · 4.16 KB
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
const { ActionTransport } = require('@microfleet/core');
const Promise = require('bluebird');
const assert = require('assert');
const { HttpStatusError } = require('common-errors');
const fetchData = require('../utils/fetch-data');
const handlePipeline = require('../utils/pipeline-error');
const {
STATUS_UPLOADED,
STATUS_PENDING,
UPLOAD_DATA,
FILES_INDEX,
FILES_DATA,
FILES_INDEX_TEMP,
FILES_INDEX_PUBLIC,
FILES_INDEX_TAGS,
FILES_OWNER_FIELD,
FILES_PUBLIC_FIELD,
FILES_TAGS_FIELD,
FILES_TEMP_FIELD,
FILES_STATUS_FIELD,
FILES_PARTS_FIELD,
FILES_UNLISTED_FIELD,
FILES_POST_ACTION,
FILES_DIRECT_ONLY_FIELD,
} = require('../constant.js');
// cached vars
const fields = [
FILES_PARTS_FIELD, FILES_TAGS_FIELD,
FILES_OWNER_FIELD, FILES_PUBLIC_FIELD, FILES_TEMP_FIELD,
FILES_UNLISTED_FIELD, FILES_DIRECT_ONLY_FIELD,
];
const jsonFields = JSON.stringify(fields);
const MissingError = new HttpStatusError(200, '404: could not find upload');
const AlreadyProcessedError = new HttpStatusError(200, '412: upload was already processed');
const is404 = { statusCode: 404 };
const is409 = { message: '409' };
/**
* Finish upload
* @param {Object} opts
* @param {Object} opts.params
* @param {String} opts.params.filename
* @param {Boolean} opts.params.skipProcessing
* @param {Boolean} opts.params.await
* @return {Promise}
*/
async function completeFileUpload({ params }) {
const { filename } = params;
const { redis, config, amqp, provider } = this;
const { prefix } = config.router.routes;
const uploadPartKey = `${UPLOAD_DATA}:${filename}`;
const data = await Promise
.bind(this, uploadPartKey)
.then(fetchData)
.catchThrow(is404, MissingError);
// we do not send 412, because google might decide to delay notifications
assert.equal(data[FILES_STATUS_FIELD], STATUS_PENDING, AlreadyProcessedError);
// ensure it was actually uploaded
const transport = provider('sync', data);
const exists = await transport.exists(filename);
assert.equal(exists, true, MissingError);
const { uploadId } = data;
const uploadKey = `${FILES_DATA}:${uploadId}`;
const postActionKey = `${FILES_POST_ACTION}:${uploadId}`;
const updateKeys = [uploadKey, postActionKey, uploadPartKey];
const updateArgs = [Date.now(), FILES_STATUS_FIELD, STATUS_UPLOADED, STATUS_PENDING, 'uploaded', jsonFields];
// set to uploaded
const update = await redis
.markAsUploaded(3, updateKeys, updateArgs)
.catchThrow(is409, AlreadyProcessedError);
const [parts, currentParts, postAction] = update;
// destructure array
// annoying redis format :(
const [
totalParts,
tags,
username,
isPublic,
isTemporary,
isUnlisted,
isDirectOnly,
] = parts;
// use pooled error to avoid stack generation
if (currentParts < totalParts) {
throw new HttpStatusError(202, `${currentParts}/${totalParts} uploaded`);
}
const pipeline = redis.pipeline();
// update key
pipeline.hmset(uploadKey, {
status: STATUS_UPLOADED,
uploadedAt: Date.now(),
});
// remove reference
pipeline.srem(FILES_INDEX_TEMP, uploadId);
// unless file is temp -> add them to index
if (!isTemporary) {
pipeline.persist(uploadKey);
if (!isUnlisted) {
pipeline.sadd(FILES_INDEX, uploadId);
pipeline.sadd(`${FILES_INDEX}:${username}`, uploadId);
// convert 1 or undef to Boolean
// if `isDirectOnly` is truthy, we won't publish this in the public index
if (isPublic && !isDirectOnly) {
pipeline.sadd(FILES_INDEX_PUBLIC, uploadId);
pipeline.sadd(`${FILES_INDEX}:${username}:pub`, uploadId);
}
// push to tags index
if (tags) {
for (const tag of JSON.parse(tags)) {
pipeline.sadd(`${FILES_INDEX_TAGS}:${tag}`, uploadId);
}
}
}
}
if (postAction) {
pipeline.persist(postActionKey);
}
handlePipeline(await pipeline.exec());
if (params.skipProcessing) {
return 'upload completed, processing skipped';
}
const action = params.await ? 'publishAndWait' : 'publish';
const route = `${prefix}.process`;
return amqp[action](route, { uploadId });
}
completeFileUpload.transports = [ActionTransport.amqp];
module.exports = completeFileUpload;