2023-09-25 09:04:52 -07:00
|
|
|
/**
|
|
|
|
* @tech_debt The `workflowId` arguments on write are for compatibility with the
|
|
|
|
* `BinaryData.Manager` interface. Unused in filesystem mode until we refactor
|
|
|
|
* how we store binary data files in the `/binaryData` dir.
|
|
|
|
*/
|
|
|
|
|
2023-09-22 08:22:12 -07:00
|
|
|
import { createReadStream } from 'fs';
|
|
|
|
import fs from 'fs/promises';
|
|
|
|
import path from 'path';
|
|
|
|
import { v4 as uuid } from 'uuid';
|
|
|
|
import { jsonParse } from 'n8n-workflow';
|
2023-09-25 03:30:28 -07:00
|
|
|
import { rename } from 'node:fs/promises';
|
2023-09-22 08:22:12 -07:00
|
|
|
|
|
|
|
import { FileNotFoundError } from '../errors';
|
2023-09-25 01:07:06 -07:00
|
|
|
import { ensureDirExists } from './utils';
|
2023-09-22 08:22:12 -07:00
|
|
|
|
|
|
|
import type { Readable } from 'stream';
|
|
|
|
import type { BinaryData } from './types';
|
|
|
|
|
|
|
|
const EXECUTION_ID_EXTRACTOR =
|
|
|
|
/^(\w+)(?:[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12})$/;
|
|
|
|
|
|
|
|
export class FileSystemManager implements BinaryData.Manager {
|
|
|
|
constructor(private storagePath: string) {}
|
|
|
|
|
|
|
|
async init() {
|
2023-09-25 01:07:06 -07:00
|
|
|
await ensureDirExists(this.storagePath);
|
2023-09-22 08:22:12 -07:00
|
|
|
}
|
|
|
|
|
2023-09-25 01:07:06 -07:00
|
|
|
getPath(fileId: string) {
|
|
|
|
return this.resolvePath(fileId);
|
2023-09-22 08:22:12 -07:00
|
|
|
}
|
|
|
|
|
2023-09-25 07:59:45 -07:00
|
|
|
async getAsStream(fileId: string, chunkSize?: number) {
|
2023-09-25 01:07:06 -07:00
|
|
|
const filePath = this.getPath(fileId);
|
2023-09-22 08:22:12 -07:00
|
|
|
|
|
|
|
return createReadStream(filePath, { highWaterMark: chunkSize });
|
|
|
|
}
|
|
|
|
|
2023-09-25 01:07:06 -07:00
|
|
|
async getAsBuffer(fileId: string) {
|
|
|
|
const filePath = this.getPath(fileId);
|
2023-09-22 08:22:12 -07:00
|
|
|
|
|
|
|
try {
|
|
|
|
return await fs.readFile(filePath);
|
|
|
|
} catch {
|
|
|
|
throw new Error(`Error finding file: ${filePath}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-25 01:07:06 -07:00
|
|
|
async getMetadata(fileId: string): Promise<BinaryData.Metadata> {
|
|
|
|
const filePath = this.resolvePath(`${fileId}.metadata`);
|
2023-09-22 08:22:12 -07:00
|
|
|
|
|
|
|
return jsonParse(await fs.readFile(filePath, { encoding: 'utf-8' }));
|
|
|
|
}
|
|
|
|
|
2023-09-25 01:07:06 -07:00
|
|
|
async store(
|
2023-09-25 09:04:52 -07:00
|
|
|
_workflowId: string,
|
2023-09-25 01:07:06 -07:00
|
|
|
executionId: string,
|
2023-09-25 09:04:52 -07:00
|
|
|
bufferOrStream: Buffer | Readable,
|
2023-09-25 01:07:06 -07:00
|
|
|
{ mimeType, fileName }: BinaryData.PreWriteMetadata,
|
|
|
|
) {
|
|
|
|
const fileId = this.createFileId(executionId);
|
|
|
|
const filePath = this.getPath(fileId);
|
2023-09-22 08:22:12 -07:00
|
|
|
|
2023-09-25 09:04:52 -07:00
|
|
|
await fs.writeFile(filePath, bufferOrStream);
|
2023-09-22 08:22:12 -07:00
|
|
|
|
2023-09-25 01:07:06 -07:00
|
|
|
const fileSize = await this.getSize(fileId);
|
|
|
|
|
|
|
|
await this.storeMetadata(fileId, { mimeType, fileName, fileSize });
|
|
|
|
|
|
|
|
return { fileId, fileSize };
|
2023-09-22 08:22:12 -07:00
|
|
|
}
|
|
|
|
|
2023-09-25 01:07:06 -07:00
|
|
|
async deleteOne(fileId: string) {
|
|
|
|
const filePath = this.getPath(fileId);
|
2023-09-22 08:22:12 -07:00
|
|
|
|
|
|
|
return fs.rm(filePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
async deleteManyByExecutionIds(executionIds: string[]) {
|
|
|
|
const set = new Set(executionIds);
|
|
|
|
const fileNames = await fs.readdir(this.storagePath);
|
|
|
|
const deletedIds = [];
|
|
|
|
|
|
|
|
for (const fileName of fileNames) {
|
|
|
|
const executionId = fileName.match(EXECUTION_ID_EXTRACTOR)?.[1];
|
|
|
|
|
|
|
|
if (executionId && set.has(executionId)) {
|
|
|
|
const filePath = this.resolvePath(fileName);
|
|
|
|
|
|
|
|
await Promise.all([fs.rm(filePath), fs.rm(`${filePath}.metadata`)]);
|
|
|
|
|
|
|
|
deletedIds.push(executionId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return deletedIds;
|
|
|
|
}
|
|
|
|
|
2023-09-25 01:07:06 -07:00
|
|
|
async copyByFilePath(
|
2023-09-25 09:04:52 -07:00
|
|
|
_workflowId: string,
|
2023-09-25 01:07:06 -07:00
|
|
|
executionId: string,
|
2023-09-25 09:04:52 -07:00
|
|
|
filePath: string,
|
2023-09-25 01:07:06 -07:00
|
|
|
{ mimeType, fileName }: BinaryData.PreWriteMetadata,
|
|
|
|
) {
|
|
|
|
const newFileId = this.createFileId(executionId);
|
|
|
|
|
|
|
|
await fs.cp(filePath, this.getPath(newFileId));
|
2023-09-22 08:22:12 -07:00
|
|
|
|
2023-09-25 01:07:06 -07:00
|
|
|
const fileSize = await this.getSize(newFileId);
|
2023-09-22 08:22:12 -07:00
|
|
|
|
2023-09-25 01:07:06 -07:00
|
|
|
await this.storeMetadata(newFileId, { mimeType, fileName, fileSize });
|
|
|
|
|
|
|
|
return { fileId: newFileId, fileSize };
|
2023-09-22 08:22:12 -07:00
|
|
|
}
|
|
|
|
|
2023-09-25 09:04:52 -07:00
|
|
|
async copyByFileId(_workflowId: string, executionId: string, fileId: string) {
|
2023-09-25 01:07:06 -07:00
|
|
|
const newFileId = this.createFileId(executionId);
|
2023-09-22 08:22:12 -07:00
|
|
|
|
2023-09-25 01:07:06 -07:00
|
|
|
await fs.copyFile(this.resolvePath(fileId), this.resolvePath(newFileId));
|
2023-09-22 08:22:12 -07:00
|
|
|
|
2023-09-25 01:07:06 -07:00
|
|
|
return newFileId;
|
2023-09-22 08:22:12 -07:00
|
|
|
}
|
|
|
|
|
2023-09-25 03:30:28 -07:00
|
|
|
async rename(oldFileId: string, newFileId: string) {
|
|
|
|
const oldPath = this.getPath(oldFileId);
|
|
|
|
const newPath = this.getPath(newFileId);
|
|
|
|
|
|
|
|
await Promise.all([
|
|
|
|
rename(oldPath, newPath),
|
|
|
|
rename(`${oldPath}.metadata`, `${newPath}.metadata`),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2023-09-22 08:22:12 -07:00
|
|
|
// ----------------------------------
|
|
|
|
// private methods
|
|
|
|
// ----------------------------------
|
|
|
|
|
2023-09-25 01:07:06 -07:00
|
|
|
private createFileId(executionId: string) {
|
2023-09-22 08:22:12 -07:00
|
|
|
return [executionId, uuid()].join('');
|
|
|
|
}
|
|
|
|
|
|
|
|
private resolvePath(...args: string[]) {
|
|
|
|
const returnPath = path.join(this.storagePath, ...args);
|
|
|
|
|
|
|
|
if (path.relative(this.storagePath, returnPath).startsWith('..')) {
|
|
|
|
throw new FileNotFoundError('Invalid path detected');
|
|
|
|
}
|
|
|
|
|
|
|
|
return returnPath;
|
|
|
|
}
|
2023-09-25 01:07:06 -07:00
|
|
|
|
|
|
|
private async storeMetadata(fileId: string, metadata: BinaryData.Metadata) {
|
|
|
|
const filePath = this.resolvePath(`${fileId}.metadata`);
|
|
|
|
|
|
|
|
await fs.writeFile(filePath, JSON.stringify(metadata), { encoding: 'utf-8' });
|
|
|
|
}
|
2023-09-25 09:04:52 -07:00
|
|
|
|
|
|
|
private async getSize(fileId: string) {
|
|
|
|
const filePath = this.getPath(fileId);
|
|
|
|
|
|
|
|
try {
|
|
|
|
const stats = await fs.stat(filePath);
|
|
|
|
return stats.size;
|
|
|
|
} catch (error) {
|
|
|
|
throw new Error('Failed to find binary data file in filesystem', { cause: error });
|
|
|
|
}
|
|
|
|
}
|
2023-09-22 08:22:12 -07:00
|
|
|
}
|