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