Use async function instead of promise (#325)

This commit is contained in:
Weihang Lo 2017-08-03 11:11:04 +08:00
parent b063cf35b8
commit bbe111a95e
No known key found for this signature in database
GPG Key ID: D7DBF189825E82E7

View File

@ -117,93 +117,79 @@ app.get('/jsconfig.js', (req, res) => {
}); });
}); });
app.get('/exists/:id', (req, res) => { app.get('/exists/:id', async (req, res) => {
const id = req.params.id; const id = req.params.id;
if (!validateID(id)) { if (!validateID(id)) {
res.sendStatus(404); res.sendStatus(404);
return; return;
} }
storage try {
.exists(id) await storage.exists(id);
.then(() => { res.sendStatus(200);
res.sendStatus(200); } catch (e) {
}) res.sendStatus(404);
.catch(err => res.sendStatus(404)); }
}); });
app.get('/download/:id', (req, res) => { app.get('/download/:id', async (req, res) => {
const id = req.params.id; const id = req.params.id;
if (!validateID(id)) { if (!validateID(id)) {
res.sendStatus(404); res.sendStatus(404);
return; return;
} }
storage try {
.filename(id) const filename = await storage.filename(id);
.then(filename => { const contentLength = await storage.length(id);
return storage.length(id).then(contentLength => { const timeToExpiry = storage.ttl(id);
storage.ttl(id).then(timeToExpiry => { res.render('download', {
res.render('download', { filename: decodeURIComponent(filename),
filename: decodeURIComponent(filename), filesize: bytes(contentLength),
filesize: bytes(contentLength), sizeInBytes: contentLength,
sizeInBytes: contentLength, timeToExpiry: timeToExpiry
timeToExpiry: timeToExpiry
});
});
});
})
.catch(() => {
res.status(404).render('notfound');
}); });
} catch (e) {
res.status(404).render('notfound');
}
}); });
app.get('/assets/download/:id', (req, res) => { app.get('/assets/download/:id', async (req, res) => {
const id = req.params.id; const id = req.params.id;
if (!validateID(id)) { if (!validateID(id)) {
res.sendStatus(404); res.sendStatus(404);
return; return;
} }
storage try {
.metadata(id) const meta = await storage.metadata(id);
.then(meta => { const contentLength = await storage.length(id);
storage res.writeHead(200, {
.length(id) 'Content-Disposition': `attachment; filename=${meta.filename}`,
.then(contentLength => { 'Content-Type': 'application/octet-stream',
res.writeHead(200, { 'Content-Length': contentLength,
'Content-Disposition': 'attachment; filename=' + meta.filename, 'X-File-Metadata': JSON.stringify(meta)
'Content-Type': 'application/octet-stream',
'Content-Length': contentLength,
'X-File-Metadata': JSON.stringify(meta)
});
const file_stream = storage.get(id);
file_stream.on('end', () => {
storage
.forceDelete(id)
.then(err => {
if (!err) {
log.info('Deleted:', id);
}
})
.catch(err => {
log.info('DeleteError:', id);
});
});
file_stream.pipe(res);
})
.catch(err => {
res.sendStatus(404);
});
})
.catch(err => {
res.sendStatus(404);
}); });
const file_stream = storage.get(id);
file_stream.on('end', async () => {
try {
const err = await storage.forceDelete(id);
if (!err) {
log.info('Deleted:', id);
}
} catch (e) {
log.info('DeleteError:', id);
}
});
file_stream.pipe(res);
} catch (e) {
res.sendStatus(404);
}
}); });
app.post('/delete/:id', (req, res) => { app.post('/delete/:id', async (req, res) => {
const id = req.params.id; const id = req.params.id;
if (!validateID(id)) { if (!validateID(id)) {
@ -218,15 +204,15 @@ app.post('/delete/:id', (req, res) => {
return; return;
} }
storage try {
.delete(id, delete_token) const err = await storage.delete(id, delete_token);
.then(err => { if (!err) {
if (!err) { log.info('Deleted:', id);
log.info('Deleted:', id); res.sendStatus(200);
res.sendStatus(200); }
} } catch (e) {
}) res.sendStatus(404);
.catch(err => res.sendStatus(404)); }
}); });
app.post('/upload', (req, res, next) => { app.post('/upload', (req, res, next) => {
@ -235,7 +221,7 @@ app.post('/upload', (req, res, next) => {
try { try {
meta = JSON.parse(req.header('X-File-Metadata')); meta = JSON.parse(req.header('X-File-Metadata'));
} catch (err) { } catch (e) {
res.sendStatus(400); res.sendStatus(400);
return; return;
} }
@ -254,39 +240,36 @@ app.post('/upload', (req, res, next) => {
log.info('meta', meta); log.info('meta', meta);
req.pipe(req.busboy); req.pipe(req.busboy);
req.busboy.on('file', (fieldname, file, filename) => { req.busboy.on('file', async (fieldname, file, filename) => {
log.info('Uploading:', newId); log.info('Uploading:', newId);
storage.set(newId, file, filename, meta).then( try {
() => { await storage.set(newId, file, filename, meta);
const protocol = conf.env === 'production' ? 'https' : req.protocol;
const url = `${protocol}://${req.get('host')}/download/${newId}/`; const protocol = conf.env === 'production' ? 'https' : req.protocol;
res.json({ const url = `${protocol}://${req.get('host')}/download/${newId}/`;
url, res.json({
delete: meta.delete, url,
id: newId delete: meta.delete,
}); id: newId
}, });
err => { } catch (e) {
if (err.message === 'limit') { if (e.message === 'limit') {
return res.sendStatus(413); return res.sendStatus(413);
}
res.sendStatus(500);
} }
); res.sendStatus(500);
}
}); });
req.on('close', err => { req.on('close', async err => {
storage try {
.forceDelete(newId) const err = await storage.forceDelete(newId);
.then(err => { if (!err) {
if (!err) { log.info('Deleted:', newId);
log.info('Deleted:', newId); }
} } catch (e) {
}) log.info('DeleteError:', newId);
.catch(err => { }
log.info('DeleteError:', newId);
});
}); });
}); });
@ -294,8 +277,13 @@ app.get('/__lbheartbeat__', (req, res) => {
res.sendStatus(200); res.sendStatus(200);
}); });
app.get('/__heartbeat__', (req, res) => { app.get('/__heartbeat__', async (req, res) => {
storage.ping().then(() => res.sendStatus(200), () => res.sendStatus(500)); try {
await storage.ping();
res.sendStatus(200);
} catch (e) {
res.sendStatus(500);
}
}); });
app.get('/__version__', (req, res) => { app.get('/__version__', (req, res) => {