バグ修正

This commit is contained in:
otofune 2017-11-14 04:28:51 +09:00
parent 54804f4a64
commit 47f98fbab7
2 changed files with 46 additions and 70 deletions

View file

@ -214,7 +214,7 @@ export default (
const readable = fs.createReadStream(path); const readable = fs.createReadStream(path);
return addToGridFS(name, readable, mime, { return addToGridFS(detectedName, readable, mime, {
user_id: user._id, user_id: user._id,
folder_id: folder !== null ? folder._id : null, folder_id: folder !== null ? folder._id : null,
comment: comment, comment: comment,
@ -224,11 +224,11 @@ export default (
.then(file => { .then(file => {
log(`drive file has been created ${file._id}`); log(`drive file has been created ${file._id}`);
resolve(file); resolve(file);
return serialize(file);
}) serialize(file)
.then(serializedFile => { .then(serializedFile => {
// Publish drive_file_created event // Publish drive_file_created event
event(user._id, 'drive_file_created', fileObj); event(user._id, 'drive_file_created', serializedFile);
// Register to search database // Register to search database
if (config.elasticsearch.enable) { if (config.elasticsearch.enable) {
@ -243,6 +243,7 @@ export default (
} }
}); });
} }
});
}) })
.catch(reject); .catch(reject);
}); });

View file

@ -10,7 +10,6 @@ import * as debug from 'debug';
import * as tmp from 'tmp'; import * as tmp from 'tmp';
import * as fs from 'fs'; import * as fs from 'fs';
import * as request from 'request'; import * as request from 'request';
import * as crypto from 'crypto';
const log = debug('misskey:endpoint:upload_from_url'); const log = debug('misskey:endpoint:upload_from_url');
@ -21,11 +20,11 @@ const log = debug('misskey:endpoint:upload_from_url');
* @param {any} user * @param {any} user
* @return {Promise<any>} * @return {Promise<any>}
*/ */
module.exports = (params, user) => new Promise((res, rej) => { module.exports = async (params, user): Promise<any> => {
// Get 'url' parameter // Get 'url' parameter
// TODO: Validate this url // TODO: Validate this url
const [url, urlErr] = $(params.url).string().$; const [url, urlErr] = $(params.url).string().$;
if (urlErr) return rej('invalid url param'); if (urlErr) throw 'invalid url param';
let name = URL.parse(url).pathname.split('/').pop(); let name = URL.parse(url).pathname.split('/').pop();
if (!validateFileName(name)) { if (!validateFileName(name)) {
@ -34,17 +33,18 @@ module.exports = (params, user) => new Promise((res, rej) => {
// Get 'folder_id' parameter // Get 'folder_id' parameter
const [folderId = null, folderIdErr] = $(params.folder_id).optional.nullable.id().$; const [folderId = null, folderIdErr] = $(params.folder_id).optional.nullable.id().$;
if (folderIdErr) return rej('invalid folder_id param'); if (folderIdErr) throw 'invalid folder_id param';
// Create temp file // Create temp file
new Promise((res, rej) => { const path = await new Promise((res: (string) => void, rej) => {
tmp.file((e, path) => { tmp.file((e, path) => {
if (e) return rej(e); if (e) return rej(e);
res(path); res(path);
}); });
}) });
// Download file
.then((path: string) => new Promise((res, rej) => { // write content at URL to temp file
await new Promise((res, rej) => {
const writable = fs.createWriteStream(path); const writable = fs.createWriteStream(path);
request(url) request(url)
.on('error', rej) .on('error', rej)
@ -54,39 +54,14 @@ module.exports = (params, user) => new Promise((res, rej) => {
}) })
.pipe(writable) .pipe(writable)
.on('error', rej); .on('error', rej);
})) });
// Calculate hash & content-type
.then((path: string) => new Promise((res, rej) => { const driveFile = await create(user, path, name, null, folderId);
const readable = fs.createReadStream(path);
const hash = crypto.createHash('md5'); // clean-up
readable
.on('error', rej)
.on('end', () => {
hash.end();
res([path, hash.digest('hex')]);
})
.pipe(hash)
.on('error', rej);
}))
// Create file
.then((rv: string[]) => new Promise((res, rej) => {
const [path, hash] = rv;
create(user, {
stream: fs.createReadStream(path),
name,
hash
}, null, folderId)
.then(driveFile => {
res(driveFile);
// crean-up
fs.unlink(path, (e) => { fs.unlink(path, (e) => {
if (e) log(e.stack); if (e) log(e.stack);
}); });
})
.catch(rej); return serialize(driveFile);
})) };
// Serialize
.then(serialize)
.then(res)
.catch(rej);
});