Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 9 additions & 32 deletions src/commands/upload-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { DriveFileService } from '../services/drive/drive-file.service';
import { CryptoService } from '../services/crypto.service';
import { DownloadService } from '../services/network/download.service';
import { ErrorUtils } from '../utils/errors.utils';
import { NotValidDirectoryError, NotValidFolderUuidError } from '../types/command.types';
import { NotValidDirectoryError } from '../types/command.types';
import { ValidationService } from '../services/validation.service';
import { EncryptionVersion } from '@internxt/sdk/dist/drive/storage/types';
import { ThumbnailService } from '../services/thumbnail.service';
Expand Down Expand Up @@ -58,11 +58,14 @@ export default class UploadFile extends Command {
const fileInfo = path.parse(filePath);
const fileType = fileInfo.ext.replaceAll('.', '');

let destinationFolderUuid = await this.getDestinationFolderUuid(flags['destination'], nonInteractive);
if (destinationFolderUuid.trim().length === 0) {
// destinationFolderUuid is empty from flags&prompt, which means we should use RootFolderUuid
destinationFolderUuid = user.rootFolderId;
}
// If destinationFolderUuid is empty from flags&prompt, means we should use RootFolderUuid
const destinationFolderUuid =
(await CLIUtils.getDestinationFolderUuid({
destinationFolderUuidFlag: flags['destination'],
destinationFlagName: UploadFile.flags['destination'].name,
nonInteractive,
reporter: this.log.bind(this),
})) ?? user.rootFolderId;

// 1. Prepare the network
CLIUtils.doing('Preparing Network', flags['json']);
Expand Down Expand Up @@ -189,32 +192,6 @@ export default class UploadFile extends Command {
this.exit(1);
};

private getDestinationFolderUuid = async (
destinationFolderUuidFlag: string | undefined,
nonInteractive: boolean,
): Promise<string> => {
const destinationFolderUuid = await CLIUtils.getValueFromFlag(
{
value: destinationFolderUuidFlag,
name: UploadFile.flags['destination'].name,
},
{
nonInteractive,
prompt: {
message: 'What is the destination folder id? (leave empty for the root folder)',
options: { type: 'input' },
},
},
{
validate: ValidationService.instance.validateUUIDv4,
error: new NotValidFolderUuidError(),
canBeEmpty: true,
},
this.log.bind(this),
);
return destinationFolderUuid;
};

private getFilePath = async (fileFlag: string | undefined, nonInteractive: boolean): Promise<string> => {
const filePath = await CLIUtils.getValueFromFlag(
{
Expand Down
67 changes: 67 additions & 0 deletions src/services/local-filesystem/local-filesystem.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { promises } from 'node:fs';
import { basename, dirname, join, relative, parse } from 'node:path';
import { FileSystemNode, ScanResult } from './local-filesystem.types';
import { logger } from '../../utils/logger.utils';

export class LocalFilesystemService {
static readonly instance = new LocalFilesystemService();

async scanLocalDirectory(path: string): Promise<ScanResult> {
const folders: FileSystemNode[] = [];
const files: FileSystemNode[] = [];

const parentPath = dirname(path);
const totalBytes = await this.scanRecursive(path, parentPath, folders, files);
return {
folders,
files,
totalItems: folders.length + files.length,
totalBytes,
};
}
async scanRecursive(
currentPath: string,
parentPath: string,
folders: FileSystemNode[],
files: FileSystemNode[],
): Promise<number> {
try {
const stats = await promises.stat(currentPath);
const relativePath = relative(parentPath, currentPath);

if (stats.isFile() && stats.size > 0) {
const fileInfo = parse(currentPath);
files.push({
type: 'file',
name: fileInfo.name,
absolutePath: currentPath,
relativePath,
size: stats.size,
});
return stats.size;
}

if (stats.isDirectory()) {
folders.push({
type: 'folder',
name: basename(currentPath),
absolutePath: currentPath,
relativePath,
size: 0,
});
const entries = await promises.readdir(currentPath, { withFileTypes: true });
const validEntries = entries.filter((e) => !e.isSymbolicLink());
const bytesArray = await Promise.all(
validEntries.map((e) => this.scanRecursive(join(currentPath, e.name), parentPath, folders, files)),
);

return bytesArray.reduce((sum, bytes) => sum + bytes, 0);
}

return 0;
} catch (error: unknown) {
logger.warn(`Error scanning path ${currentPath}: ${(error as Error).message} - skipping...`);
return 0;
}
}
}
14 changes: 14 additions & 0 deletions src/services/local-filesystem/local-filesystem.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export interface FileSystemNode {
type: 'file' | 'folder';
name: string;
size: number;
absolutePath: string;
relativePath: string;
}

export interface ScanResult {
folders: FileSystemNode[];
files: FileSystemNode[];
totalItems: number;
totalBytes: number;
}
40 changes: 39 additions & 1 deletion src/utils/cli.utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { ux, Flags } from '@oclif/core';
import cliProgress from 'cli-progress';
import Table, { Header } from 'tty-table';
import { PromptOptions } from '../types/command.types';
import { NotValidFolderUuidError, PromptOptions } from '../types/command.types';
import { InquirerUtils } from './inquirer.utils';
import { ErrorUtils } from './errors.utils';
import { ValidationService } from '../services/validation.service';

export class CLIUtils {
static readonly clearPreviousLine = (jsonFlag?: boolean) => {
Expand Down Expand Up @@ -125,6 +126,43 @@ export class CLIUtils {
}
};

static readonly getDestinationFolderUuid = async ({
destinationFolderUuidFlag,
destinationFlagName,
nonInteractive,
reporter,
}: {
destinationFolderUuidFlag: string | undefined;
destinationFlagName: string;
nonInteractive: boolean;
reporter: (message: string) => void;
}): Promise<string | undefined> => {
const destinationFolderUuid = await this.getValueFromFlag(
{
value: destinationFolderUuidFlag,
name: destinationFlagName,
},
{
nonInteractive,
prompt: {
message: 'What is the destination folder id? (leave empty for the root folder)',
options: { type: 'input' },
},
},
{
validate: ValidationService.instance.validateUUIDv4,
error: new NotValidFolderUuidError(),
canBeEmpty: true,
},
reporter,
);
if (destinationFolderUuid.trim().length === 0) {
return undefined;
} else {
return destinationFolderUuid;
}
};

private static readonly promptWithAttempts = async (
prompt: { message: string; options: PromptOptions },
maxAttempts: number,
Expand Down
6 changes: 6 additions & 0 deletions src/utils/errors.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ export function isError(error: unknown): error is Error {
return types.isNativeError(error);
}

export function isAlreadyExistsError(error: unknown): error is Error {
return (
(isError(error) && error.message.includes('already exists')) ||
(typeof error === 'object' && error !== null && 'status' in error && error.status === 409)
);
}
export class ErrorUtils {
static report(error: unknown, props: Record<string, unknown> = {}) {
if (isError(error)) {
Expand Down
168 changes: 168 additions & 0 deletions test/services/local-filesystem/local-filesystem.service.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
import { beforeEach, describe, expect, it, vi, MockedFunction } from 'vitest';
import { LocalFilesystemService } from '../../../src/services/local-filesystem/local-filesystem.service';
import { Dirent, promises, Stats } from 'fs';
import { logger } from '../../../src/utils/logger.utils';
import { FileSystemNode } from '../../../src/services/local-filesystem/local-filesystem.types';

vi.mock('fs', () => ({
promises: {
stat: vi.fn(),
readdir: vi.fn(),
},
}));

vi.mock('../../../src/utils/logger.utils', () => ({
logger: {
warn: vi.fn(),
error: vi.fn(),
info: vi.fn(),
},
}));

describe('Local Filesystem Service', () => {
let service: LocalFilesystemService;
const mockStat = vi.mocked(promises.stat);
const mockReaddir = vi.mocked(promises.readdir) as unknown as MockedFunction<() => Promise<Dirent<string>[]>>;

const createMockStats = (isFile: boolean, size: number): Stats =>
({
isFile: () => isFile,
isDirectory: () => !isFile,
size,
}) as Stats;

const createMockDirent = (name: string, isSymlink = false) =>
({
name,
isSymbolicLink: () => isSymlink,
isFile: () => false,
isDirectory: () => false,
}) as unknown as Dirent<string>;

beforeEach(() => {
service = LocalFilesystemService.instance;
vi.clearAllMocks();
mockReaddir.mockResolvedValue([]);
});

describe('scanRecursive', () => {
it('should handle a single file', async () => {
mockStat.mockResolvedValue(createMockStats(true, 100));

const folders: FileSystemNode[] = [];
const files: FileSystemNode[] = [];
const bytes = await service.scanRecursive('/path/file.txt', '/path', folders, files);

expect(bytes).toBe(100);
expect(files).toHaveLength(1);
expect(folders).toHaveLength(0);
});

it('should handle an empty directory', async () => {
mockStat.mockResolvedValue(createMockStats(false, 0));
mockReaddir.mockResolvedValue([]);

const folders: FileSystemNode[] = [];
const files: FileSystemNode[] = [];
const bytes = await service.scanRecursive('/path/folder', '/path', folders, files);

expect(bytes).toBe(0);
expect(folders).toHaveLength(1);
expect(folders[0]).toMatchObject({
type: 'folder',
name: 'folder',
});
expect(files).toHaveLength(0);
});

it('should handle a directory with files', async () => {
mockStat
.mockResolvedValueOnce(createMockStats(false, 0))
.mockResolvedValueOnce(createMockStats(true, 50))
.mockResolvedValueOnce(createMockStats(true, 75));

mockReaddir.mockResolvedValueOnce([createMockDirent('file1.txt', false), createMockDirent('file2.txt', false)]);

const folders: FileSystemNode[] = [];
const files: FileSystemNode[] = [];
const bytes = await service.scanRecursive('/path/folder', '/path', folders, files);

expect(bytes).toBe(125);
expect(folders).toHaveLength(1);
expect(files).toHaveLength(2);
});

it('should skip symbolic links', async () => {
mockStat.mockResolvedValueOnce(createMockStats(false, 0)).mockResolvedValueOnce(createMockStats(true, 100));
mockReaddir.mockResolvedValueOnce([createMockDirent('symlink', true), createMockDirent('file.txt', false)]);

const folders: FileSystemNode[] = [];
const files: FileSystemNode[] = [];
await service.scanRecursive('/path/folder', '/path', folders, files);

expect(mockStat).toHaveBeenCalledTimes(2);
expect(files).toHaveLength(1);
});

it('should handle errors gracefully', async () => {
mockStat.mockRejectedValue(new Error('Permission denied'));

const folders: FileSystemNode[] = [];
const files: FileSystemNode[] = [];
const bytes = await service.scanRecursive('/path/forbidden', '/path', folders, files);

expect(bytes).toBe(0);
expect(logger.warn).toHaveBeenCalledWith(expect.stringContaining('Permission denied'));
});

it('should handle nested directories', async () => {
mockStat
.mockResolvedValueOnce(createMockStats(false, 0))
.mockResolvedValueOnce(createMockStats(false, 0))
.mockResolvedValueOnce(createMockStats(true, 200));

const subfolder = [createMockDirent('subfolder', false)];
const file = [createMockDirent('file.txt', false)];

mockReaddir.mockResolvedValueOnce(subfolder).mockResolvedValueOnce(file);

const folders: FileSystemNode[] = [];
const files: FileSystemNode[] = [];
const bytes = await service.scanRecursive('/path/folder', '/path', folders, files);

expect(bytes).toBe(200);
expect(folders).toHaveLength(2);
expect(files).toHaveLength(1);
});
it('should properly skip empty files', async () => {
mockStat
.mockResolvedValueOnce(createMockStats(false, 0))
.mockResolvedValueOnce(createMockStats(true, 0))
.mockResolvedValueOnce(createMockStats(true, 200));
const folders: FileSystemNode[] = [];
const files: FileSystemNode[] = [];
mockReaddir.mockResolvedValueOnce([createMockDirent('file1.txt', false), createMockDirent('file2.txt', false)]);
const bytes = await service.scanRecursive('/path/folder', '/path', folders, files);

expect(bytes).toBe(200);
expect(folders).toHaveLength(1);
expect(files).toHaveLength(1);
});
});
describe('scanLocalDirectory', () => {
it('should scan a directory and return complete results', async () => {
mockStat.mockResolvedValueOnce(createMockStats(false, 0)).mockResolvedValueOnce(createMockStats(true, 100));

mockReaddir.mockResolvedValueOnce([createMockDirent('file.txt', false)]);

const result = await service.scanLocalDirectory('/test/folder');

expect(result).toMatchObject({
totalItems: 2,
totalBytes: 100,
});
expect(result.folders).toHaveLength(1);
expect(result.files).toHaveLength(1);
});
});
});
Loading
Loading