This commit is contained in:
syuilo 2018-04-17 19:55:58 +09:00
parent e69554f810
commit 0f67a544bd
2 changed files with 33 additions and 6 deletions

View File

@ -8,11 +8,11 @@ import MessagingMessage, { deleteMessagingMessage } from './messaging-message';
import User from './user'; import User from './user';
const DriveFile = monkDb.get<IDriveFile>('driveFiles.files'); const DriveFile = monkDb.get<IDriveFile>('driveFiles.files');
DriveFile.createIndex('metadata.uri', { sparse: true, unique: true }); DriveFile.createIndex('metadata.uri', { sparse: true, unique: true });
export default DriveFile; export default DriveFile;
export const DriveFileChunk = monkDb.get('driveFiles.chunks');
const getGridFSBucket = async (): Promise<mongo.GridFSBucket> => { const getGridFSBucket = async (): Promise<mongo.GridFSBucket> => {
const db = await nativeDbConn(); const db = await nativeDbConn();
const bucket = new mongo.GridFSBucket(db, { const bucket = new mongo.GridFSBucket(db, {
@ -93,7 +93,7 @@ export async function deleteDriveFile(driveFile: string | mongo.ObjectID | IDriv
} }
// このDriveFileのチャンクをすべて削除 // このDriveFileのチャンクをすべて削除
await monkDb.get('driveFiles.chunks').remove({ await DriveFileChunk.remove({
files_id: d._id files_id: d._id
}); });

View File

@ -10,12 +10,12 @@ import * as debug from 'debug';
import fileType = require('file-type'); import fileType = require('file-type');
import prominence = require('prominence'); import prominence = require('prominence');
import DriveFile, { IMetadata, getGridFSBucket, IDriveFile } from '../../models/drive-file'; import DriveFile, { IMetadata, getGridFSBucket, IDriveFile, DriveFileChunk } from '../../models/drive-file';
import DriveFolder from '../../models/drive-folder'; import DriveFolder from '../../models/drive-folder';
import { pack } from '../../models/drive-file'; import { pack } from '../../models/drive-file';
import event, { publishDriveStream } from '../../publishers/stream'; import event, { publishDriveStream } from '../../publishers/stream';
import getAcct from '../../acct/render'; import getAcct from '../../acct/render';
import { IUser } from '../../models/user'; import { IUser, isLocalUser } from '../../models/user';
const gm = _gm.subClass({ const gm = _gm.subClass({
imageMagick: true imageMagick: true
@ -207,7 +207,34 @@ const addFile = async (
// If usage limit exceeded // If usage limit exceeded
if (usage + size > user.driveCapacity) { if (usage + size > user.driveCapacity) {
throw 'no-free-space'; if (isLocalUser(user)) {
throw 'no-free-space';
} else {
//#region (アバターまたはバナーを含まず)最も古いファイルを削除する
const oldFile = await DriveFile.findOne({
_id: {
$nin: [user.avatarId, user.bannerId]
}
}, {
sort: {
_id: 1
}
});
if (oldFile) {
// チャンクをすべて削除
DriveFileChunk.remove({
files_id: oldFile._id
});
DriveFile.update({ _id: oldFile._id }, {
$set: {
'metadata.deletedAt': new Date()
}
});
}
//#endregion
}
} }
})() })()
]); ]);