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

73 lines
1.7 KiB
TypeScript
Raw Normal View History

import * as fs from 'fs';
2018-03-27 07:51:12 +00:00
import * as URL from 'url';
2018-03-27 07:51:12 +00:00
import * as debug from 'debug';
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 05:28:43 +00:00
import * as mongodb from 'mongodb';
2018-04-05 09:08:51 +00:00
const log = debug('misskey:drive:upload-from-url');
2018-03-27 07:51:12 +00:00
2018-07-19 17:40:37 +00:00
export default async (url: string, user: IUser, folderId: mongodb.ObjectID = null, uri: string = null, sensitive = false): Promise<IDriveFile> => {
2018-04-05 09:08:51 +00:00
log(`REQUESTED: ${url}`);
2018-03-27 07:51:12 +00:00
let name = URL.parse(url).pathname.split('/').pop();
if (!validateFileName(name)) {
name = null;
}
2018-04-05 09:08:51 +00:00
log(`name: ${name}`);
2018-03-27 07:51:12 +00:00
// Create temp file
2018-04-09 19:02:25 +00:00
const [path, cleanup] = await new Promise<[string, any]>((res, rej) => {
tmp.file((e, path, fd, cleanup) => {
2018-03-27 07:51:12 +00:00
if (e) return rej(e);
2018-04-09 19:02:25 +00:00
res([path, cleanup]);
2018-03-27 07:51:12 +00:00
});
});
// write content at URL to temp file
await new Promise((res, rej) => {
const writable = fs.createWriteStream(path);
2018-09-06 17:26:31 +00:00
const requestUrl = URL.parse(url).pathname.match(/[^\u0021-\u00ff]/) ? encodeURI(url) : url;
2018-09-04 08:44:21 +00:00
request({
2018-09-06 17:26:31 +00:00
url: requestUrl,
2018-09-04 08:44:21 +00:00
headers: {
'User-Agent': config.user_agent
}
})
2018-03-27 07:51:12 +00:00
.on('error', rej)
.on('end', () => {
writable.close();
2018-04-09 19:03:25 +00:00
res();
2018-03-27 07:51:12 +00:00
})
.pipe(writable)
.on('error', rej);
});
2018-04-09 19:11:52 +00:00
let driveFile: IDriveFile;
let error;
try {
2018-07-19 17:40:37 +00:00
driveFile = await create(user, path, name, null, folderId, false, config.preventCacheRemoteFiles, url, uri, sensitive);
2018-07-11 14:59:57 +00:00
log(`got: ${driveFile._id}`);
2018-04-09 19:11:52 +00:00
} catch (e) {
error = e;
log(`failed: ${e}`);
}
2018-04-05 09:08:51 +00:00
2018-03-27 07:51:12 +00:00
// clean-up
2018-04-09 19:02:25 +00:00
cleanup();
2018-03-27 07:51:12 +00:00
2018-04-09 19:11:52 +00:00
if (error) {
throw error;
} else {
return driveFile;
}
2018-03-27 07:51:12 +00:00
};