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