forked from FoundKeyGang/FoundKey
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 fileType = require('file-type');
|
||||||
import prominence = require('prominence');
|
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 DriveFolder from '../../models/drive-folder';
|
||||||
import { pack } from '../../models/drive-file';
|
import { pack } from '../../models/drive-file';
|
||||||
import event, { publishDriveStream } from '../../publishers/stream';
|
import event, { publishDriveStream } from '../../publishers/stream';
|
||||||
import getAcct from '../../acct/render';
|
import getAcct from '../../acct/render';
|
||||||
import config from '../../config';
|
import { IUser } from '../../models/user';
|
||||||
|
|
||||||
const gm = _gm.subClass({
|
const gm = _gm.subClass({
|
||||||
imageMagick: true
|
imageMagick: true
|
||||||
|
@ -34,20 +34,20 @@ const addToGridFS = (name: string, readable: stream.Readable, type: string, meta
|
||||||
getGridFSBucket()
|
getGridFSBucket()
|
||||||
.then(bucket => new Promise((resolve, reject) => {
|
.then(bucket => new Promise((resolve, reject) => {
|
||||||
const writeStream = bucket.openUploadStream(name, { contentType: type, metadata });
|
const writeStream = bucket.openUploadStream(name, { contentType: type, metadata });
|
||||||
writeStream.once('finish', (doc) => { resolve(doc); });
|
writeStream.once('finish', resolve);
|
||||||
writeStream.on('error', reject);
|
writeStream.on('error', reject);
|
||||||
readable.pipe(writeStream);
|
readable.pipe(writeStream);
|
||||||
}));
|
}));
|
||||||
|
|
||||||
const addFile = async (
|
const addFile = async (
|
||||||
user: any,
|
user: IUser,
|
||||||
path: string,
|
path: string,
|
||||||
name: string = null,
|
name: string = null,
|
||||||
comment: string = null,
|
comment: string = null,
|
||||||
folderId: mongodb.ObjectID = null,
|
folderId: mongodb.ObjectID = null,
|
||||||
force: boolean = false,
|
force: boolean = false,
|
||||||
uri: string = null
|
uri: string = null
|
||||||
) => {
|
): Promise<IDriveFile> => {
|
||||||
log(`registering ${name} (user: ${getAcct(user)}, path: ${path})`);
|
log(`registering ${name} (user: ${getAcct(user)}, path: ${path})`);
|
||||||
|
|
||||||
// Calculate hash, get content type and get file size
|
// Calculate hash, get content type and get file size
|
||||||
|
@ -251,13 +251,13 @@ const addFile = async (
|
||||||
* @return Object that represents added file
|
* @return Object that represents added file
|
||||||
*/
|
*/
|
||||||
export default (user: any, file: string | stream.Readable, ...args) => new Promise<any>((resolve, reject) => {
|
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
|
// Get file path
|
||||||
new Promise((res: (v: [string, boolean]) => void, rej) => {
|
new Promise<string>((res, rej) => {
|
||||||
if (typeof file === 'string') {
|
if (typeof file === 'string') {
|
||||||
res([file, false]);
|
res(file);
|
||||||
return;
|
} else if (isStream) {
|
||||||
}
|
|
||||||
if (typeof file === 'object' && typeof file.read === 'function') {
|
|
||||||
tmpFile()
|
tmpFile()
|
||||||
.then(path => {
|
.then(path => {
|
||||||
const readable: stream.Readable = file;
|
const readable: stream.Readable = file;
|
||||||
|
@ -265,22 +265,23 @@ export default (user: any, file: string | stream.Readable, ...args) => new Promi
|
||||||
readable
|
readable
|
||||||
.on('error', rej)
|
.on('error', rej)
|
||||||
.on('end', () => {
|
.on('end', () => {
|
||||||
res([path, true]);
|
res(path);
|
||||||
})
|
})
|
||||||
.pipe(writable)
|
.pipe(writable)
|
||||||
.on('error', rej);
|
.on('error', rej);
|
||||||
})
|
})
|
||||||
.catch(rej);
|
.catch(rej);
|
||||||
}
|
} else {
|
||||||
rej(new Error('un-compatible file.'));
|
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)
|
addFile(user, path, ...args)
|
||||||
.then(file => {
|
.then(file => {
|
||||||
res(file);
|
res(file);
|
||||||
if (shouldCleanup) {
|
if (isStream) {
|
||||||
fs.unlink(path, (e) => {
|
fs.unlink(path, e => {
|
||||||
if (e) log(e.stack);
|
if (e) console.error(e.stack);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -288,26 +289,13 @@ export default (user: any, file: string | stream.Readable, ...args) => new Promi
|
||||||
}))
|
}))
|
||||||
.then(file => {
|
.then(file => {
|
||||||
log(`drive file has been created ${file._id}`);
|
log(`drive file has been created ${file._id}`);
|
||||||
|
|
||||||
resolve(file);
|
resolve(file);
|
||||||
|
|
||||||
pack(file).then(serializedFile => {
|
pack(file).then(packedFile => {
|
||||||
// Publish drive_file_created event
|
// Publish drive_file_created event
|
||||||
event(user._id, 'drive_file_created', serializedFile);
|
event(user._id, 'drive_file_created', packedFile);
|
||||||
publishDriveStream(user._id, 'file_created', serializedFile);
|
publishDriveStream(user._id, 'file_created', packedFile);
|
||||||
|
|
||||||
// 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()
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
.catch(reject);
|
.catch(reject);
|
||||||
|
|
Loading…
Reference in a new issue