From 074c851a875102190bb4bfa13d6c02d2b66d4aef Mon Sep 17 00:00:00 2001 From: syuilo Date: Sun, 3 Feb 2019 17:09:16 +0900 Subject: [PATCH] Better logging --- src/services/drive/upload-from-url.ts | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/services/drive/upload-from-url.ts b/src/services/drive/upload-from-url.ts index 3d2269d91..8daecbadb 100644 --- a/src/services/drive/upload-from-url.ts +++ b/src/services/drive/upload-from-url.ts @@ -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 => { - 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