2018-10-08 17:14:03 +00:00
|
|
|
import * as Minio from 'minio';
|
|
|
|
import * as uuid from 'uuid';
|
2018-10-11 12:14:20 +00:00
|
|
|
import * as promiseLimit from 'promise-limit';
|
|
|
|
import DriveFile, { DriveFileChunk, getDriveFileBucket, IDriveFile } from '../models/drive-file';
|
2018-10-08 17:14:03 +00:00
|
|
|
import DriveFileThumbnail, { DriveFileThumbnailChunk } from '../models/drive-file-thumbnail';
|
|
|
|
import config from '../config';
|
|
|
|
|
2018-10-11 12:14:20 +00:00
|
|
|
const limit = promiseLimit(16);
|
|
|
|
|
2018-10-08 17:14:03 +00:00
|
|
|
DriveFile.find({
|
|
|
|
$or: [{
|
2018-10-11 14:52:11 +00:00
|
|
|
'metadata.withoutChunks': { $exists: false }
|
2018-10-08 17:14:03 +00:00
|
|
|
}, {
|
2018-10-11 14:52:11 +00:00
|
|
|
'metadata.withoutChunks': false
|
2018-10-11 09:35:19 +00:00
|
|
|
}],
|
|
|
|
'metadata.deletedAt': { $exists: false }
|
2018-10-11 06:56:18 +00:00
|
|
|
}, {
|
|
|
|
fields: {
|
|
|
|
_id: true
|
|
|
|
}
|
2018-10-08 17:14:03 +00:00
|
|
|
}).then(async files => {
|
2018-10-11 06:56:18 +00:00
|
|
|
console.log(`there is ${files.length} files`);
|
|
|
|
|
2018-10-11 12:14:20 +00:00
|
|
|
await Promise.all(files.map(file => limit(() => job(file))));
|
2018-10-11 06:56:18 +00:00
|
|
|
|
2018-10-11 12:14:20 +00:00
|
|
|
console.log('ALL DONE');
|
|
|
|
});
|
2018-10-08 17:14:03 +00:00
|
|
|
|
2018-10-11 12:14:20 +00:00
|
|
|
async function job(file: IDriveFile): Promise<any> {
|
|
|
|
file = await DriveFile.findOne({ _id: file._id });
|
2018-10-08 17:14:03 +00:00
|
|
|
|
2018-10-11 12:14:20 +00:00
|
|
|
const minio = new Minio.Client(config.drive.config);
|
2018-10-08 17:14:03 +00:00
|
|
|
|
2018-10-11 19:01:45 +00:00
|
|
|
const name = file.filename.substr(0, 50);
|
2018-10-11 12:14:20 +00:00
|
|
|
const keyDir = `${config.drive.prefix}/${uuid.v4()}`;
|
|
|
|
const key = `${keyDir}/${name}`;
|
|
|
|
const thumbnailKeyDir = `${config.drive.prefix}/${uuid.v4()}`;
|
|
|
|
const thumbnailKey = `${thumbnailKeyDir}/${name}.thumbnail.jpg`;
|
2018-10-08 17:14:03 +00:00
|
|
|
|
2018-10-11 12:14:20 +00:00
|
|
|
const baseUrl = config.drive.baseUrl
|
|
|
|
|| `${ config.drive.config.useSSL ? 'https' : 'http' }://${ config.drive.config.endPoint }${ config.drive.config.port ? `:${config.drive.config.port}` : '' }/${ config.drive.bucket }`;
|
2018-10-08 17:14:03 +00:00
|
|
|
|
2018-10-11 12:14:20 +00:00
|
|
|
const bucket = await getDriveFileBucket();
|
|
|
|
const readable = bucket.openDownloadStream(file._id);
|
2018-10-08 17:14:03 +00:00
|
|
|
|
2018-10-11 12:14:20 +00:00
|
|
|
await minio.putObject(config.drive.bucket, key, readable, file.length, {
|
|
|
|
'Content-Type': file.contentType,
|
|
|
|
'Cache-Control': 'max-age=31536000, immutable'
|
|
|
|
});
|
2018-10-08 17:14:03 +00:00
|
|
|
|
2018-10-11 12:14:20 +00:00
|
|
|
await DriveFile.findOneAndUpdate({ _id: file._id }, {
|
|
|
|
$set: {
|
|
|
|
'metadata.withoutChunks': true,
|
|
|
|
'metadata.storage': 'minio',
|
|
|
|
'metadata.storageProps': {
|
|
|
|
key: key,
|
|
|
|
thumbnailKey: thumbnailKey
|
|
|
|
},
|
|
|
|
'metadata.url': `${ baseUrl }/${ keyDir }/${ encodeURIComponent(name) }`,
|
|
|
|
}
|
|
|
|
});
|
2018-10-08 17:14:03 +00:00
|
|
|
|
2018-10-11 12:14:20 +00:00
|
|
|
// チャンクをすべて削除
|
|
|
|
await DriveFileChunk.remove({
|
|
|
|
files_id: file._id
|
|
|
|
});
|
2018-10-08 17:14:03 +00:00
|
|
|
|
2018-10-11 12:14:20 +00:00
|
|
|
//#region サムネイルもあれば削除
|
|
|
|
const thumbnail = await DriveFileThumbnail.findOne({
|
|
|
|
'metadata.originalId': file._id
|
|
|
|
});
|
2018-10-08 20:46:21 +00:00
|
|
|
|
2018-10-11 12:14:20 +00:00
|
|
|
if (thumbnail) {
|
|
|
|
await DriveFileThumbnailChunk.remove({
|
|
|
|
files_id: thumbnail._id
|
|
|
|
});
|
|
|
|
|
|
|
|
await DriveFileThumbnail.remove({ _id: thumbnail._id });
|
|
|
|
}
|
|
|
|
//#endregion
|
|
|
|
|
|
|
|
console.log('done', file._id);
|
|
|
|
}
|