summaryrefslogtreecommitdiff
path: root/src/tools
diff options
context:
space:
mode:
authorsyuilo <syuilotan@yahoo.co.jp>2018-10-11 21:14:20 +0900
committersyuilo <syuilotan@yahoo.co.jp>2018-10-11 21:14:20 +0900
commit3a80b599860037f0b0f3221ef003413c548f66db (patch)
tree802cb020eed323c0210007561854528563ac11d5 /src/tools
parent10.8.0 (diff)
downloadsharkey-3a80b599860037f0b0f3221ef003413c548f66db.tar.gz
sharkey-3a80b599860037f0b0f3221ef003413c548f66db.tar.bz2
sharkey-3a80b599860037f0b0f3221ef003413c548f66db.zip
並列に処理するように
Diffstat (limited to 'src/tools')
-rw-r--r--src/tools/move-drive-files.ts100
1 files changed, 53 insertions, 47 deletions
diff --git a/src/tools/move-drive-files.ts b/src/tools/move-drive-files.ts
index 0c201ff8d5..782ea664af 100644
--- a/src/tools/move-drive-files.ts
+++ b/src/tools/move-drive-files.ts
@@ -1,10 +1,12 @@
import * as Minio from 'minio';
import * as uuid from 'uuid';
-const sequential = require('promise-sequential');
-import DriveFile, { DriveFileChunk, getDriveFileBucket } from '../models/drive-file';
+import * as promiseLimit from 'promise-limit';
+import DriveFile, { DriveFileChunk, getDriveFileBucket, IDriveFile } from '../models/drive-file';
import DriveFileThumbnail, { DriveFileThumbnailChunk } from '../models/drive-file-thumbnail';
import config from '../config';
+const limit = promiseLimit(16);
+
DriveFile.find({
$or: [{
withoutChunks: { $exists: false }
@@ -19,59 +21,63 @@ DriveFile.find({
}).then(async files => {
console.log(`there is ${files.length} files`);
- await sequential(files.map(file => async () => {
- file = await DriveFile.findOne({ _id: file._id });
+ await Promise.all(files.map(file => limit(() => job(file))));
- const minio = new Minio.Client(config.drive.config);
+ console.log('ALL DONE');
+});
- const name = file.filename;
- const keyDir = `${config.drive.prefix}/${uuid.v4()}`;
- const key = `${keyDir}/${name}`;
- const thumbnailKeyDir = `${config.drive.prefix}/${uuid.v4()}`;
- const thumbnailKey = `${thumbnailKeyDir}/${name}.thumbnail.jpg`;
+async function job(file: IDriveFile): Promise<any> {
+ file = await DriveFile.findOne({ _id: file._id });
- 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 }`;
+ const minio = new Minio.Client(config.drive.config);
- const bucket = await getDriveFileBucket();
- const readable = bucket.openDownloadStream(file._id);
+ const name = file.filename;
+ const keyDir = `${config.drive.prefix}/${uuid.v4()}`;
+ const key = `${keyDir}/${name}`;
+ const thumbnailKeyDir = `${config.drive.prefix}/${uuid.v4()}`;
+ const thumbnailKey = `${thumbnailKeyDir}/${name}.thumbnail.jpg`;
- await minio.putObject(config.drive.bucket, key, readable, file.length, {
- 'Content-Type': file.contentType,
- 'Cache-Control': 'max-age=31536000, immutable'
- });
+ 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 }`;
- 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) }`,
- }
- });
+ const bucket = await getDriveFileBucket();
+ const readable = bucket.openDownloadStream(file._id);
- // チャンクをすべて削除
- await DriveFileChunk.remove({
- files_id: file._id
- });
+ await minio.putObject(config.drive.bucket, key, readable, file.length, {
+ 'Content-Type': file.contentType,
+ 'Cache-Control': 'max-age=31536000, immutable'
+ });
- //#region サムネイルもあれば削除
- const thumbnail = await DriveFileThumbnail.findOne({
- 'metadata.originalId': file._id
- });
+ 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) }`,
+ }
+ });
- if (thumbnail) {
- await DriveFileThumbnailChunk.remove({
- files_id: thumbnail._id
- });
+ // チャンクをすべて削除
+ await DriveFileChunk.remove({
+ files_id: file._id
+ });
- await DriveFileThumbnail.remove({ _id: thumbnail._id });
- }
- //#endregion
+ //#region サムネイルもあれば削除
+ const thumbnail = await DriveFileThumbnail.findOne({
+ 'metadata.originalId': file._id
+ });
- console.log('done', file._id);
- }));
-});
+ if (thumbnail) {
+ await DriveFileThumbnailChunk.remove({
+ files_id: thumbnail._id
+ });
+
+ await DriveFileThumbnail.remove({ _id: thumbnail._id });
+ }
+ //#endregion
+
+ console.log('done', file._id);
+}