Skip to content
Snippets Groups Projects
Unverified Commit efe2a6be authored by syuilo's avatar syuilo
Browse files

Better logging

parent 75558add
No related branches found
No related tags found
No related merge requests found
......@@ -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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment