Skip to content

Commit

Permalink
Merge branch 'main' into dependabot-npm_and_yarn-typescript-5.4.2
Browse files Browse the repository at this point in the history
  • Loading branch information
PixoDev authored Mar 12, 2024
2 parents a50f227 + 1ab7686 commit 7c80de4
Show file tree
Hide file tree
Showing 21 changed files with 843 additions and 119 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"posttest": "yarn lint",
"prepack": "yarn build && oclif manifest && oclif readme",
"prepare": "husky",
"test:unit": "nyc --reporter=lcov --reporter=text mocha \"test/**/*.test.ts\"",
"test:unit": "nyc --reporter=lcov --reporter=text mocha \"test/**/*.test.ts\" --exit",
"dev:webdav": "nodemon -e ts --exec 'ts-node src/webdav/index.ts'",
"version": "oclif readme && git add README.md"
},
Expand Down Expand Up @@ -43,6 +43,7 @@
"express-basic-auth": "^1.2.1",
"fast-xml-parser": "^4.3.5",
"openpgp": "^5.11.1",
"realm": "^12.6.2",
"superagent": "^8.1.2",
"winston": "^3.12.0"
},
Expand Down
2 changes: 1 addition & 1 deletion sonar-project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ sonar.projectKey=internxt_cli
sonar.organization=internxt
sonar.javascript.lcov.reportPaths=./coverage/lcov.info
sonar.sources=src/utils,src/services,src/webdav

sonar.exclusions=src/webdav/index.ts
13 changes: 12 additions & 1 deletion src/services/config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import { LoginCredentials } from '../types/command.types';
import { CryptoService } from './crypto.service';

export class ConfigService {
static readonly CREDENTIALS_FILE = path.join(os.homedir(), '.inxtcli');
static readonly INTERNXT_CLI_DATA_DIR = path.join(os.homedir(), '.internxt-cli');
static readonly CREDENTIALS_FILE = path.join(this.INTERNXT_CLI_DATA_DIR, '.inxtcli');
static readonly DRIVE_REALM_FILE = path.join(this.INTERNXT_CLI_DATA_DIR, 'internxt-cli-drive.realm');
public static readonly instance: ConfigService = new ConfigService();

/**
Expand All @@ -27,6 +29,7 @@ export class ConfigService {
* @async
**/
public saveUser = async (loginCredentials: LoginCredentials): Promise<void> => {
await this.ensureInternxtCliDataDirExists();
const credentialsString = JSON.stringify(loginCredentials);
const encryptedCredentials = CryptoService.instance.encryptText(credentialsString);
await fs.writeFile(ConfigService.CREDENTIALS_FILE, encryptedCredentials, 'utf8');
Expand Down Expand Up @@ -63,4 +66,12 @@ export class ConfigService {
return;
}
};

ensureInternxtCliDataDirExists = async () => {
try {
await fs.access(ConfigService.INTERNXT_CLI_DATA_DIR);
} catch {
await fs.mkdir(ConfigService.INTERNXT_CLI_DATA_DIR);
}
};
}
49 changes: 49 additions & 0 deletions src/services/realms/drive-files.realm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import Realm, { ObjectSchema } from 'realm';

export class DriveFileRealmSchema extends Realm.Object<DriveFileRealmSchema> {
id!: number;
name!: string;
type?: string;
uuid!: string;
fileId!: string;
folder_id!: number;
folder_uuid!: string;
bucket!: string;
relative_path!: string;
created_at!: Date;
updated_at!: Date;
size!: number;
status!: 'EXISTS' | 'REMOVED' | 'TRASHED';
static readonly schema: ObjectSchema = {
name: 'DriveFile',
properties: {
id: 'int',
name: 'string',
type: 'string?',
uuid: { type: 'string', indexed: true },
file_id: 'string',
folder_id: 'int',
folder_uuid: 'string',
bucket: 'string',
relative_path: { type: 'string', indexed: true },
created_at: 'date',
updated_at: 'date',
size: 'int',
status: 'string',
},
primaryKey: 'id',
};
}

export class DriveFilesRealm {
constructor(private realm: Realm) {}

async findByRelativePath(relativePath: string): Promise<DriveFileRealmSchema | null> {
const object = this.realm
.objects<DriveFileRealmSchema>('DriveFile')
.filtered('relative_path = $0', relativePath)
.find((file) => file.relative_path === relativePath);

return object ?? null;
}
}
68 changes: 68 additions & 0 deletions src/services/realms/drive-folders.realm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import Realm, { ObjectSchema } from 'realm';
import { DriveFolderItem } from '../../types/drive.types';

export class DriveFolderRealmSchema extends Realm.Object<DriveFolderRealmSchema> {
id!: number;
name!: string;
uuid!: string;
relative_path!: string;
parent_id?: number;
created_at!: Date;
updated_at!: Date;
status!: 'EXISTS' | 'REMOVED' | 'TRASHED';
static readonly schema: ObjectSchema = {
name: 'DriveFolder',
properties: {
id: 'int',
name: 'string',
uuid: { type: 'string', indexed: true },
relative_path: { type: 'string', indexed: true },
parent_id: { type: 'int', indexed: true, default: -1 },
created_at: 'date',
updated_at: 'date',
status: 'string',
},
primaryKey: 'id',
};
}

export class DriveFoldersRealm {
constructor(private realm: Realm) {}

async findByRelativePath(relativePath: string): Promise<DriveFolderRealmSchema | null> {
const object = this.realm
.objects<DriveFolderRealmSchema>('DriveFolder')
.filtered('relative_path = $0', relativePath)
.find((folder) => folder.relative_path === relativePath);

return object ?? null;
}

async findByParentId(parentId: number | null): Promise<DriveFolderRealmSchema | null> {
// -1 is root as we cannot index null fields
const parentFolder = this.realm.objectForPrimaryKey<DriveFolderRealmSchema>('DriveFolder', parentId ?? -1);

return parentFolder;
}

async createOrReplace(driveFolder: DriveFolderItem, relativePath: string) {
const existingObject = this.realm.objectForPrimaryKey<DriveFolderRealmSchema>('DriveFolder', driveFolder.id);

this.realm.write(() => {
if (existingObject) {
this.realm.delete(existingObject);
}

this.realm.create<DriveFolderRealmSchema>('DriveFolder', {
id: driveFolder.id,
name: driveFolder.name,
uuid: driveFolder.uuid,
parent_id: driveFolder.parentId ?? -1,
created_at: driveFolder.createdAt,
updated_at: driveFolder.updatedAt,
status: 'EXISTS',
relative_path: relativePath,
});
});
}
}
41 changes: 41 additions & 0 deletions src/services/realms/drive-realm-manager.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import path from 'path';
import { DriveFolderItem } from '../../types/drive.types';
import { DriveFileRealmSchema, DriveFilesRealm } from './drive-files.realm';
import { DriveFolderRealmSchema, DriveFoldersRealm } from './drive-folders.realm';

export class DriveRealmManager {
constructor(
private driveFilesRealm: DriveFilesRealm,
private driveFoldersRealm: DriveFoldersRealm,
) {}

async findByRelativePath(relativePath: string): Promise<DriveFolderRealmSchema | DriveFileRealmSchema | null> {
const driveFile = await this.driveFilesRealm.findByRelativePath(relativePath);

if (driveFile) return driveFile;

const driveFolder = await this.driveFoldersRealm.findByRelativePath(relativePath);

if (driveFolder) return driveFolder;

return null;
}

async createFolder(driveFolder: DriveFolderItem) {
const relativePath = await this.buildRelativePathForFolder(driveFolder.name, driveFolder.parentId ?? null);

return this.driveFoldersRealm.createOrReplace(driveFolder, relativePath);
}

async buildRelativePathForFolder(folderName: string, parentId: number | null): Promise<string> {
const parentFolder = await this.driveFoldersRealm.findByParentId(parentId);

if (!parentFolder) {
return path.join('/', folderName, '/');
}

const parentPath = await this.buildRelativePathForFolder(parentFolder.name, parentFolder.parent_id ?? null);

return path.join(parentPath, folderName, '/');
}
}
2 changes: 1 addition & 1 deletion src/types/drive.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export type DriveFileItem = Pick<DriveFileData, 'name' | 'bucket' | 'fileId' | '
updatedAt: Date;
};

export type DriveFolderItem = Pick<DriveFolderData, 'name' | 'bucket' | 'id'> & {
export type DriveFolderItem = Pick<DriveFolderData, 'name' | 'bucket' | 'id' | 'parentId'> & {
encryptedName: string;
uuid: string;
createdAt: Date;
Expand Down
6 changes: 3 additions & 3 deletions src/utils/xml.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ export class XMLUtils {
return parser.parse(xml);
}

static toXML(object: Record<string, any>, options: XmlBuilderOptions = { format: true }) {
static toXML(object: object, options: XmlBuilderOptions = { format: true }) {
const builder = new XMLBuilder(options);
return builder.build(object);
}

static toWebDavXML(object: Record<string, any>, options: XmlBuilderOptions) {
static toWebDavXML(object: object, options: XmlBuilderOptions) {
const xmlContent = this.toXML(object, options);
return `<?xml version="1.0" encoding="utf-8" ?><D:multistatus xmlns:D="DAV:">${xmlContent}</D:multistatus>`;
return `<?xml version="1.0" encoding="utf-8" ?><multistatus xmlns:D="DAV:">${xmlContent}</multistatus>`;
}
}
Loading

0 comments on commit 7c80de4

Please sign in to comment.