Update migrate.ts
This commit is contained in:
parent
fa6eae2937
commit
54a5246061
|
@ -1,20 +1,48 @@
|
||||||
import mongo from 'monk';
|
import monk from 'monk';
|
||||||
|
import * as mongo from 'mongodb';
|
||||||
|
import * as fs from 'fs';
|
||||||
|
import * as uuid from 'uuid';
|
||||||
import config from './config';
|
import config from './config';
|
||||||
import { initDb } from './db/postgre';
|
import { initDb } from './db/postgre';
|
||||||
import { User } from './models/entities/user';
|
import { User } from './models/entities/user';
|
||||||
import { getRepository } from 'typeorm';
|
import { getRepository } from 'typeorm';
|
||||||
import generateUserToken from './server/api/common/generate-native-user-token';
|
import generateUserToken from './server/api/common/generate-native-user-token';
|
||||||
import { DriveFile } from './models/entities/drive-file';
|
import { DriveFile } from './models/entities/drive-file';
|
||||||
|
import { InternalStorage } from './services/drive/internal-storage';
|
||||||
|
import { createTemp } from './misc/create-temp';
|
||||||
|
|
||||||
const u = (config as any).mongodb.user ? encodeURIComponent((config as any).mongodb.user) : null;
|
const u = (config as any).mongodb.user ? encodeURIComponent((config as any).mongodb.user) : null;
|
||||||
const p = (config as any).mongodb.pass ? encodeURIComponent((config as any).mongodb.pass) : null;
|
const p = (config as any).mongodb.pass ? encodeURIComponent((config as any).mongodb.pass) : null;
|
||||||
|
|
||||||
const uri = `mongodb://${u && p ? `${u}:${p}@` : ''}${(config as any).mongodb.host}:${(config as any).mongodb.port}/${(config as any).mongodb.db}`;
|
const uri = `mongodb://${u && p ? `${u}:${p}@` : ''}${(config as any).mongodb.host}:${(config as any).mongodb.port}/${(config as any).mongodb.db}`;
|
||||||
|
|
||||||
const db = mongo(uri);
|
const db = monk(uri);
|
||||||
|
let mdb: mongo.Db;
|
||||||
|
|
||||||
|
const nativeDbConn = async (): Promise<mongo.Db> => {
|
||||||
|
if (mdb) return mdb;
|
||||||
|
|
||||||
|
const db = await ((): Promise<mongo.Db> => new Promise((resolve, reject) => {
|
||||||
|
mongo.MongoClient.connect(uri, { useNewUrlParser: true }, (e: Error, client: any) => {
|
||||||
|
if (e) return reject(e);
|
||||||
|
resolve(client.db((config as any).mongo.db));
|
||||||
|
});
|
||||||
|
}))();
|
||||||
|
|
||||||
|
mdb = db;
|
||||||
|
|
||||||
|
return db;
|
||||||
|
};
|
||||||
|
|
||||||
const _User = db.get<any>('users');
|
const _User = db.get<any>('users');
|
||||||
const _DriveFile = db.get<any>('driveFiles.files');
|
const _DriveFile = db.get<any>('driveFiles.files');
|
||||||
|
const getDriveFileBucket = async (): Promise<mongo.GridFSBucket> => {
|
||||||
|
const db = await nativeDbConn();
|
||||||
|
const bucket = new mongo.GridFSBucket(db, {
|
||||||
|
bucketName: 'driveFiles'
|
||||||
|
});
|
||||||
|
return bucket;
|
||||||
|
};
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
await initDb();
|
await initDb();
|
||||||
|
@ -38,8 +66,8 @@ async function main() {
|
||||||
autoAcceptFollowed: true,
|
autoAcceptFollowed: true,
|
||||||
autoWatch: false,
|
autoWatch: false,
|
||||||
name: user.name,
|
name: user.name,
|
||||||
location: user.profile.location,
|
location: user.profile ? user.profile.location : null,
|
||||||
birthday: user.profile.birthday,
|
birthday: user.profile ? user.profile.birthday : null,
|
||||||
followersCount: user.followersCount,
|
followersCount: user.followersCount,
|
||||||
followingCount: user.followingCount,
|
followingCount: user.followingCount,
|
||||||
notesCount: user.notesCount,
|
notesCount: user.notesCount,
|
||||||
|
@ -59,17 +87,48 @@ async function main() {
|
||||||
const file = await _DriveFile.findOne({}, {
|
const file = await _DriveFile.findOne({}, {
|
||||||
skip: i
|
skip: i
|
||||||
});
|
});
|
||||||
|
const user = await _User.findOne({
|
||||||
|
_id: file.metadata.userId
|
||||||
|
});
|
||||||
|
if (file.metadata.storage && file.metadata.storage.key) { // when object storage
|
||||||
await DriveFiles.save({
|
await DriveFiles.save({
|
||||||
id: file._id.toHexString(),
|
id: file._id.toHexString(),
|
||||||
userId: file.userId.toHexString(),
|
userId: user._id.toHexString(),
|
||||||
|
userHost: user.host,
|
||||||
createdAt: file.uploadDate || new Date(),
|
createdAt: file.uploadDate || new Date(),
|
||||||
md5: file.md5,
|
md5: file.md5,
|
||||||
name: file.filename,
|
name: file.filename,
|
||||||
type: file.contentType,
|
type: file.contentType,
|
||||||
properties: file.metadata.properties,
|
properties: file.metadata.properties,
|
||||||
size: file.length,
|
size: file.length,
|
||||||
|
url: file.metadata.url,
|
||||||
|
uri: file.metadata.uri,
|
||||||
|
accessKey: file.metadata.storage.key
|
||||||
});
|
});
|
||||||
console.log(`USER (${i + 1}/${allDriveFilesCount}) ${file._id} DONE`);
|
} else {
|
||||||
|
const [temp, clean] = await createTemp();
|
||||||
|
const bucket = await getDriveFileBucket();
|
||||||
|
const readable = bucket.openDownloadStream(file._id);
|
||||||
|
fs.writeFileSync(temp, readable);
|
||||||
|
const key = uuid.v4();
|
||||||
|
const url = InternalStorage.saveFromPath(key, temp);
|
||||||
|
await DriveFiles.save({
|
||||||
|
id: file._id.toHexString(),
|
||||||
|
userId: user._id.toHexString(),
|
||||||
|
userHost: user.host,
|
||||||
|
createdAt: file.uploadDate || new Date(),
|
||||||
|
md5: file.md5,
|
||||||
|
name: file.filename,
|
||||||
|
type: file.contentType,
|
||||||
|
properties: file.metadata.properties,
|
||||||
|
size: file.length,
|
||||||
|
url: url,
|
||||||
|
uri: file.metadata.uri,
|
||||||
|
accessKey: key
|
||||||
|
});
|
||||||
|
clean();
|
||||||
|
}
|
||||||
|
console.log(`DRIVEFILE (${i + 1}/${allDriveFilesCount}) ${file._id} DONE`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue