🌟 Data migration from nextcloud (#302)

- Files upload to Twake Drive
- New logger
This commit is contained in:
Anton Shepilov
2024-01-02 17:09:43 +03:00
committed by GitHub
parent 75dffb1c7a
commit 6f7f714fe8
14 changed files with 261 additions and 57 deletions
@@ -1,8 +1,11 @@
import { exec } from 'child_process';
// @ts-ignore
import fs from 'fs';
import { LdapUser } from './shell_ldap_user.js';
import { LdapUser } from './shell_ldap_user';
import { User } from './ldap_user.js';
import { TwakeDriveClient } from './twake_client.js';
import { TwakeDriveClient, TwakeDriveUser } from './twake_client';
import path from 'path';
import { logger } from "./logger"
export type NextcloudMigrationConfiguration = {
ldap: {
@@ -26,7 +29,7 @@ export class NextcloudMigration {
private ldap: LdapUser;
private driveClient: TwakeDriveClient;
driveClient: TwakeDriveClient;
constructor(config: NextcloudMigrationConfiguration) {
this.config = config;
@@ -37,11 +40,14 @@ export class NextcloudMigration {
async migrate(username: string, password: string) {
const dir = this.createTmpDir(username);
try {
// await this.download(username, password, dir);
const user = await this.getLDAPUser(username);
//create user if needed Twake Drive
await this.driveClient.createUser(user);
const driveUser = await this.driveClient.createUser(user);
console.log(`Drive user ${driveUser.id} created`);
//download all files from nextcloud to tmp dir
await this.download(username, password, dir);
//upload files to the Twake Drive
await this.upload(driveUser, dir);
} catch (e) {
console.error('Error downloading files from next cloud', e);
throw e;
@@ -99,4 +105,46 @@ export class NextcloudMigration {
console.log(`Directory ${dir} deleted`);
}
async upload(user: TwakeDriveUser, sourceDirPath: string, parentDirId = "user_" + user.id) {
const dirsToUpload: Map<string, string> = new Map<string, string>();
const filesToUpload: string[] = [];
const parent = await this.driveClient.getDocument(parentDirId);
const exists = (filename: string) => {
return parent.children.filter(i => i.name.startsWith(path.parse(filename).name)).length > 0;
}
logger.debug(`Reading content of the directory ${sourceDirPath} ...`)
fs.readdirSync(sourceDirPath).forEach(function (name) {
const filePath = path.join(sourceDirPath, name);
const stat = fs.statSync(filePath);
if (exists(name)) {
logger.info(`File ${name} already exists`);
} else {
if (stat.isFile()) {
logger.info(`Add file for future upload ${filePath}`);
filesToUpload.push(filePath)
} else if (stat.isDirectory()) {
logger.info(`Add directory for the future upload ${filePath}`);
dirsToUpload.set(name, filePath);
}
}
});
//upload all files
logger.debug(`UPLOAD FILES FOR ${sourceDirPath}`)
for (const file of filesToUpload) {
logger.debug(`Upload file ${file}`)
await this.driveClient.createFile(file, parentDirId);
}
logger.debug(`UPLOAD DIRS FOR ${sourceDirPath}`)
for (const [name, path] of dirsToUpload) {
logger.info(`Create directory ${name}`);
const dir = await this.driveClient.createDirectory(name, parentDirId)
await this.upload(user, path, dir.id);
}
}
}