summaryrefslogtreecommitdiff
path: root/src/tools/move-drive-files.ts
blob: 8a1e944503e6ec8bb852524ea3c4ab7e62a4acf1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import * as Minio from 'minio';
import * as uuid from 'uuid';
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: [{
		'metadata.withoutChunks': { $exists: false }
	}, {
		'metadata.withoutChunks': false
	}],
	'metadata.deletedAt': { $exists: false }
}, {
	fields: {
		_id: true
	}
}).then(async files => {
	console.log(`there is ${files.length} files`);

	await Promise.all(files.map(file => limit(() => job(file))));

	console.log('ALL DONE');
});

async function job(file: IDriveFile): Promise<any> {
	file = await DriveFile.findOne({ _id: file._id });

	const minio = new Minio.Client(config.drive.config);

	const name = file.filename.substr(0, 50);
	const keyDir = `${config.drive.prefix}/${uuid.v4()}`;
	const key = `${keyDir}/${name}`;
	const thumbnailKeyDir = `${config.drive.prefix}/${uuid.v4()}`;
	const thumbnailKey = `${thumbnailKeyDir}/${name}.thumbnail.jpg`;

	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 bucket = await getDriveFileBucket();
	const readable = bucket.openDownloadStream(file._id);

	await minio.putObject(config.drive.bucket, key, readable, file.length, {
		'Content-Type': file.contentType,
		'Cache-Control': 'max-age=31536000, immutable'
	});

	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) }`,
		}
	});

	// チャンクをすべて削除
	await DriveFileChunk.remove({
		files_id: file._id
	});

	//#region サムネイルもあれば削除
	const thumbnail = await DriveFileThumbnail.findOne({
		'metadata.originalId': file._id
	});

	if (thumbnail) {
		await DriveFileThumbnailChunk.remove({
			files_id: thumbnail._id
		});

		await DriveFileThumbnail.remove({ _id: thumbnail._id });
	}
	//#endregion

	console.log('done', file._id);
}