2018-04-11 12:22:06 -07:00
|
|
|
import * as mongo from 'mongodb';
|
2018-02-01 15:06:01 -08:00
|
|
|
import deepcopy = require('deepcopy');
|
|
|
|
import { pack as packFolder } from './drive-folder';
|
2018-04-01 21:15:53 -07:00
|
|
|
import config from '../config';
|
2018-03-29 04:32:18 -07:00
|
|
|
import monkDb, { nativeDbConn } from '../db/mongodb';
|
2018-04-11 12:22:06 -07:00
|
|
|
import Note, { deleteNote } from './note';
|
|
|
|
import MessagingMessage, { deleteMessagingMessage } from './messaging-message';
|
|
|
|
import User from './user';
|
2017-01-16 16:12:33 -08:00
|
|
|
|
2018-03-28 22:48:47 -07:00
|
|
|
const DriveFile = monkDb.get<IDriveFile>('driveFiles.files');
|
2017-01-31 16:29:44 -08:00
|
|
|
|
2018-04-03 07:45:13 -07:00
|
|
|
DriveFile.createIndex('metadata.uri', { sparse: true, unique: true });
|
|
|
|
|
2018-02-01 15:06:01 -08:00
|
|
|
export default DriveFile;
|
2016-12-28 14:49:51 -08:00
|
|
|
|
2018-04-11 12:22:06 -07:00
|
|
|
const getGridFSBucket = async (): Promise<mongo.GridFSBucket> => {
|
2017-11-05 23:32:01 -08:00
|
|
|
const db = await nativeDbConn();
|
2018-04-11 12:22:06 -07:00
|
|
|
const bucket = new mongo.GridFSBucket(db, {
|
2018-03-28 22:48:47 -07:00
|
|
|
bucketName: 'driveFiles'
|
2017-11-05 23:32:01 -08:00
|
|
|
});
|
|
|
|
return bucket;
|
|
|
|
};
|
2017-11-05 21:37:00 -08:00
|
|
|
|
2017-11-05 23:32:01 -08:00
|
|
|
export { getGridFSBucket };
|
2017-11-05 21:37:00 -08:00
|
|
|
|
2018-04-03 07:45:13 -07:00
|
|
|
export type IMetadata = {
|
|
|
|
properties: any;
|
2018-04-11 12:22:06 -07:00
|
|
|
userId: mongo.ObjectID;
|
|
|
|
folderId: mongo.ObjectID;
|
2018-04-03 07:45:13 -07:00
|
|
|
comment: string;
|
|
|
|
uri: string;
|
|
|
|
};
|
|
|
|
|
2018-02-01 15:06:01 -08:00
|
|
|
export type IDriveFile = {
|
2018-04-11 12:22:06 -07:00
|
|
|
_id: mongo.ObjectID;
|
2018-02-01 15:21:30 -08:00
|
|
|
uploadDate: Date;
|
|
|
|
md5: string;
|
|
|
|
filename: string;
|
2018-02-03 21:52:33 -08:00
|
|
|
contentType: string;
|
2018-04-03 07:45:13 -07:00
|
|
|
metadata: IMetadata;
|
2018-02-01 15:06:01 -08:00
|
|
|
};
|
|
|
|
|
2016-12-28 14:49:51 -08:00
|
|
|
export function validateFileName(name: string): boolean {
|
|
|
|
return (
|
|
|
|
(name.trim().length > 0) &&
|
|
|
|
(name.length <= 200) &&
|
|
|
|
(name.indexOf('\\') === -1) &&
|
|
|
|
(name.indexOf('/') === -1) &&
|
|
|
|
(name.indexOf('..') === -1)
|
|
|
|
);
|
|
|
|
}
|
2018-02-01 15:06:01 -08:00
|
|
|
|
2018-04-11 12:22:06 -07:00
|
|
|
/**
|
|
|
|
* DriveFileを物理削除します
|
|
|
|
*/
|
|
|
|
export async function deleteDriveFile(driveFile: string | mongo.ObjectID | IDriveFile) {
|
|
|
|
let d: IDriveFile;
|
|
|
|
|
|
|
|
// Populate
|
|
|
|
if (mongo.ObjectID.prototype.isPrototypeOf(driveFile)) {
|
|
|
|
d = await DriveFile.findOne({
|
|
|
|
_id: driveFile
|
|
|
|
});
|
|
|
|
} else if (typeof driveFile === 'string') {
|
|
|
|
d = await DriveFile.findOne({
|
|
|
|
_id: new mongo.ObjectID(driveFile)
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
d = driveFile as IDriveFile;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (d == null) return;
|
|
|
|
|
|
|
|
// このDriveFileを添付しているNoteをすべて削除
|
|
|
|
await Promise.all((
|
|
|
|
await Note.find({ mediaIds: d._id })
|
|
|
|
).map(x => deleteNote(x)));
|
|
|
|
|
|
|
|
// このDriveFileを添付しているMessagingMessageをすべて削除
|
|
|
|
await Promise.all((
|
|
|
|
await MessagingMessage.find({ fileId: d._id })
|
|
|
|
).map(x => deleteMessagingMessage(x)));
|
|
|
|
|
|
|
|
// このDriveFileがアバターやバナーに使われていたらそれらのプロパティをnullにする
|
|
|
|
const u = await User.findOne({ _id: d.metadata.userId });
|
|
|
|
if (u) {
|
2018-04-14 19:57:33 -07:00
|
|
|
if (u.avatarId && u.avatarId.equals(d._id)) {
|
2018-04-11 12:22:06 -07:00
|
|
|
await User.update({ _id: u._id }, { $set: { avatarId: null } });
|
|
|
|
}
|
2018-04-14 19:57:33 -07:00
|
|
|
if (u.bannerId && u.bannerId.equals(d._id)) {
|
2018-04-11 12:22:06 -07:00
|
|
|
await User.update({ _id: u._id }, { $set: { bannerId: null } });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-14 19:57:33 -07:00
|
|
|
// このDriveFileのチャンクをすべて削除
|
|
|
|
await monkDb.get('driveFiles.chunks').remove({
|
|
|
|
files_id: d._id
|
|
|
|
});
|
|
|
|
|
2018-04-11 12:22:06 -07:00
|
|
|
// このDriveFileを削除
|
|
|
|
await DriveFile.remove({
|
|
|
|
_id: d._id
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-02-01 15:06:01 -08:00
|
|
|
/**
|
|
|
|
* Pack a drive file for API response
|
|
|
|
*/
|
|
|
|
export const pack = (
|
|
|
|
file: any,
|
|
|
|
options?: {
|
|
|
|
detail: boolean
|
|
|
|
}
|
|
|
|
) => new Promise<any>(async (resolve, reject) => {
|
|
|
|
const opts = Object.assign({
|
|
|
|
detail: false
|
|
|
|
}, options);
|
|
|
|
|
|
|
|
let _file: any;
|
|
|
|
|
|
|
|
// Populate the file if 'file' is ID
|
2018-04-11 14:20:53 -07:00
|
|
|
if (mongo.ObjectID.prototype.isPrototypeOf(file)) {
|
2018-02-01 15:06:01 -08:00
|
|
|
_file = await DriveFile.findOne({
|
|
|
|
_id: file
|
|
|
|
});
|
|
|
|
} else if (typeof file === 'string') {
|
|
|
|
_file = await DriveFile.findOne({
|
2018-04-11 14:20:53 -07:00
|
|
|
_id: new mongo.ObjectID(file)
|
2018-02-01 15:06:01 -08:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
_file = deepcopy(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_file) return reject('invalid file arg.');
|
|
|
|
|
|
|
|
// rendered target
|
|
|
|
let _target: any = {};
|
|
|
|
|
|
|
|
_target.id = _file._id;
|
2018-03-28 22:48:47 -07:00
|
|
|
_target.createdAt = _file.uploadDate;
|
2018-02-01 15:06:01 -08:00
|
|
|
_target.name = _file.filename;
|
|
|
|
_target.type = _file.contentType;
|
|
|
|
_target.datasize = _file.length;
|
|
|
|
_target.md5 = _file.md5;
|
|
|
|
|
|
|
|
_target = Object.assign(_target, _file.metadata);
|
|
|
|
|
|
|
|
_target.url = `${config.drive_url}/${_target.id}/${encodeURIComponent(_target.name)}`;
|
|
|
|
|
|
|
|
if (_target.properties == null) _target.properties = {};
|
|
|
|
|
|
|
|
if (opts.detail) {
|
2018-03-28 22:48:47 -07:00
|
|
|
if (_target.folderId) {
|
2018-02-01 15:06:01 -08:00
|
|
|
// Populate folder
|
2018-03-28 22:48:47 -07:00
|
|
|
_target.folder = await packFolder(_target.folderId, {
|
2018-02-01 15:06:01 -08:00
|
|
|
detail: true
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
if (_target.tags) {
|
|
|
|
// Populate tags
|
|
|
|
_target.tags = await _target.tags.map(async (tag: any) =>
|
|
|
|
await serializeDriveTag(tag)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve(_target);
|
|
|
|
});
|