feat(core): Remove storeMetadata and getSize from binary data manager interface (no-changelog) (#7195)

Depends on: #7164 | Story:
[PAY-838](https://linear.app/n8n/issue/PAY-838/introduce-object-store-service-for-binary-data)

This PR removes `storeMetadata` and `getSize` from the binary data
manager interface, as these are specific to filesystem mode. Also this
disambiguates identifiers:

```
binaryDataId
filesystem:289b4aac51e-dac6-4167-b793-6d5c415e2b47 {mode}:{fileId}

fileId - FS
289b4aac51e-dac6-4167-b793-6d5c415e2b47 {executionId}{uuid}

fileId - S3
/workflows/{workflowId}/executions/{executionId}/binary_data/b4aac51e-dac6-4167-b793-6d5c415e2b47
```

Note: The object store changes originally in this PR were extracted out
into the final PR.

---------

Co-authored-by: कारतोफ्फेलस्क्रिप्ट™ <aditya@netroy.in>
This commit is contained in:
Iván Ovejero
2023-09-25 10:07:06 +02:00
committed by GitHub
parent 6d6e2488c6
commit dcc9cc13ed
7 changed files with 143 additions and 132 deletions

View File

@@ -5,9 +5,9 @@ import { v4 as uuid } from 'uuid';
import { jsonParse } from 'n8n-workflow';
import { FileNotFoundError } from '../errors';
import { ensureDirExists } from './utils';
import type { Readable } from 'stream';
import type { BinaryMetadata } from 'n8n-workflow';
import type { BinaryData } from './types';
const EXECUTION_ID_EXTRACTOR =
@@ -17,15 +17,15 @@ export class FileSystemManager implements BinaryData.Manager {
constructor(private storagePath: string) {}
async init() {
await this.ensureDirExists(this.storagePath);
await ensureDirExists(this.storagePath);
}
getPath(identifier: string) {
return this.resolvePath(identifier);
getPath(fileId: string) {
return this.resolvePath(fileId);
}
async getSize(identifier: string) {
const filePath = this.getPath(identifier);
async getSize(fileId: string) {
const filePath = this.getPath(fileId);
try {
const stats = await fs.stat(filePath);
@@ -35,14 +35,14 @@ export class FileSystemManager implements BinaryData.Manager {
}
}
getStream(identifier: string, chunkSize?: number) {
const filePath = this.getPath(identifier);
getAsStream(fileId: string, chunkSize?: number) {
const filePath = this.getPath(fileId);
return createReadStream(filePath, { highWaterMark: chunkSize });
}
async getBuffer(identifier: string) {
const filePath = this.getPath(identifier);
async getAsBuffer(fileId: string) {
const filePath = this.getPath(fileId);
try {
return await fs.readFile(filePath);
@@ -51,29 +51,31 @@ export class FileSystemManager implements BinaryData.Manager {
}
}
async storeMetadata(identifier: string, metadata: BinaryMetadata) {
const filePath = this.resolvePath(`${identifier}.metadata`);
await fs.writeFile(filePath, JSON.stringify(metadata), { encoding: 'utf-8' });
}
async getMetadata(identifier: string): Promise<BinaryMetadata> {
const filePath = this.resolvePath(`${identifier}.metadata`);
async getMetadata(fileId: string): Promise<BinaryData.Metadata> {
const filePath = this.resolvePath(`${fileId}.metadata`);
return jsonParse(await fs.readFile(filePath, { encoding: 'utf-8' }));
}
async store(binaryData: Buffer | Readable, executionId: string) {
const identifier = this.createIdentifier(executionId);
const filePath = this.getPath(identifier);
async store(
binaryData: Buffer | Readable,
executionId: string,
{ mimeType, fileName }: BinaryData.PreWriteMetadata,
) {
const fileId = this.createFileId(executionId);
const filePath = this.getPath(fileId);
await fs.writeFile(filePath, binaryData);
return identifier;
const fileSize = await this.getSize(fileId);
await this.storeMetadata(fileId, { mimeType, fileName, fileSize });
return { fileId, fileSize };
}
async deleteOne(identifier: string) {
const filePath = this.getPath(identifier);
async deleteOne(fileId: string) {
const filePath = this.getPath(fileId);
return fs.rm(filePath);
}
@@ -98,35 +100,35 @@ export class FileSystemManager implements BinaryData.Manager {
return deletedIds;
}
async copyByPath(filePath: string, executionId: string) {
const identifier = this.createIdentifier(executionId);
async copyByFilePath(
filePath: string,
executionId: string,
{ mimeType, fileName }: BinaryData.PreWriteMetadata,
) {
const newFileId = this.createFileId(executionId);
await fs.cp(filePath, this.getPath(identifier));
await fs.cp(filePath, this.getPath(newFileId));
return identifier;
const fileSize = await this.getSize(newFileId);
await this.storeMetadata(newFileId, { mimeType, fileName, fileSize });
return { fileId: newFileId, fileSize };
}
async copyByIdentifier(identifier: string, executionId: string) {
const newIdentifier = this.createIdentifier(executionId);
async copyByFileId(fileId: string, executionId: string) {
const newFileId = this.createFileId(executionId);
await fs.copyFile(this.resolvePath(identifier), this.resolvePath(newIdentifier));
await fs.copyFile(this.resolvePath(fileId), this.resolvePath(newFileId));
return newIdentifier;
return newFileId;
}
// ----------------------------------
// private methods
// ----------------------------------
private async ensureDirExists(dir: string) {
try {
await fs.access(dir);
} catch {
await fs.mkdir(dir, { recursive: true });
}
}
private createIdentifier(executionId: string) {
private createFileId(executionId: string) {
return [executionId, uuid()].join('');
}
@@ -139,4 +141,10 @@ export class FileSystemManager implements BinaryData.Manager {
return returnPath;
}
private async storeMetadata(fileId: string, metadata: BinaryData.Metadata) {
const filePath = this.resolvePath(`${fileId}.metadata`);
await fs.writeFile(filePath, JSON.stringify(metadata), { encoding: 'utf-8' });
}
}