Refactor
This commit is contained in:
parent
dcd851cb68
commit
43ae3c6e27
1 changed files with 21 additions and 33 deletions
|
@ -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<IDriveFile> => {
|
||||
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<any>((resolve, reject) => {
|
||||
const isStream = typeof file === 'object' && typeof file.read === 'function';
|
||||
|
||||
// Get file path
|
||||
new Promise((res: (v: [string, boolean]) => void, rej) => {
|
||||
new Promise<string>((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.'));
|
||||
}
|
||||
})
|
||||
.then(([path, shouldCleanup]): Promise<any> => new Promise((res, rej) => {
|
||||
.then(path => new Promise<IDriveFile>((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);
|
||||
|
|
Loading…
Reference in a new issue