The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
refactor(file-storage): make copy private and drop dead copy/folder code (#26593)
Etienne50cd77b48667
diff --git a/packages/twenty-server/src/engine/core-modules/file-storage/drivers/__tests__/validated-storage.driver.spec.ts b/packages/twenty-server/src/engine/core-modules/file-storage/drivers/__tests__/validated-storage.driver.spec.tsindex 6be1359b192..214d8333566 100644--- a/packages/twenty-server/src/engine/core-modules/file-storage/drivers/__tests__/validated-storage.driver.spec.ts+++ b/packages/twenty-server/src/engine/core-modules/file-storage/drivers/__tests__/validated-storage.driver.spec.ts@@ -12,8 +12,6 @@ const createMockDriver = (): jest.Mocked<StorageDriver> => ({writeFileStream: jest.fn().mockResolvedValue(undefined),getFileMetadata: jest.fn().mockResolvedValue(null),getPresignedUploadUrl: jest.fn().mockResolvedValue(null),- downloadFolder: jest.fn().mockResolvedValue(undefined),- uploadFolder: jest.fn().mockResolvedValue(undefined),downloadFile: jest.fn().mockResolvedValue(undefined),delete: jest.fn().mockResolvedValue(undefined),move: jest.fn().mockResolvedValue(undefined),@@ -53,30 +51,6 @@ describe('ValidatedStorageDriver', () => {expect(mockDelegate.writeFile).toHaveBeenCalledWith(params);});- it('should delegate downloadFolder', async () => {- await driver.downloadFolder({- onStoragePath: 'folder',- localPath: '/tmp/local',- });-- expect(mockDelegate.downloadFolder).toHaveBeenCalledWith({- onStoragePath: 'folder',- localPath: '/tmp/local',- });- });-- it('should delegate uploadFolder', async () => {- await driver.uploadFolder({- localPath: '/tmp/local',- onStoragePath: 'folder',- });-- expect(mockDelegate.uploadFolder).toHaveBeenCalledWith({- localPath: '/tmp/local',- onStoragePath: 'folder',- });- });-it('should delegate delete', async () => {await driver.delete({ folderPath: 'folder', filename: 'file.txt' });@@ -324,19 +298,6 @@ describe('ValidatedStorageDriver', () => {expect(mockDelegate.copy).not.toHaveBeenCalled();});- it('should reject downloadFolder with traversal', async () => {- await expect(- driver.downloadFolder({- onStoragePath: '../../../etc',- localPath: '/tmp/local',- }),- ).rejects.toMatchObject({- code: FileStorageExceptionCode.ACCESS_DENIED,- });-- expect(mockDelegate.downloadFolder).not.toHaveBeenCalled();- });-it('should reject getPresignedUrl with traversal', async () => {await expect(driver.getPresignedUrl({ filePath: '../../../etc/passwd' }),@@ -349,24 +310,6 @@ describe('ValidatedStorageDriver', () => {});describe('does NOT validate localPath parameters', () => {- it('should allow absolute localPath in downloadFolder', async () => {- await driver.downloadFolder({- onStoragePath: 'folder',- localPath: '/tmp/any-path',- });-- expect(mockDelegate.downloadFolder).toHaveBeenCalled();- });-- it('should allow absolute localPath in uploadFolder', async () => {- await driver.uploadFolder({- localPath: '/tmp/any-path',- onStoragePath: 'folder',- });-- expect(mockDelegate.uploadFolder).toHaveBeenCalled();- });-it('should allow absolute localPath in downloadFile', async () => {await driver.downloadFile({onStoragePath: 'folder/file.txt',diff --git a/packages/twenty-server/src/engine/core-modules/file-storage/drivers/interfaces/storage-driver.interface.ts b/packages/twenty-server/src/engine/core-modules/file-storage/drivers/interfaces/storage-driver.interface.tsindex becc26acad2..c8a84ad16fc 100644--- a/packages/twenty-server/src/engine/core-modules/file-storage/drivers/interfaces/storage-driver.interface.ts+++ b/packages/twenty-server/src/engine/core-modules/file-storage/drivers/interfaces/storage-driver.interface.ts@@ -28,15 +28,6 @@ export interface StorageDriver {filePath: string;}): Promise<FileStorageMetadata | null>;- downloadFolder(params: {- onStoragePath: string;- localPath: string;- }): Promise<void>;- uploadFolder(params: {- localPath: string;- onStoragePath: string;- }): Promise<void>;-downloadFile(params: {onStoragePath: string;localPath: string;diff --git a/packages/twenty-server/src/engine/core-modules/file-storage/drivers/local.driver.ts b/packages/twenty-server/src/engine/core-modules/file-storage/drivers/local.driver.tsindex 0341842b68e..8cf1955741e 100644--- a/packages/twenty-server/src/engine/core-modules/file-storage/drivers/local.driver.ts+++ b/packages/twenty-server/src/engine/core-modules/file-storage/drivers/local.driver.ts@@ -292,94 +292,6 @@ export class LocalDriver implements StorageDriver {await fs.writeFile(params.localPath, content);}- async downloadFolder(params: {- onStoragePath: string;- localPath: string;- }): Promise<void> {- const resolvedPath = path.resolve(- this.options.storagePath,- params.onStoragePath,- );- let rootFolderPath: string;-- try {- rootFolderPath = realpathSync(resolvedPath);- } catch {- throw new FileStorageException(- 'File not found',- FileStorageExceptionCode.FILE_NOT_FOUND,- );- }-- this.assertRealPathIsWithinStorage(rootFolderPath);-- await this.createFolder(params.localPath);-- await this.downloadFolderFromRealPath({- rootFolderPath,- localPath: params.localPath,- });- }-- private async downloadFolderFromRealPath(params: {- rootFolderPath: string;- localPath: string;- }): Promise<void> {- await this.createFolder(params.localPath);-- const resources = await fs.readdir(params.rootFolderPath);-- for (const resource of resources) {- const resourcePath = path.join(params.rootFolderPath, resource);- const stats = await fs.lstat(resourcePath);-- if (stats.isSymbolicLink()) {- throw new FileStorageException(- 'Access denied',- FileStorageExceptionCode.ACCESS_DENIED,- );- }-- if (stats.isFile()) {- const content = await fs.readFile(resourcePath);-- await fs.writeFile(path.join(params.localPath, resource), content);- } else {- await this.downloadFolderFromRealPath({- rootFolderPath: resourcePath,- localPath: path.join(params.localPath, resource),- });- }- }- }-- async uploadFolder(params: {- localPath: string;- onStoragePath: string;- }): Promise<void> {- const resources = await fs.readdir(params.localPath);-- for (const resource of resources) {- const resourcePath = path.join(params.localPath, resource);- const stats = await fs.stat(resourcePath);-- if (stats.isFile()) {- const content = await fs.readFile(resourcePath);-- await this.writeFile({- filePath: path.join(params.onStoragePath, resource),- sourceFile: content,- mimeType: undefined,- });- } else {- await this.uploadFolder({- localPath: resourcePath,- onStoragePath: path.join(params.onStoragePath, resource),- });- }- }- }-async delete(params: {folderPath: string;filename?: string;diff --git a/packages/twenty-server/src/engine/core-modules/file-storage/drivers/s3.driver.ts b/packages/twenty-server/src/engine/core-modules/file-storage/drivers/s3.driver.tsindex c5821251bbb..81239322c5c 100644--- a/packages/twenty-server/src/engine/core-modules/file-storage/drivers/s3.driver.ts+++ b/packages/twenty-server/src/engine/core-modules/file-storage/drivers/s3.driver.ts@@ -1,8 +1,7 @@import { Logger } from '@nestjs/common';import fs from 'fs';-import { readdir, readFile } from 'fs/promises';-import { dirname, join } from 'path';+import { dirname } from 'path';import { Readable } from 'stream';import { pipeline } from 'stream/promises';@@ -296,73 +295,6 @@ export class S3Driver implements StorageDriver {await pipeline(fileStream, fs.createWriteStream(params.localPath));}- async downloadFolder(params: {- onStoragePath: string;- localPath: string;- }): Promise<void> {- const listedObjects = await this.fetchS3FolderContents(- params.onStoragePath,- );-- if (!listedObjects.Contents || listedObjects.Contents.length === 0) {- return;- }-- for (const object of listedObjects.Contents) {- const folderAndFilePaths = this.extractFolderAndFilePaths(object.Key);-- if (!isDefined(folderAndFilePaths)) {- continue;- }-- const { fromFolderPath, filename } = folderAndFilePaths;-- const relativePath = fromFolderPath- .replace(params.onStoragePath + '/', '')- .replace(params.onStoragePath, '');-- const localFolderPath = relativePath- ? join(params.localPath, relativePath)- : params.localPath;-- await this.createFolder(localFolderPath);-- const fileStream = await this.readFile({- filePath: `${fromFolderPath}/${filename}`,- });-- const toPath = join(localFolderPath, filename);-- await pipeline(fileStream, fs.createWriteStream(toPath));- }- }-- async uploadFolder(params: {- localPath: string;- onStoragePath: string;- }): Promise<void> {- const entries = await readdir(params.localPath, { withFileTypes: true });-- for (const entry of entries) {- const localEntryPath = join(params.localPath, entry.name);-- if (entry.isDirectory()) {- await this.uploadFolder({- localPath: localEntryPath,- onStoragePath: join(params.onStoragePath, entry.name),- });- } else {- const fileContent = await readFile(localEntryPath);-- await this.writeFile({- filePath: `${params.onStoragePath}/${entry.name}`,- sourceFile: fileContent,- mimeType: undefined,- });- }- }- }-async delete(params: {folderPath: string;filename?: string;diff --git a/packages/twenty-server/src/engine/core-modules/file-storage/drivers/validated-storage.driver.ts b/packages/twenty-server/src/engine/core-modules/file-storage/drivers/validated-storage.driver.tsindex bfb721d7f53..845ae04a4f6 100644--- a/packages/twenty-server/src/engine/core-modules/file-storage/drivers/validated-storage.driver.ts+++ b/packages/twenty-server/src/engine/core-modules/file-storage/drivers/validated-storage.driver.ts@@ -55,24 +55,6 @@ export class ValidatedStorageDriver implements StorageDriver {return this.delegate.getFileMetadata(params);}- async downloadFolder(params: {- onStoragePath: string;- localPath: string;- }): Promise<void> {- assertStoragePathIsSafe(params.onStoragePath);-- return this.delegate.downloadFolder(params);- }-- async uploadFolder(params: {- localPath: string;- onStoragePath: string;- }): Promise<void> {- assertStoragePathIsSafe(params.onStoragePath);-- return this.delegate.uploadFolder(params);- }-async downloadFile(params: {onStoragePath: string;localPath: string;diff --git a/packages/twenty-server/src/engine/core-modules/file-storage/services/file-storage.service.ts b/packages/twenty-server/src/engine/core-modules/file-storage/services/file-storage.service.tsindex 53001fcc0ae..ed535ef5c5c 100644--- a/packages/twenty-server/src/engine/core-modules/file-storage/services/file-storage.service.ts+++ b/packages/twenty-server/src/engine/core-modules/file-storage/services/file-storage.service.ts@@ -885,15 +885,6 @@ export class FileStorageService {await driver.delete({ folderPath: workspaceId });}- copyLegacy(params: {- from: { folderPath: string; filename?: string };- to: { folderPath: string; filename?: string };- }): Promise<void> {- const driver = this.fileStorageDriverFactory.getCurrentDriver();-- return driver.copy(params);- }-async copyFile({from,to,diff --git a/packages/twenty-server/src/engine/core-modules/file-storage/services/server-file-storage.service.ts b/packages/twenty-server/src/engine/core-modules/file-storage/services/server-file-storage.service.tsindex 15e07f3a6c0..5b09ba11d9e 100644--- a/packages/twenty-server/src/engine/core-modules/file-storage/services/server-file-storage.service.ts+++ b/packages/twenty-server/src/engine/core-modules/file-storage/services/server-file-storage.service.ts@@ -172,43 +172,6 @@ export class ServerFileStorageService {});}- checkServerFileExists({- fileFolder,- applicationRegistrationId,- resourcePath,- }: ServerResourceIdentifier): Promise<boolean> {- const driver = this.fileStorageDriverFactory.getCurrentDriver();-- const { onStorageFilePath } =- this.validateAndBuildServerFileStoragePathOrThrow({- fileFolder,- applicationRegistrationId,- resourcePath,- });-- return driver.checkFileExists({ filePath: onStorageFilePath });- }-- async deleteServerFile({- fileFolder,- applicationRegistrationId,- resourcePath,- }: ServerResourceIdentifier): Promise<void> {- const { onStorageFilePath, filePath } =- this.validateAndBuildServerFileStoragePathOrThrow({- fileFolder,- applicationRegistrationId,- resourcePath,- });-- await this.deleteServerFileBytesBestEffort(onStorageFilePath);-- await this.serverFileRepository.delete({- path: filePath,- workspaceId: IsNull(),- });- }-async deleteByApplicationRegistrationId(applicationRegistrationId: string,): Promise<void> {