mirror of
https://github.com/colanode/colanode.git
synced 2025-12-16 11:47:47 +01:00
60 lines
1.6 KiB
TypeScript
60 lines
1.6 KiB
TypeScript
import { createReadStream, createWriteStream, promises as fs } from 'fs';
|
|
import { Readable } from 'stream';
|
|
|
|
import { FileStore } from '@tus/file-store';
|
|
import { DataStore } from '@tus/server';
|
|
|
|
import type { FileStorageConfig } from '@colanode/server/lib/config/storage';
|
|
|
|
import type { Storage } from './core';
|
|
|
|
export class FileSystemStorage implements Storage {
|
|
private readonly directory: string;
|
|
public readonly tusStore: DataStore;
|
|
|
|
constructor(config: FileStorageConfig) {
|
|
this.directory = config.directory;
|
|
this.tusStore = new FileStore({ directory: this.directory });
|
|
}
|
|
|
|
async download(
|
|
path: string
|
|
): Promise<{ stream: Readable; contentType?: string }> {
|
|
const fullPath = `${this.directory}/${path}`;
|
|
const stream = createReadStream(fullPath);
|
|
|
|
return {
|
|
stream,
|
|
contentType: undefined,
|
|
};
|
|
}
|
|
|
|
async delete(path: string): Promise<void> {
|
|
const fullPath = `${this.directory}/${path}`;
|
|
await fs.unlink(fullPath);
|
|
}
|
|
|
|
async upload(
|
|
path: string,
|
|
data: Buffer | Readable,
|
|
_contentType: string,
|
|
_contentLength?: bigint
|
|
): Promise<void> {
|
|
const fullPath = `${this.directory}/${path}`;
|
|
const dirPath = fullPath.substring(0, fullPath.lastIndexOf('/'));
|
|
await fs.mkdir(dirPath, { recursive: true });
|
|
|
|
if (data instanceof Buffer) {
|
|
await fs.writeFile(fullPath, data);
|
|
return;
|
|
}
|
|
|
|
const writeStream = createWriteStream(fullPath);
|
|
await new Promise<void>((resolve, reject) => {
|
|
(data as Readable).pipe(writeStream);
|
|
writeStream.on('finish', resolve);
|
|
writeStream.on('error', reject);
|
|
});
|
|
}
|
|
}
|