summaryrefslogtreecommitdiff
path: root/src/api
diff options
context:
space:
mode:
authorotofune <otofune@gmail.com>2017-11-14 04:28:51 +0900
committerotofune <otofune@gmail.com>2017-11-14 04:29:30 +0900
commit47f98fbab76e8680f5a7c99037b3b237c7256ca2 (patch)
treeec58fd3b4541199ff70231fc9024fc9d35900add /src/api
parentadd-file-to-drive - hashがstreamを受ける時、hashもまたstreamなのだ (diff)
downloadsharkey-47f98fbab76e8680f5a7c99037b3b237c7256ca2.tar.gz
sharkey-47f98fbab76e8680f5a7c99037b3b237c7256ca2.tar.bz2
sharkey-47f98fbab76e8680f5a7c99037b3b237c7256ca2.zip
バグ修正
Diffstat (limited to 'src/api')
-rw-r--r--src/api/common/add-file-to-drive.ts35
-rw-r--r--src/api/endpoints/drive/files/upload_from_url.ts81
2 files changed, 46 insertions, 70 deletions
diff --git a/src/api/common/add-file-to-drive.ts b/src/api/common/add-file-to-drive.ts
index 5f7d255e4b..1c8965e31d 100644
--- a/src/api/common/add-file-to-drive.ts
+++ b/src/api/common/add-file-to-drive.ts
@@ -214,7 +214,7 @@ export default (
const readable = fs.createReadStream(path);
- return addToGridFS(name, readable, mime, {
+ return addToGridFS(detectedName, readable, mime, {
user_id: user._id,
folder_id: folder !== null ? folder._id : null,
comment: comment,
@@ -224,25 +224,26 @@ export default (
.then(file => {
log(`drive file has been created ${file._id}`);
resolve(file);
- return serialize(file);
- })
- .then(serializedFile => {
- // Publish drive_file_created event
- event(user._id, 'drive_file_created', fileObj);
- // Register to search database
- if (config.elasticsearch.enable) {
- const es = require('../../db/elasticsearch');
- es.index({
- index: 'misskey',
- type: 'drive_file',
- id: file._id.toString(),
- body: {
- name: file.name,
- user_id: user._id.toString()
+ serialize(file)
+ .then(serializedFile => {
+ // Publish drive_file_created event
+ event(user._id, 'drive_file_created', serializedFile);
+
+ // Register to search database
+ if (config.elasticsearch.enable) {
+ const es = require('../../db/elasticsearch');
+ es.index({
+ index: 'misskey',
+ type: 'drive_file',
+ id: file._id.toString(),
+ body: {
+ name: file.name,
+ user_id: user._id.toString()
+ }
+ });
}
});
- }
})
.catch(reject);
});
diff --git a/src/api/endpoints/drive/files/upload_from_url.ts b/src/api/endpoints/drive/files/upload_from_url.ts
index 60332b4afe..519e0bdf65 100644
--- a/src/api/endpoints/drive/files/upload_from_url.ts
+++ b/src/api/endpoints/drive/files/upload_from_url.ts
@@ -10,7 +10,6 @@ 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';
const log = debug('misskey:endpoint:upload_from_url');
@@ -21,11 +20,11 @@ const log = debug('misskey:endpoint:upload_from_url');
* @param {any} user
* @return {Promise<any>}
*/
-module.exports = (params, user) => new Promise((res, rej) => {
+module.exports = async (params, user): Promise<any> => {
// Get 'url' parameter
// TODO: Validate this url
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();
if (!validateFileName(name)) {
@@ -34,59 +33,35 @@ module.exports = (params, user) => new Promise((res, rej) => {
// Get 'folder_id' parameter
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
- new Promise((res, rej) => {
+ const path = await new Promise((res: (string) => void, rej) => {
tmp.file((e, path) => {
if (e) return rej(e);
res(path);
});
- })
- // Download file
- .then((path: string) => new Promise((res, rej) => {
- const writable = fs.createWriteStream(path);
- request(url)
- .on('error', rej)
- .on('end', () => {
- writable.close();
- res(path);
- })
- .pipe(writable)
- .on('error', rej);
- }))
- // Calculate hash & content-type
- .then((path: string) => new Promise((res, rej) => {
- const readable = fs.createReadStream(path);
- const hash = crypto.createHash('md5');
- 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) => {
- if (e) log(e.stack);
- });
- })
- .catch(rej);
- }))
- // Serialize
- .then(serialize)
- .then(res)
- .catch(rej);
-});
+ });
+
+ // write content at URL to temp file
+ await new Promise((res, rej) => {
+ const writable = fs.createWriteStream(path);
+ request(url)
+ .on('error', rej)
+ .on('end', () => {
+ writable.close();
+ res(path);
+ })
+ .pipe(writable)
+ .on('error', rej);
+ });
+
+ const driveFile = await create(user, path, name, null, folderId);
+
+ // clean-up
+ fs.unlink(path, (e) => {
+ if (e) log(e.stack);
+ });
+
+ return serialize(driveFile);
+};