From b7359694abc14fcca663dc771db0b46d7b8bf143 Mon Sep 17 00:00:00 2001 From: syuilo Date: Tue, 10 Apr 2018 02:34:47 +0900 Subject: [PATCH] Refactor --- src/services/drive/add-file.ts | 54 +++++++++++++--------------------- 1 file changed, 21 insertions(+), 33 deletions(-) diff --git a/src/services/drive/add-file.ts b/src/services/drive/add-file.ts index 64a2f1834..4889b357a 100644 --- a/src/services/drive/add-file.ts +++ b/src/services/drive/add-file.ts @@ -10,12 +10,12 @@ import * as debug from 'debug'; import fileType = require('file-type'); import prominence = require('prominence'); -import DriveFile, { IMetadata, getGridFSBucket } from '../../models/drive-file'; +import DriveFile, { IMetadata, getGridFSBucket, IDriveFile } from '../../models/drive-file'; import DriveFolder from '../../models/drive-folder'; import { pack } from '../../models/drive-file'; import event, { publishDriveStream } from '../../publishers/stream'; import getAcct from '../../acct/render'; -import config from '../../config'; +import { IUser } from '../../models/user'; const gm = _gm.subClass({ imageMagick: true @@ -34,20 +34,20 @@ const addToGridFS = (name: string, readable: stream.Readable, type: string, meta getGridFSBucket() .then(bucket => new Promise((resolve, reject) => { const writeStream = bucket.openUploadStream(name, { contentType: type, metadata }); - writeStream.once('finish', (doc) => { resolve(doc); }); + writeStream.once('finish', resolve); writeStream.on('error', reject); readable.pipe(writeStream); })); const addFile = async ( - user: any, + user: IUser, path: string, name: string = null, comment: string = null, folderId: mongodb.ObjectID = null, force: boolean = false, uri: string = null -) => { +): Promise => { log(`registering ${name} (user: ${getAcct(user)}, path: ${path})`); // Calculate hash, get content type and get file size @@ -251,13 +251,13 @@ const addFile = async ( * @return Object that represents added file */ export default (user: any, file: string | stream.Readable, ...args) => new Promise((resolve, reject) => { + const isStream = typeof file === 'object' && typeof file.read === 'function'; + // Get file path - new Promise((res: (v: [string, boolean]) => void, rej) => { + new Promise((res, rej) => { if (typeof file === 'string') { - res([file, false]); - return; - } - if (typeof file === 'object' && typeof file.read === 'function') { + res(file); + } else if (isStream) { tmpFile() .then(path => { const readable: stream.Readable = file; @@ -265,22 +265,23 @@ export default (user: any, file: string | stream.Readable, ...args) => new Promi readable .on('error', rej) .on('end', () => { - res([path, true]); + res(path); }) .pipe(writable) .on('error', rej); }) .catch(rej); + } else { + rej(new Error('un-compatible file.')); } - rej(new Error('un-compatible file.')); }) - .then(([path, shouldCleanup]): Promise => new Promise((res, rej) => { + .then(path => new Promise((res, rej) => { addFile(user, path, ...args) .then(file => { res(file); - if (shouldCleanup) { - fs.unlink(path, (e) => { - if (e) log(e.stack); + if (isStream) { + fs.unlink(path, e => { + if (e) console.error(e.stack); }); } }) @@ -288,26 +289,13 @@ export default (user: any, file: string | stream.Readable, ...args) => new Promi })) .then(file => { log(`drive file has been created ${file._id}`); + resolve(file); - pack(file).then(serializedFile => { + pack(file).then(packedFile => { // Publish drive_file_created event - event(user._id, 'drive_file_created', serializedFile); - publishDriveStream(user._id, '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, - userId: user._id.toString() - } - }); - } + event(user._id, 'drive_file_created', packedFile); + publishDriveStream(user._id, 'file_created', packedFile); }); }) .catch(reject);