refactor(core): Include workflow ID in binary data writes (no-changelog) (#7220)

Depends on: https://github.com/n8n-io/n8n/pull/7195 | Story:
[PAY-837](https://linear.app/n8n/issue/PAY-837/implement-object-store-manager-for-binary-data)

This PR includes `workflowId` in binary data writes so that the S3
manager can support this filepath structure
`/workflows/{workflowId}/executions/{executionId}/binaryData/{binaryFilename}`
to easily delete binary data for workflows. Also all binary data service
and manager methods that take `workflowId` and `executionId` are made
consistent in arg order.

Note: `workflowId` is included in filesystem mode for compatibility with
the common interface, but `workflowId` will remain unused by filesystem
mode until we decide to restructure how this mode stores data.

---------

Co-authored-by: कारतोफ्फेलस्क्रिप्ट™ <aditya@netroy.in>
This commit is contained in:
Iván Ovejero
2023-09-25 18:04:52 +02:00
committed by GitHub
parent 75541e91f2
commit 77d6e3fc07
9 changed files with 121 additions and 70 deletions

View File

@@ -1,3 +1,9 @@
/**
* @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.
*/
import { createReadStream } from 'fs';
import fs from 'fs/promises';
import path from 'path';
@@ -25,17 +31,6 @@ export class FileSystemManager implements BinaryData.Manager {
return this.resolvePath(fileId);
}
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 });
}
}
async getAsStream(fileId: string, chunkSize?: number) {
const filePath = this.getPath(fileId);
@@ -59,14 +54,15 @@ export class FileSystemManager implements BinaryData.Manager {
}
async store(
binaryData: Buffer | Readable,
_workflowId: string,
executionId: string,
bufferOrStream: Buffer | Readable,
{ mimeType, fileName }: BinaryData.PreWriteMetadata,
) {
const fileId = this.createFileId(executionId);
const filePath = this.getPath(fileId);
await fs.writeFile(filePath, binaryData);
await fs.writeFile(filePath, bufferOrStream);
const fileSize = await this.getSize(fileId);
@@ -102,8 +98,9 @@ export class FileSystemManager implements BinaryData.Manager {
}
async copyByFilePath(
filePath: string,
_workflowId: string,
executionId: string,
filePath: string,
{ mimeType, fileName }: BinaryData.PreWriteMetadata,
) {
const newFileId = this.createFileId(executionId);
@@ -117,7 +114,7 @@ export class FileSystemManager implements BinaryData.Manager {
return { fileId: newFileId, fileSize };
}
async copyByFileId(fileId: string, executionId: string) {
async copyByFileId(_workflowId: string, executionId: string, fileId: string) {
const newFileId = this.createFileId(executionId);
await fs.copyFile(this.resolvePath(fileId), this.resolvePath(newFileId));
@@ -158,4 +155,15 @@ export class FileSystemManager implements BinaryData.Manager {
await fs.writeFile(filePath, JSON.stringify(metadata), { encoding: 'utf-8' });
}
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 });
}
}
}