2017-02-16 21:46:14 +01:00
|
|
|
/**
|
|
|
|
* Module dependencies
|
|
|
|
*/
|
|
|
|
import * as URL from 'url';
|
2017-03-08 19:50:09 +01:00
|
|
|
import $ from 'cafy';
|
2017-02-16 21:46:14 +01:00
|
|
|
import { validateFileName } from '../../../models/drive-file';
|
|
|
|
import serialize from '../../../serializers/drive-file';
|
|
|
|
import create from '../../../common/add-file-to-drive';
|
2017-11-13 19:46:30 +01:00
|
|
|
import * as debug from 'debug';
|
|
|
|
import * as tmp from 'tmp';
|
|
|
|
import * as fs from 'fs';
|
|
|
|
import * as request from 'request';
|
|
|
|
import * as crypto from 'crypto';
|
|
|
|
|
2017-11-13 19:47:42 +01:00
|
|
|
const log = debug('misskey:endpoint:upload_from_url');
|
2017-02-16 21:46:14 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a file from a URL
|
|
|
|
*
|
2017-03-01 09:37:01 +01:00
|
|
|
* @param {any} params
|
|
|
|
* @param {any} user
|
|
|
|
* @return {Promise<any>}
|
2017-02-16 21:46:14 +01:00
|
|
|
*/
|
2017-11-13 19:46:30 +01:00
|
|
|
module.exports = (params, user) => new Promise((res, rej) => {
|
2017-02-16 21:46:14 +01:00
|
|
|
// Get 'url' parameter
|
2017-02-21 01:28:11 +01:00
|
|
|
// TODO: Validate this url
|
2017-03-08 19:50:09 +01:00
|
|
|
const [url, urlErr] = $(params.url).string().$;
|
2017-03-03 11:33:14 +01:00
|
|
|
if (urlErr) return rej('invalid url param');
|
2017-02-16 21:46:14 +01:00
|
|
|
|
|
|
|
let name = URL.parse(url).pathname.split('/').pop();
|
|
|
|
if (!validateFileName(name)) {
|
|
|
|
name = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get 'folder_id' parameter
|
2017-03-08 19:50:09 +01:00
|
|
|
const [folderId = null, folderIdErr] = $(params.folder_id).optional.nullable.id().$;
|
2017-03-03 11:33:14 +01:00
|
|
|
if (folderIdErr) return rej('invalid folder_id param');
|
2017-02-16 21:46:14 +01:00
|
|
|
|
2017-11-13 19:46:30 +01:00
|
|
|
// Create temp file
|
|
|
|
new Promise((res, rej) => {
|
|
|
|
tmp.file((e, path) => {
|
2017-11-13 19:47:42 +01:00
|
|
|
if (e) return rej(e);
|
|
|
|
res(path);
|
|
|
|
});
|
2017-11-13 19:46:30 +01:00
|
|
|
})
|
|
|
|
// Download file
|
|
|
|
.then((path: string) => new Promise((res, rej) => {
|
2017-11-13 19:47:42 +01:00
|
|
|
const writable = fs.createWriteStream(path);
|
2017-11-13 19:46:30 +01:00
|
|
|
request(url)
|
|
|
|
.on('error', rej)
|
|
|
|
.on('end', () => {
|
2017-11-13 19:47:42 +01:00
|
|
|
writable.close();
|
|
|
|
res(path);
|
2017-11-13 19:46:30 +01:00
|
|
|
})
|
|
|
|
.pipe(writable)
|
2017-11-13 19:47:42 +01:00
|
|
|
.on('error', rej);
|
2017-11-13 19:46:30 +01:00
|
|
|
}))
|
|
|
|
// Calculate hash & content-type
|
|
|
|
.then((path: string) => new Promise((res, rej) => {
|
2017-11-13 19:47:42 +01:00
|
|
|
const readable = fs.createReadStream(path);
|
|
|
|
const hash = crypto.createHash('md5');
|
2017-11-13 19:46:30 +01:00
|
|
|
readable
|
|
|
|
.on('error', rej)
|
|
|
|
.on('end', () => {
|
2017-11-13 19:47:42 +01:00
|
|
|
hash.end();
|
|
|
|
res([path, hash.digest('hex')]);
|
2017-11-13 19:46:30 +01:00
|
|
|
})
|
|
|
|
.pipe(hash)
|
2017-11-13 19:47:42 +01:00
|
|
|
.on('error', rej);
|
2017-11-13 19:46:30 +01:00
|
|
|
}))
|
|
|
|
// Create file
|
|
|
|
.then((rv: string[]) => new Promise((res, rej) => {
|
2017-11-13 19:47:42 +01:00
|
|
|
const [path, hash] = rv;
|
2017-11-13 19:46:30 +01:00
|
|
|
create(user, {
|
|
|
|
stream: fs.createReadStream(path),
|
|
|
|
name,
|
|
|
|
hash
|
|
|
|
}, null, folderId)
|
|
|
|
.then(driveFile => {
|
2017-11-13 19:47:42 +01:00
|
|
|
res(driveFile);
|
2017-11-13 19:46:30 +01:00
|
|
|
// crean-up
|
|
|
|
fs.unlink(path, (e) => {
|
2017-11-13 19:47:42 +01:00
|
|
|
if (e) log(e.stack);
|
|
|
|
});
|
2017-11-13 19:46:30 +01:00
|
|
|
})
|
2017-11-13 19:47:42 +01:00
|
|
|
.catch(rej);
|
2017-11-13 19:46:30 +01:00
|
|
|
}))
|
|
|
|
// Serialize
|
|
|
|
.then(serialize)
|
|
|
|
.then(res)
|
2017-11-13 19:47:42 +01:00
|
|
|
.catch(rej);
|
2017-02-16 21:46:14 +01:00
|
|
|
});
|