1
0
mirror of https://github.com/hotomoe/hotomoe synced 2024-11-28 23:08:17 +09:00

Better logging

This commit is contained in:
syuilo 2019-02-03 17:09:16 +09:00
parent 75558add17
commit efe2a6be14
No known key found for this signature in database
GPG Key ID: BDC4C49D06AB9D69

View File

@ -9,6 +9,7 @@ import config from '../../config';
import { IUser } from '../../models/user';
import * as mongodb from 'mongodb';
import { driveLogger } from './logger';
import chalk from 'chalk';
const logger = driveLogger.createSubLogger('downloader');
@ -21,15 +22,11 @@ export default async (
force = false,
link = false
): Promise<IDriveFile> => {
logger.info(`REQUESTED: ${url}`);
let name = URL.parse(url).pathname.split('/').pop();
if (!validateFileName(name)) {
name = null;
}
logger.info(`name: ${name}`);
// Create temp file
const [path, cleanup] = await new Promise<[string, any]>((res, rej) => {
tmp.file((e, path, fd, cleanup) => {
@ -40,13 +37,17 @@ export default async (
// write content at URL to temp file
await new Promise((res, rej) => {
logger.info(`Downloading ${chalk.cyan(url)} ...`);
const writable = fs.createWriteStream(path);
writable.on('finish', () => {
logger.succ(`Download succeeded: ${chalk.cyan(url)}`);
res();
});
writable.on('error', error => {
logger.error(error);
rej(error);
});
@ -65,12 +66,14 @@ export default async (
req.on('response', response => {
if (response.statusCode !== 200) {
logger.error(`Got ${response.statusCode} (${url})`);
writable.close();
rej(response.statusCode);
}
});
req.on('error', error => {
logger.error(error);
writable.close();
rej(error);
});
@ -81,10 +84,10 @@ export default async (
try {
driveFile = await create(user, path, name, null, folderId, force, link, url, uri, sensitive);
logger.succ(`got: ${driveFile._id}`);
logger.succ(`Got: ${driveFile._id}`);
} catch (e) {
error = e;
logger.error(`failed: ${e}`);
logger.error(`Failed: ${e}`);
}
// clean-up