サムネイルを予め生成するように
This commit is contained in:
parent
75764e59e1
commit
15e4cf1243
9 changed files with 182 additions and 107 deletions
61
src/models/drive-file-thumbnail.ts
Normal file
61
src/models/drive-file-thumbnail.ts
Normal file
|
@ -0,0 +1,61 @@
|
|||
import * as mongo from 'mongodb';
|
||||
import monkDb, { nativeDbConn } from '../db/mongodb';
|
||||
|
||||
const DriveFileThumbnail = monkDb.get<IDriveFileThumbnail>('driveFileThumbnails.files');
|
||||
DriveFileThumbnail.createIndex('metadata.originalId', { sparse: true, unique: true });
|
||||
export default DriveFileThumbnail;
|
||||
|
||||
export const DriveFileThumbnailChunk = monkDb.get('driveFileThumbnails.chunks');
|
||||
|
||||
export const getDriveFileThumbnailBucket = async (): Promise<mongo.GridFSBucket> => {
|
||||
const db = await nativeDbConn();
|
||||
const bucket = new mongo.GridFSBucket(db, {
|
||||
bucketName: 'driveFileThumbnails'
|
||||
});
|
||||
return bucket;
|
||||
};
|
||||
|
||||
export type IMetadata = {
|
||||
originalId: mongo.ObjectID;
|
||||
};
|
||||
|
||||
export type IDriveFileThumbnail = {
|
||||
_id: mongo.ObjectID;
|
||||
uploadDate: Date;
|
||||
md5: string;
|
||||
filename: string;
|
||||
contentType: string;
|
||||
metadata: IMetadata;
|
||||
};
|
||||
|
||||
/**
|
||||
* DriveFileThumbnailを物理削除します
|
||||
*/
|
||||
export async function deleteDriveFileThumbnail(driveFile: string | mongo.ObjectID | IDriveFileThumbnail) {
|
||||
let d: IDriveFileThumbnail;
|
||||
|
||||
// Populate
|
||||
if (mongo.ObjectID.prototype.isPrototypeOf(driveFile)) {
|
||||
d = await DriveFileThumbnail.findOne({
|
||||
_id: driveFile
|
||||
});
|
||||
} else if (typeof driveFile === 'string') {
|
||||
d = await DriveFileThumbnail.findOne({
|
||||
_id: new mongo.ObjectID(driveFile)
|
||||
});
|
||||
} else {
|
||||
d = driveFile as IDriveFileThumbnail;
|
||||
}
|
||||
|
||||
if (d == null) return;
|
||||
|
||||
// このDriveFileThumbnailのチャンクをすべて削除
|
||||
await DriveFileThumbnailChunk.remove({
|
||||
files_id: d._id
|
||||
});
|
||||
|
||||
// このDriveFileThumbnailを削除
|
||||
await DriveFileThumbnail.remove({
|
||||
_id: d._id
|
||||
});
|
||||
}
|
|
@ -6,6 +6,7 @@ import monkDb, { nativeDbConn } from '../db/mongodb';
|
|||
import Note, { deleteNote } from './note';
|
||||
import MessagingMessage, { deleteMessagingMessage } from './messaging-message';
|
||||
import User from './user';
|
||||
import DriveFileThumbnail, { deleteDriveFileThumbnail } from './drive-file-thumbnail';
|
||||
|
||||
const DriveFile = monkDb.get<IDriveFile>('driveFiles.files');
|
||||
DriveFile.createIndex('metadata.uri', { sparse: true, unique: true });
|
||||
|
@ -13,7 +14,7 @@ export default DriveFile;
|
|||
|
||||
export const DriveFileChunk = monkDb.get('driveFiles.chunks');
|
||||
|
||||
const getGridFSBucket = async (): Promise<mongo.GridFSBucket> => {
|
||||
export const getDriveFileBucket = async (): Promise<mongo.GridFSBucket> => {
|
||||
const db = await nativeDbConn();
|
||||
const bucket = new mongo.GridFSBucket(db, {
|
||||
bucketName: 'driveFiles'
|
||||
|
@ -21,8 +22,6 @@ const getGridFSBucket = async (): Promise<mongo.GridFSBucket> => {
|
|||
return bucket;
|
||||
};
|
||||
|
||||
export { getGridFSBucket };
|
||||
|
||||
export type IMetadata = {
|
||||
properties: any;
|
||||
userId: mongo.ObjectID;
|
||||
|
@ -93,6 +92,11 @@ export async function deleteDriveFile(driveFile: string | mongo.ObjectID | IDriv
|
|||
}
|
||||
}
|
||||
|
||||
// このDriveFileのDriveFileThumbnailをすべて削除
|
||||
await Promise.all((
|
||||
await DriveFileThumbnail.find({ 'metadata.originalId': d._id })
|
||||
).map(x => deleteDriveFileThumbnail(x)));
|
||||
|
||||
// このDriveFileのチャンクをすべて削除
|
||||
await DriveFileChunk.remove({
|
||||
files_id: d._id
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue