2018-04-12 04:22:06 +09:00
|
|
|
import * as mongo from 'mongodb';
|
2018-06-18 09:54:53 +09:00
|
|
|
const deepcopy = require('deepcopy');
|
2018-02-02 08:06:01 +09:00
|
|
|
import { pack as packFolder } from './drive-folder';
|
2018-04-02 13:15:53 +09:00
|
|
|
import config from '../config';
|
2018-03-29 20:32:18 +09:00
|
|
|
import monkDb, { nativeDbConn } from '../db/mongodb';
|
2018-04-12 04:22:06 +09:00
|
|
|
import Note, { deleteNote } from './note';
|
|
|
|
import MessagingMessage, { deleteMessagingMessage } from './messaging-message';
|
|
|
|
import User from './user';
|
2018-05-03 20:03:14 +09:00
|
|
|
import DriveFileThumbnail, { deleteDriveFileThumbnail } from './drive-file-thumbnail';
|
2017-01-17 09:12:33 +09:00
|
|
|
|
2018-03-29 14:48:47 +09:00
|
|
|
const DriveFile = monkDb.get<IDriveFile>('driveFiles.files');
|
2018-06-10 08:21:48 +09:00
|
|
|
DriveFile.createIndex('md5');
|
2018-08-12 03:09:22 +09:00
|
|
|
DriveFile.createIndex('metadata.uri');
|
2018-02-02 08:06:01 +09:00
|
|
|
export default DriveFile;
|
2016-12-29 07:49:51 +09:00
|
|
|
|
2018-04-17 19:55:58 +09:00
|
|
|
export const DriveFileChunk = monkDb.get('driveFiles.chunks');
|
|
|
|
|
2018-05-03 20:03:14 +09:00
|
|
|
export const getDriveFileBucket = async (): Promise<mongo.GridFSBucket> => {
|
2017-11-06 16:32:01 +09:00
|
|
|
const db = await nativeDbConn();
|
2018-04-12 04:22:06 +09:00
|
|
|
const bucket = new mongo.GridFSBucket(db, {
|
2018-03-29 14:48:47 +09:00
|
|
|
bucketName: 'driveFiles'
|
2017-11-06 16:32:01 +09:00
|
|
|
});
|
|
|
|
return bucket;
|
|
|
|
};
|
2017-11-06 14:37:00 +09:00
|
|
|
|
2018-04-03 23:45:13 +09:00
|
|
|
export type IMetadata = {
|
|
|
|
properties: any;
|
2018-04-12 04:22:06 +09:00
|
|
|
userId: mongo.ObjectID;
|
2018-05-04 15:30:56 +09:00
|
|
|
_user: any;
|
2018-04-12 04:22:06 +09:00
|
|
|
folderId: mongo.ObjectID;
|
2018-04-03 23:45:13 +09:00
|
|
|
comment: string;
|
2018-05-04 18:32:03 +09:00
|
|
|
uri?: string;
|
|
|
|
url?: string;
|
2018-08-16 07:17:04 +09:00
|
|
|
thumbnailUrl?: string;
|
2018-07-24 05:04:43 +09:00
|
|
|
src?: string;
|
2018-04-17 20:04:19 +09:00
|
|
|
deletedAt?: Date;
|
2018-07-24 05:04:43 +09:00
|
|
|
withoutChunks?: boolean;
|
2018-07-24 06:21:21 +09:00
|
|
|
storage?: string;
|
|
|
|
storageProps?: any;
|
2018-07-20 02:40:37 +09:00
|
|
|
isSensitive?: boolean;
|
2018-07-25 09:54:03 +09:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 外部の(信頼されていない)URLへの直リンクか否か
|
|
|
|
*/
|
2018-07-25 00:29:18 +09:00
|
|
|
isRemote?: boolean;
|
2018-04-03 23:45:13 +09:00
|
|
|
};
|
|
|
|
|
2018-02-02 08:06:01 +09:00
|
|
|
export type IDriveFile = {
|
2018-04-12 04:22:06 +09:00
|
|
|
_id: mongo.ObjectID;
|
2018-02-02 08:21:30 +09:00
|
|
|
uploadDate: Date;
|
|
|
|
md5: string;
|
|
|
|
filename: string;
|
2018-02-04 14:52:33 +09:00
|
|
|
contentType: string;
|
2018-04-03 23:45:13 +09:00
|
|
|
metadata: IMetadata;
|
2018-08-18 23:48:54 +09:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ファイルサイズ
|
|
|
|
*/
|
|
|
|
length: number;
|
2018-02-02 08:06:01 +09:00
|
|
|
};
|
|
|
|
|
2016-12-29 07:49:51 +09: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-02 08:06:01 +09:00
|
|
|
|
2018-04-12 04:22:06 +09: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((
|
2018-09-05 19:32:46 +09:00
|
|
|
await Note.find({ fileIds: d._id })
|
2018-04-12 04:22:06 +09:00
|
|
|
).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-15 11:57:33 +09:00
|
|
|
if (u.avatarId && u.avatarId.equals(d._id)) {
|
2018-04-12 04:22:06 +09:00
|
|
|
await User.update({ _id: u._id }, { $set: { avatarId: null } });
|
|
|
|
}
|
2018-04-15 11:57:33 +09:00
|
|
|
if (u.bannerId && u.bannerId.equals(d._id)) {
|
2018-04-12 04:22:06 +09:00
|
|
|
await User.update({ _id: u._id }, { $set: { bannerId: null } });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-03 20:03:14 +09:00
|
|
|
// このDriveFileのDriveFileThumbnailをすべて削除
|
|
|
|
await Promise.all((
|
|
|
|
await DriveFileThumbnail.find({ 'metadata.originalId': d._id })
|
|
|
|
).map(x => deleteDriveFileThumbnail(x)));
|
|
|
|
|
2018-04-15 11:57:33 +09:00
|
|
|
// このDriveFileのチャンクをすべて削除
|
2018-04-17 19:55:58 +09:00
|
|
|
await DriveFileChunk.remove({
|
2018-04-15 11:57:33 +09:00
|
|
|
files_id: d._id
|
|
|
|
});
|
|
|
|
|
2018-04-12 04:22:06 +09:00
|
|
|
// このDriveFileを削除
|
|
|
|
await DriveFile.remove({
|
|
|
|
_id: d._id
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-10-05 01:43:47 +09:00
|
|
|
export const packMany = async (
|
|
|
|
files: any[],
|
|
|
|
options?: {
|
|
|
|
detail: boolean
|
|
|
|
}
|
|
|
|
) => {
|
|
|
|
return (await Promise.all(files.map(f => pack(f, options)))).filter(x => x != null);
|
|
|
|
};
|
|
|
|
|
2018-02-02 08:06:01 +09: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-12 06:20:53 +09:00
|
|
|
if (mongo.ObjectID.prototype.isPrototypeOf(file)) {
|
2018-02-02 08:06:01 +09:00
|
|
|
_file = await DriveFile.findOne({
|
|
|
|
_id: file
|
|
|
|
});
|
|
|
|
} else if (typeof file === 'string') {
|
|
|
|
_file = await DriveFile.findOne({
|
2018-04-12 06:20:53 +09:00
|
|
|
_id: new mongo.ObjectID(file)
|
2018-02-02 08:06:01 +09:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
_file = deepcopy(file);
|
|
|
|
}
|
|
|
|
|
2018-10-05 01:43:47 +09:00
|
|
|
// (データベースの欠損などで)ファイルがデータベース上に見つからなかったとき
|
|
|
|
if (_file == null) {
|
|
|
|
console.warn(`in packaging driveFile: driveFile not found on database: ${_file}`);
|
|
|
|
return null;
|
|
|
|
}
|
2018-02-02 08:06:01 +09:00
|
|
|
|
|
|
|
// rendered target
|
|
|
|
let _target: any = {};
|
|
|
|
|
|
|
|
_target.id = _file._id;
|
2018-03-29 14:48:47 +09:00
|
|
|
_target.createdAt = _file.uploadDate;
|
2018-02-02 08:06:01 +09:00
|
|
|
_target.name = _file.filename;
|
|
|
|
_target.type = _file.contentType;
|
|
|
|
_target.datasize = _file.length;
|
|
|
|
_target.md5 = _file.md5;
|
|
|
|
|
|
|
|
_target = Object.assign(_target, _file.metadata);
|
|
|
|
|
2018-07-24 05:04:43 +09:00
|
|
|
_target.url = _file.metadata.url ? _file.metadata.url : `${config.drive_url}/${_target.id}/${encodeURIComponent(_target.name)}`;
|
2018-08-16 08:18:41 +09:00
|
|
|
_target.thumbnailUrl = _file.metadata.thumbnailUrl ? _file.metadata.thumbnailUrl : _file.metadata.url ? _file.metadata.url : `${config.drive_url}/${_target.id}/${encodeURIComponent(_target.name)}?thumbnail`;
|
2018-07-25 00:29:18 +09:00
|
|
|
_target.isRemote = _file.metadata.isRemote;
|
2018-02-02 08:06:01 +09:00
|
|
|
|
|
|
|
if (_target.properties == null) _target.properties = {};
|
|
|
|
|
|
|
|
if (opts.detail) {
|
2018-03-29 14:48:47 +09:00
|
|
|
if (_target.folderId) {
|
2018-02-02 08:06:01 +09:00
|
|
|
// Populate folder
|
2018-03-29 14:48:47 +09:00
|
|
|
_target.folder = await packFolder(_target.folderId, {
|
2018-02-02 08:06:01 +09:00
|
|
|
detail: true
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
if (_target.tags) {
|
|
|
|
// Populate tags
|
|
|
|
_target.tags = await _target.tags.map(async (tag: any) =>
|
|
|
|
await serializeDriveTag(tag)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
2018-09-02 09:31:11 +09:00
|
|
|
delete _target.withoutChunks;
|
|
|
|
delete _target.storage;
|
|
|
|
delete _target.storageProps;
|
|
|
|
delete _target.isRemote;
|
|
|
|
|
2018-02-02 08:06:01 +09:00
|
|
|
resolve(_target);
|
|
|
|
});
|