2017-11-06 14:37:00 +09:00
|
|
|
import * as mongodb from 'mongodb';
|
2018-02-02 08:06:01 +09:00
|
|
|
import deepcopy = require('deepcopy');
|
|
|
|
import { pack as packFolder } from './drive-folder';
|
2018-03-29 01:20:40 +09:00
|
|
|
import config from '../../../conf';
|
|
|
|
import monkDb, { nativeDbConn } from '../../../db/mongodb';
|
2017-01-17 09:12:33 +09:00
|
|
|
|
2018-03-29 14:48:47 +09:00
|
|
|
const DriveFile = monkDb.get<IDriveFile>('driveFiles.files');
|
2017-02-01 09:29:44 +09:00
|
|
|
|
2018-02-02 08:06:01 +09:00
|
|
|
export default DriveFile;
|
2016-12-29 07:49:51 +09:00
|
|
|
|
2017-11-06 14:37:00 +09:00
|
|
|
const getGridFSBucket = async (): Promise<mongodb.GridFSBucket> => {
|
2017-11-06 16:32:01 +09:00
|
|
|
const db = await nativeDbConn();
|
2017-11-06 14:37:00 +09:00
|
|
|
const bucket = new mongodb.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
|
|
|
|
2017-11-06 16:32:01 +09:00
|
|
|
export { getGridFSBucket };
|
2017-11-06 14:37:00 +09:00
|
|
|
|
2018-02-02 08:06:01 +09:00
|
|
|
export type IDriveFile = {
|
|
|
|
_id: mongodb.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-02-02 08:21:30 +09:00
|
|
|
metadata: {
|
|
|
|
properties: any;
|
2018-03-29 14:48:47 +09:00
|
|
|
userId: mongodb.ObjectID;
|
|
|
|
folderId: mongodb.ObjectID;
|
2018-02-02 08:21:30 +09:00
|
|
|
}
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* Pack a drive file for API response
|
|
|
|
*
|
|
|
|
* @param {any} file
|
|
|
|
* @param {any} options?
|
|
|
|
* @return {Promise<any>}
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
if (mongodb.ObjectID.prototype.isPrototypeOf(file)) {
|
|
|
|
_file = await DriveFile.findOne({
|
|
|
|
_id: file
|
|
|
|
});
|
|
|
|
} else if (typeof file === 'string') {
|
|
|
|
_file = await DriveFile.findOne({
|
|
|
|
_id: new mongodb.ObjectID(file)
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
_file = deepcopy(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_file) return reject('invalid file arg.');
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
_target.url = `${config.drive_url}/${_target.id}/${encodeURIComponent(_target.name)}`;
|
|
|
|
|
|
|
|
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)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve(_target);
|
|
|
|
});
|