File storage Kubik, for Rubik's applications
This module is designed to be used with rubik-main
.
const { App, Kubiks } = require('rubik-main');
const File = require('rubik-file');
const FileFS = require('rubik-file-fs');
// Creating application instance
const app = new App();
// Creating config kubik, add path to the config directory
// Config kubik is a depedency of File kubik
const config = new Kubiks.Config(path.join(__dirname, '../config/'));
// Creating file kubik
const file = new File({
provider: 'FS',
bucket: 'main'
});
// Add filesystem provider constructor
File.addProvider('FS', FileFS);
app.add([config, file]);
app.up().
then(async () => {
console.info('Application started');
const rs = fs.createReadStream(path.join(__dirname, './in/file.txt'));
// Write to the storage
await app.file.write({ key: 'some-file.txt' }, rs);
// Read from the storage
const stream = await app.file.read({ key: 'some-file.txt' });
const ws = fs.createWriteStream(path.join(__dirname, './out/file.txt'));
stream.pipe(ws);
}).
catch((err) => {
console.error(err);
process.exit(1);
});
To make it easier to extend File kubik with different storages, the logic for working with them must be placed in classes called Providers.
A provider is a Class-child of the Provider class.
The Provider should has an implementation of the write
, has
, read
, and remove
methods.
Optionally, there may also be a copy
method - for copying files inside storage.
I will use this type bellow for some methods' arguments and params
Basically it is an Object with two keys:
-
key
is an file identifier inside storage -
bucket
is an abstraction of Drive in some kind of storages
More about keys and buckets you can read in the AWS documentation.
Writes stream to a storage
There two required arguments:
-
to
is aSource
object,key
andbucket
inside storage for new file -
readableStream
is a Readable Stream
It should return Promise
(or be async
) that resolves nothing
If an error occurs, an exception must be thrown.
Checks file in the storage
There only one required argument:
-
source
is aSource
object, key and bucket inside storage
It should return Promise
(or be async
) that resolves Boolean
,
-
true
, when file exists; -
false
, when file does not exists;
If an error occurs, an exception must be thrown.
Reads file from the storage
There only one required argument:
-
from
is aSource
object, key and bucket inside storage
It should return Promise
(or be async
) that resolves Readable Stream,
If an error occurs, an exception must be thrown.
Removes file from the storage
There only one required argument:
-
source
is aSource
object, key and bucket inside storage
It should return Promise
(or be async
) that resolves nothing
If an error occurs, an exception must be thrown.
Copies file from one destination to other. Inside the storage.
It is an optional method. If it is not exists,
File kubik just calls read(from)
and write(to, streamFrom)
.
There two required arguments:
-
from
is aSource
object,key
andbucket
inside storage for existing file -
to
is aSource
object,key
andbucket
inside storage for new file
It should return Promise
(or be async
) that resolves nothing
If an error occurs, an exception must be thrown.