summaryrefslogtreecommitdiff
path: root/src/api/endpoints/drive
diff options
context:
space:
mode:
authorotofune <otofune@gmail.com>2017-11-14 03:46:30 +0900
committerotofune <otofune@gmail.com>2017-11-14 03:46:30 +0900
commit64aedcaa6b3e9170e77c428ee306830464b42bcf (patch)
treeecb3ccaa2cf310554ff92d1d5c85b62c8c99fb64 /src/api/endpoints/drive
parentv3066 (diff)
downloadsharkey-64aedcaa6b3e9170e77c428ee306830464b42bcf.tar.gz
sharkey-64aedcaa6b3e9170e77c428ee306830464b42bcf.tar.bz2
sharkey-64aedcaa6b3e9170e77c428ee306830464b42bcf.zip
add-file-to-drive - Promise百烈拳とメモリ削減
Diffstat (limited to 'src/api/endpoints/drive')
-rw-r--r--src/api/endpoints/drive/files/upload_from_url.ts74
1 files changed, 61 insertions, 13 deletions
diff --git a/src/api/endpoints/drive/files/upload_from_url.ts b/src/api/endpoints/drive/files/upload_from_url.ts
index 46cfffb69c..9c759994e0 100644
--- a/src/api/endpoints/drive/files/upload_from_url.ts
+++ b/src/api/endpoints/drive/files/upload_from_url.ts
@@ -2,11 +2,17 @@
* Module dependencies
*/
import * as URL from 'url';
-const download = require('download');
import $ from 'cafy';
import { validateFileName } from '../../../models/drive-file';
import serialize from '../../../serializers/drive-file';
import create from '../../../common/add-file-to-drive';
+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')
/**
* Create a file from a URL
@@ -15,7 +21,7 @@ import create from '../../../common/add-file-to-drive';
* @param {any} user
* @return {Promise<any>}
*/
-module.exports = (params, user) => new Promise(async (res, rej) => {
+module.exports = (params, user) => new Promise((res, rej) => {
// Get 'url' parameter
// TODO: Validate this url
const [url, urlErr] = $(params.url).string().$;
@@ -30,15 +36,57 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
const [folderId = null, folderIdErr] = $(params.folder_id).optional.nullable.id().$;
if (folderIdErr) return rej('invalid folder_id param');
- // Download file
- const data = await download(url);
-
- // Create file
- const driveFile = await create(user, data, name, null, folderId);
-
- // Serialize
- const fileObj = await serialize(driveFile);
-
- // Response
- res(fileObj);
+ // Create temp file
+ new Promise((res, 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)
});