/
promises-native-async-await.js
51 lines (47 loc) · 1.54 KB
/
promises-native-async-await.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
global.useNative = true;
try {
if (Promise.race.toString() !== 'function race() { [native code] }')
throw 0;
} catch (e) {
throw new Error("No ES6 promises available");
}
require('../lib/fakesP');
module.exports = async function upload(stream, idOrPath, tag, done) {
try {
var blob = blobManager.create(account);
var tx = db.begin();
var blobId = await blob.put(stream);
var file = await self.byUuidOrPath(idOrPath).get();
var previousId = file ? file.version : null;
version = {
userAccountId: userAccount.id,
date: new Date(),
blobId: blobId,
creatorId: userAccount.id,
previousId: previousId,
};
version.id = Version.createHash(version);
await Version.insert(version).execWithin(tx);
if (!file) {
var splitPath = idOrPath.split('/');
var fileName = splitPath[splitPath.length - 1];
file = {
id: uuid.v1(),
userAccountId: userAccount.id,
name: fileName,
version: version.id
}
var query = await self.createQuery(idOrPath, file);
await query.execWithin(tx);
}
await FileVersion.insert({fileId: file.id, versionId: version.id})
.execWithin(tx);
await File.whereUpdate({id: file.id}, {version: version.id})
.execWithin(tx);
tx.commit();
done();
} catch (err) {
tx.rollback();
done(err);
}
};