1
0
mirror of https://github.com/MisskeyIO/misskey synced 2024-12-18 00:29:00 +09:00
MisskeyIO/src/models/drive-file.ts

231 lines
5.2 KiB
TypeScript
Raw Normal View History

2018-04-12 04:22:06 +09:00
import * as mongo from 'mongodb';
import * as deepcopy from 'deepcopy';
2018-02-02 08:06:01 +09:00
import { pack as packFolder } from './drive-folder';
2018-12-14 19:09:11 +09:00
import { pack as packUser } from './user';
2018-03-29 20:32:18 +09:00
import monkDb, { nativeDbConn } from '../db/mongodb';
2018-10-16 11:38:09 +09:00
import isObjectId from '../misc/is-objectid';
import getDriveFileUrl, { getOriginalUrl } from '../misc/get-drive-file-url';
2017-01-17 09:12:33 +09:00
2018-03-29 14:48:47 +09:00
const DriveFile = monkDb.get<IDriveFile>('driveFiles.files');
DriveFile.createIndex('md5');
2018-08-12 03:09:22 +09:00
DriveFile.createIndex('metadata.uri');
2018-10-13 01:00:43 +09:00
DriveFile.createIndex('metadata.userId');
2018-10-13 01:17:23 +09:00
DriveFile.createIndex('metadata.folderId');
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');
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;
_user: any;
2018-04-12 04:22:06 +09:00
folderId: mongo.ObjectID;
2018-04-03 23:45:13 +09:00
comment: string;
/**
* URL
*/
uri?: string;
/**
* URL for web() or original
* * or
*/
url?: string;
/**
* URL for thumbnail (thumbnailがなければなし)
* * or
*/
2018-08-16 07:17:04 +09:00
thumbnailUrl?: string;
/**
* URL for original (web用が生成されてない場合はurlがoriginalを指す)
* * or
*/
webpublicUrl?: string;
accessKey?: string;
2018-07-24 05:04:43 +09:00
src?: string;
2018-04-17 20:04:19 +09:00
deletedAt?: Date;
2018-10-31 22:35:02 +09:00
/**
* MongoDB内に保存されていないか否か
2018-10-31 22:35:02 +09:00
* or
* true
2018-10-31 22:35:02 +09:00
*/
2018-07-24 05:04:43 +09:00
withoutChunks?: boolean;
2018-10-31 22:35:02 +09:00
2018-07-24 06:21:21 +09:00
storage?: string;
/***
* ObjectStorage
*/
storageProps?: IStorageProps;
2018-07-20 02:40:37 +09:00
isSensitive?: boolean;
2018-07-25 09:54:03 +09:00
2018-10-23 07:04:00 +09:00
/**
* 稿ID一覧
*/
attachedNoteIds?: mongo.ObjectID[];
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
};
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 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-10-31 11:22:49 +09:00
export const packMany = (
files: any[],
options?: {
detail?: boolean
self?: boolean,
2018-12-14 19:09:11 +09:00
withUser?: boolean,
}
) => {
2018-10-31 11:22:49 +09:00
return Promise.all(files.map(f => pack(f, options)));
};
2018-02-02 08:06:01 +09:00
/**
* Pack a drive file for API response
*/
export const pack = (
file: any,
options?: {
detail?: boolean,
self?: boolean,
2018-12-14 19:09:11 +09:00
withUser?: boolean,
2018-02-02 08:06:01 +09:00
}
) => new Promise<any>(async (resolve, reject) => {
const opts = Object.assign({
detail: false,
self: false
2018-02-02 08:06:01 +09:00
}, options);
let _file: any;
// Populate the file if 'file' is ID
2018-10-16 11:38:09 +09:00
if (isObjectId(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);
}
// (データベースの欠損などで)ファイルがデータベース上に見つからなかったとき
if (_file == null) {
2018-10-11 02:19:21 +09:00
console.warn(`[DAMAGED DB] (missing) pkg: driveFile :: ${file}`);
2018-10-05 04:50:23 +09:00
return resolve(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-10-31 22:55:17 +09:00
_target.url = getDriveFileUrl(_file);
_target.thumbnailUrl = getDriveFileUrl(_file, true);
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-12-14 19:09:11 +09:00
if (opts.withUser) {
// Populate user
_target.user = await packUser(_file.metadata.userId);
}
2018-09-02 09:31:11 +09:00
delete _target.withoutChunks;
delete _target.storage;
delete _target.storageProps;
delete _target.isRemote;
2018-11-01 09:02:54 +09:00
delete _target._user;
2018-09-02 09:31:11 +09:00
if (opts.self) {
_target.url = getOriginalUrl(_file);
}
2018-02-02 08:06:01 +09:00
resolve(_target);
});