2018-05-25 13:19:14 +02:00
|
|
|
import * as fs from 'fs';
|
2018-03-27 09:51:12 +02:00
|
|
|
import * as URL from 'url';
|
|
|
|
import * as tmp from 'tmp';
|
|
|
|
import * as request from 'request';
|
|
|
|
|
2018-05-25 13:19:14 +02:00
|
|
|
import { IDriveFile, validateFileName } from '../../models/drive-file';
|
|
|
|
import create from './add-file';
|
|
|
|
import config from '../../config';
|
2018-06-17 13:04:19 +02:00
|
|
|
import { IUser } from '../../models/user';
|
2018-06-18 07:28:43 +02:00
|
|
|
import * as mongodb from 'mongodb';
|
2019-02-03 08:45:13 +01:00
|
|
|
import { driveLogger } from './logger';
|
2018-05-25 13:19:14 +02:00
|
|
|
|
2019-02-03 08:45:13 +01:00
|
|
|
const logger = driveLogger.createSubLogger('downloader');
|
2018-03-27 09:51:12 +02:00
|
|
|
|
2018-11-07 04:12:43 +01:00
|
|
|
export default async (
|
|
|
|
url: string,
|
|
|
|
user: IUser,
|
|
|
|
folderId: mongodb.ObjectID = null,
|
|
|
|
uri: string = null,
|
|
|
|
sensitive = false,
|
2018-11-07 11:43:21 +01:00
|
|
|
force = false,
|
|
|
|
link = false
|
2018-11-07 04:12:43 +01:00
|
|
|
): Promise<IDriveFile> => {
|
2019-02-03 08:45:13 +01:00
|
|
|
logger.info(`REQUESTED: ${url}`);
|
2018-04-05 11:08:51 +02:00
|
|
|
|
2018-03-27 09:51:12 +02:00
|
|
|
let name = URL.parse(url).pathname.split('/').pop();
|
|
|
|
if (!validateFileName(name)) {
|
|
|
|
name = null;
|
|
|
|
}
|
|
|
|
|
2019-02-03 08:45:13 +01:00
|
|
|
logger.info(`name: ${name}`);
|
2018-04-05 11:08:51 +02:00
|
|
|
|
2018-03-27 09:51:12 +02:00
|
|
|
// Create temp file
|
2018-04-09 21:02:25 +02:00
|
|
|
const [path, cleanup] = await new Promise<[string, any]>((res, rej) => {
|
|
|
|
tmp.file((e, path, fd, cleanup) => {
|
2018-03-27 09:51:12 +02:00
|
|
|
if (e) return rej(e);
|
2018-04-09 21:02:25 +02:00
|
|
|
res([path, cleanup]);
|
2018-03-27 09:51:12 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// write content at URL to temp file
|
|
|
|
await new Promise((res, rej) => {
|
|
|
|
const writable = fs.createWriteStream(path);
|
2018-11-05 23:53:03 +01:00
|
|
|
|
|
|
|
writable.on('finish', () => {
|
|
|
|
res();
|
|
|
|
});
|
|
|
|
|
|
|
|
writable.on('error', error => {
|
|
|
|
rej(error);
|
|
|
|
});
|
|
|
|
|
2018-09-06 19:26:31 +02:00
|
|
|
const requestUrl = URL.parse(url).pathname.match(/[^\u0021-\u00ff]/) ? encodeURI(url) : url;
|
2018-11-05 23:53:03 +01:00
|
|
|
|
|
|
|
const req = request({
|
2018-09-06 19:26:31 +02:00
|
|
|
url: requestUrl,
|
2018-10-13 06:13:15 +02:00
|
|
|
proxy: config.proxy,
|
2018-11-05 23:53:03 +01:00
|
|
|
timeout: 10 * 1000,
|
2018-09-04 10:44:21 +02:00
|
|
|
headers: {
|
|
|
|
'User-Agent': config.user_agent
|
|
|
|
}
|
2018-11-05 23:53:03 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
req.pipe(writable);
|
|
|
|
|
|
|
|
req.on('response', response => {
|
|
|
|
if (response.statusCode !== 200) {
|
2018-03-27 09:51:12 +02:00
|
|
|
writable.close();
|
2018-11-05 23:53:03 +01:00
|
|
|
rej(response.statusCode);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
req.on('error', error => {
|
|
|
|
writable.close();
|
|
|
|
rej(error);
|
|
|
|
});
|
2018-03-27 09:51:12 +02:00
|
|
|
});
|
|
|
|
|
2018-04-09 21:11:52 +02:00
|
|
|
let driveFile: IDriveFile;
|
|
|
|
let error;
|
|
|
|
|
|
|
|
try {
|
2018-11-07 11:43:21 +01:00
|
|
|
driveFile = await create(user, path, name, null, folderId, force, link, url, uri, sensitive);
|
2019-02-03 08:45:13 +01:00
|
|
|
logger.succ(`got: ${driveFile._id}`);
|
2018-04-09 21:11:52 +02:00
|
|
|
} catch (e) {
|
|
|
|
error = e;
|
2019-02-03 08:45:13 +01:00
|
|
|
logger.error(`failed: ${e}`);
|
2018-04-09 21:11:52 +02:00
|
|
|
}
|
2018-04-05 11:08:51 +02:00
|
|
|
|
2018-03-27 09:51:12 +02:00
|
|
|
// clean-up
|
2018-04-09 21:02:25 +02:00
|
|
|
cleanup();
|
2018-03-27 09:51:12 +02:00
|
|
|
|
2018-04-09 21:11:52 +02:00
|
|
|
if (error) {
|
|
|
|
throw error;
|
|
|
|
} else {
|
|
|
|
return driveFile;
|
|
|
|
}
|
2018-03-27 09:51:12 +02:00
|
|
|
};
|