2018-04-11 22:22:06 +03:00
|
|
|
import * as mongo from 'mongodb';
|
2018-06-18 03:54:53 +03:00
|
|
|
const deepcopy = require('deepcopy');
|
2018-02-02 01:06:01 +02:00
|
|
|
import { pack as packFolder } from './drive-folder';
|
2018-03-29 14:32:18 +03:00
|
|
|
import monkDb, { nativeDbConn } from '../db/mongodb';
|
2018-10-16 05:38:09 +03:00
|
|
|
import isObjectId from '../misc/is-objectid';
|
2018-10-31 15:35:02 +02:00
|
|
|
import getDriveFileUrl from '../misc/get-drive-file-url';
|
2017-01-17 02:12:33 +02:00
|
|
|
|
2018-03-29 08:48:47 +03:00
|
|
|
const DriveFile = monkDb.get<IDriveFile>('driveFiles.files');
|
2018-06-10 02:21:48 +03:00
|
|
|
DriveFile.createIndex('md5');
|
2018-08-11 21:09:22 +03:00
|
|
|
DriveFile.createIndex('metadata.uri');
|
2018-10-12 19:00:43 +03:00
|
|
|
DriveFile.createIndex('metadata.userId');
|
2018-10-12 19:17:23 +03:00
|
|
|
DriveFile.createIndex('metadata.folderId');
|
2018-02-02 01:06:01 +02:00
|
|
|
export default DriveFile;
|
2016-12-29 00:49:51 +02:00
|
|
|
|
2018-04-17 13:55:58 +03:00
|
|
|
export const DriveFileChunk = monkDb.get('driveFiles.chunks');
|
|
|
|
|
2018-05-03 14:03:14 +03:00
|
|
|
export const getDriveFileBucket = async (): Promise<mongo.GridFSBucket> => {
|
2017-11-06 09:32:01 +02:00
|
|
|
const db = await nativeDbConn();
|
2018-04-11 22:22:06 +03:00
|
|
|
const bucket = new mongo.GridFSBucket(db, {
|
2018-03-29 08:48:47 +03:00
|
|
|
bucketName: 'driveFiles'
|
2017-11-06 09:32:01 +02:00
|
|
|
});
|
|
|
|
return bucket;
|
|
|
|
};
|
2017-11-06 07:37:00 +02:00
|
|
|
|
2018-04-03 17:45:13 +03:00
|
|
|
export type IMetadata = {
|
|
|
|
properties: any;
|
2018-04-11 22:22:06 +03:00
|
|
|
userId: mongo.ObjectID;
|
2018-05-04 09:30:56 +03:00
|
|
|
_user: any;
|
2018-04-11 22:22:06 +03:00
|
|
|
folderId: mongo.ObjectID;
|
2018-04-03 17:45:13 +03:00
|
|
|
comment: string;
|
2018-05-04 12:32:03 +03:00
|
|
|
uri?: string;
|
|
|
|
url?: string;
|
2018-08-16 01:17:04 +03:00
|
|
|
thumbnailUrl?: string;
|
2018-07-23 23:04:43 +03:00
|
|
|
src?: string;
|
2018-04-17 14:04:19 +03:00
|
|
|
deletedAt?: Date;
|
2018-10-31 15:35:02 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* このファイルの中身データがMongoDB内に保存されているのか否か
|
|
|
|
* オブジェクトストレージを利用している or リモートサーバーへの直リンクである
|
|
|
|
* な場合は false になります
|
|
|
|
*/
|
2018-07-23 23:04:43 +03:00
|
|
|
withoutChunks?: boolean;
|
2018-10-31 15:35:02 +02:00
|
|
|
|
2018-07-24 00:21:21 +03:00
|
|
|
storage?: string;
|
|
|
|
storageProps?: any;
|
2018-07-19 20:40:37 +03:00
|
|
|
isSensitive?: boolean;
|
2018-07-25 03:54:03 +03:00
|
|
|
|
2018-10-23 01:04:00 +03:00
|
|
|
/**
|
|
|
|
* このファイルが添付された投稿のID一覧
|
|
|
|
*/
|
|
|
|
attachedNoteIds?: mongo.ObjectID[];
|
|
|
|
|
2018-07-25 03:54:03 +03:00
|
|
|
/**
|
|
|
|
* 外部の(信頼されていない)URLへの直リンクか否か
|
|
|
|
*/
|
2018-07-24 18:29:18 +03:00
|
|
|
isRemote?: boolean;
|
2018-04-03 17:45:13 +03:00
|
|
|
};
|
|
|
|
|
2018-02-02 01:06:01 +02:00
|
|
|
export type IDriveFile = {
|
2018-04-11 22:22:06 +03:00
|
|
|
_id: mongo.ObjectID;
|
2018-02-02 01:21:30 +02:00
|
|
|
uploadDate: Date;
|
|
|
|
md5: string;
|
|
|
|
filename: string;
|
2018-02-04 07:52:33 +02:00
|
|
|
contentType: string;
|
2018-04-03 17:45:13 +03:00
|
|
|
metadata: IMetadata;
|
2018-08-18 17:48:54 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ファイルサイズ
|
|
|
|
*/
|
|
|
|
length: number;
|
2018-02-02 01:06:01 +02:00
|
|
|
};
|
|
|
|
|
2016-12-29 00:49:51 +02: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 01:06:01 +02:00
|
|
|
|
2018-10-31 04:22:49 +02:00
|
|
|
export const packMany = (
|
2018-10-04 19:43:47 +03:00
|
|
|
files: any[],
|
|
|
|
options?: {
|
|
|
|
detail: boolean
|
|
|
|
}
|
|
|
|
) => {
|
2018-10-31 04:22:49 +02:00
|
|
|
return Promise.all(files.map(f => pack(f, options)));
|
2018-10-04 19:43:47 +03:00
|
|
|
};
|
|
|
|
|
2018-02-02 01:06:01 +02: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-10-16 05:38:09 +03:00
|
|
|
if (isObjectId(file)) {
|
2018-02-02 01:06:01 +02:00
|
|
|
_file = await DriveFile.findOne({
|
|
|
|
_id: file
|
|
|
|
});
|
|
|
|
} else if (typeof file === 'string') {
|
|
|
|
_file = await DriveFile.findOne({
|
2018-04-12 00:20:53 +03:00
|
|
|
_id: new mongo.ObjectID(file)
|
2018-02-02 01:06:01 +02:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
_file = deepcopy(file);
|
|
|
|
}
|
|
|
|
|
2018-10-04 19:43:47 +03:00
|
|
|
// (データベースの欠損などで)ファイルがデータベース上に見つからなかったとき
|
|
|
|
if (_file == null) {
|
2018-10-10 20:19:21 +03:00
|
|
|
console.warn(`[DAMAGED DB] (missing) pkg: driveFile :: ${file}`);
|
2018-10-04 22:50:23 +03:00
|
|
|
return resolve(null);
|
2018-10-04 19:43:47 +03:00
|
|
|
}
|
2018-02-02 01:06:01 +02:00
|
|
|
|
|
|
|
// rendered target
|
|
|
|
let _target: any = {};
|
|
|
|
|
|
|
|
_target.id = _file._id;
|
2018-03-29 08:48:47 +03:00
|
|
|
_target.createdAt = _file.uploadDate;
|
2018-02-02 01:06:01 +02:00
|
|
|
_target.name = _file.filename;
|
|
|
|
_target.type = _file.contentType;
|
|
|
|
_target.datasize = _file.length;
|
|
|
|
_target.md5 = _file.md5;
|
|
|
|
|
|
|
|
_target = Object.assign(_target, _file.metadata);
|
|
|
|
|
2018-10-31 15:35:02 +02:00
|
|
|
_target.url = getDriveFileUrl(file);
|
|
|
|
_target.thumbnailUrl = getDriveFileUrl(file, true);
|
2018-07-24 18:29:18 +03:00
|
|
|
_target.isRemote = _file.metadata.isRemote;
|
2018-02-02 01:06:01 +02:00
|
|
|
|
|
|
|
if (_target.properties == null) _target.properties = {};
|
|
|
|
|
|
|
|
if (opts.detail) {
|
2018-03-29 08:48:47 +03:00
|
|
|
if (_target.folderId) {
|
2018-02-02 01:06:01 +02:00
|
|
|
// Populate folder
|
2018-03-29 08:48:47 +03:00
|
|
|
_target.folder = await packFolder(_target.folderId, {
|
2018-02-02 01:06:01 +02:00
|
|
|
detail: true
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
if (_target.tags) {
|
|
|
|
// Populate tags
|
|
|
|
_target.tags = await _target.tags.map(async (tag: any) =>
|
|
|
|
await serializeDriveTag(tag)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
2018-09-02 03:31:11 +03:00
|
|
|
delete _target.withoutChunks;
|
|
|
|
delete _target.storage;
|
|
|
|
delete _target.storageProps;
|
|
|
|
delete _target.isRemote;
|
|
|
|
|
2018-02-02 01:06:01 +02:00
|
|
|
resolve(_target);
|
|
|
|
});
|