import fs from 'node:fs' import path from 'node:path' import sanitize from 'sanitize-filename' import { getUserHome } from '../utils' import { isPathTraversal, isUnsafeFilePath, isValidUUID } from '../validator' import { FileInfo, IStorageProvider, StorageResult, StorageSizeResult } from './IStorageProvider' export abstract class BaseStorageProvider implements IStorageProvider { protected storagePath: string constructor() { this.storagePath = this.getStoragePath() } abstract getStorageType(): string abstract getConfig(): any abstract addBase64FilesToStorage(fileBase64: string, chatflowid: string, fileNames: string[], orgId: string): Promise abstract addArrayFilesToStorage( mime: string, bf: Buffer, fileName: string, fileNames: string[], ...paths: string[] ): Promise abstract addSingleFileToStorage(mime: string, bf: Buffer, fileName: string, ...paths: string[]): Promise abstract getFileFromUpload(filePath: string): Promise abstract getFileFromStorage(file: string, ...paths: string[]): Promise abstract getFilesListFromStorage(...paths: string[]): Promise abstract streamStorageFile( chatflowId: string, chatId: string, fileName: string, orgId: string ): Promise abstract removeFilesFromStorage(...paths: string[]): Promise abstract removeSpecificFileFromUpload(filePath: string): Promise abstract removeSpecificFileFromStorage(...paths: string[]): Promise abstract removeFolderFromStorage(...paths: string[]): Promise abstract getStorageSize(orgId: string): Promise abstract getMulterStorage(): any abstract getLoggerTransports(logType: 'server' | 'error' | 'requests' | 'audit', config?: any): any[] /** * Shared utility for sanitizing filenames to prevent path traversal and other issues */ protected sanitizeFilename(filename: string): string { if (!filename || isUnsafeFilePath(filename)) { throw new Error('Invalid or unsafe fileName detected') } const sanitizedFilename = sanitize(filename) // Remove leading dots to prevent hidden files or relative path jumps const cleaned = sanitizedFilename.replace(/^\.+/, '') if (!cleaned || cleaned.includes('/') || cleaned.includes('\\')) { throw new Error('Invalid filename after sanitization') } return cleaned } /** * Shared utility for getting the base storage path */ protected getStoragePath(): string { const storagePath = process.env.BLOB_STORAGE_PATH ? path.join(process.env.BLOB_STORAGE_PATH) : path.join(getUserHome(), '.flowise', 'storage') if (!fs.existsSync(storagePath)) { fs.mkdirSync(storagePath, { recursive: true }) } return storagePath } /** * Shared utility for validating chatflowId format (UUID) */ protected validateChatflowId(chatflowId: string): void { if (!chatflowId || !isValidUUID(chatflowId)) { throw new Error('Invalid chatflowId format - must be a valid UUID') } } /** * Shared utility for checking path traversal attempts */ protected validatePathSecurity(...paths: string[]): void { for (const p of paths) { if (p && isPathTraversal(p)) { throw new Error('Invalid path characters detected') } } } /** * Shared utility for building a storage path from components */ protected buildPath(...paths: string[]): string { const sanitizedPaths = paths.filter((p) => p && typeof p === 'string').map((p) => this.sanitizeFilename(p)) return path.join(this.storagePath, ...sanitizedPaths) } }