misskey/src/services/drive/upload-from-url.ts

111 lines
2.4 KiB
TypeScript
Raw Normal View History

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