tweedegolf / storage-abstraction

Provides an abstraction layer for interacting with a storage; the storage can be local or in the cloud.
MIT License
107 stars 18 forks source link

Add method to save files directly from a filestream #2

Closed josafatburmeister closed 4 years ago

josafatburmeister commented 4 years ago

Problem to solve

I would highly appreciate if the storage-abstraction interface would offer a method to save a Readable Stream to a file. Right now, this is only possible by using a workaround like this:

const { Storage } = require("@tweedegolf/storage-abstraction")
const storageConfig = {..}

const storage = new Storage(storageConfig);
const readStream = createReadStreamSomeHow();

const chunks = [];
readStream.once('end', () => {
   const buffer = Buffer.concat(chunks);
   storage.addFileFromBuffer(buffer, 'targetPath');
});
readStream.on('data', (chunk) => {
   chunks.push(chunk);
});

Unfortunately this code is neither performant nor comprehensible.

Proposed solution

I suggest to add method addFileFromStream(fileStream: Readable, targetPath: string): Promise<void> to the storage-abstraction API. Using such a method could look like this:

const { Storage } = require("@tweedegolf/storage-abstraction")
const storageConfig = {..}

const storage = new Storage(storageConfig);
const readStream = createReadStreamSomeHow();
await storage.addFileFromReadStream(readStream, 'targetPath'); 
marlonbaeten commented 4 years ago

@abudaan could you take a look at this?

abudaan commented 4 years ago

Implemented in 1.3.1