Concatenates chunked file to one (big) file on the server-side in asynchronous manner. The primary motivation is to be able to support Tus' Concatenation Extension.
- Download 'Concatenator.js' file to appropriate location.
- Require the downloaded file in the source file that you want to use Concatenator class.
// index.js
// ...
const Concatenator = require('./Concatenator');
// ...
- Instantiate a new Concatenator object with source files.
// index.js
// ...
const Concatenator = require('./Concatenator');
// Below two lines added
const chunkFilepaths = ['/path/to/upload/dir/x00', '/path/to/upload/dir/x01', '/path/to/upload/dir/x02'];
const cctr = new Concatenator(chunkFilepaths);
// ...
- Listen Concatenator instance events.
// index.js
// ...
const Concatenator = require('./Concatenator');
const chunkFilepaths = ['/path/to/upload/dir/x00', '/path/to/upload/dir/x01', '/path/to/upload/dir/x02'];
const cctr = new Concatenator(chunkFilepaths);
// Below lines added
// You should always listen for 'error' events, like every EventEmitter class.
cctr.on('error', (err) => {
// Do something with the error
console.error(err);
});
// We are going to listen 'ready' event once.
cctr.once('ready', () => {
// All promises started in constructor were settled.
// We can start the concatenation.
cctr.start();
});
cctr.on('progress', (processed, total) => {
const percentage = ((100 * processed) / total).toFixed(2);
// Sample output of the log below might be;
// %42.13 - 4213/10000 bytes
console.log(`%${percentage} - ${processed}/${total} bytes`);
});
// We are going to listen 'done' event once.
cctr.once('done', (finalFile, bytesWritten) => {
// Concatenation finished
// Sample output of the log below might be;
// 10000 bytes written to '/path/to/upload/dir/x00'
console.log(`${bytesWritten} bytes written to '${finalFile}'`);
// Optionally you can remove all the listeners for this instance.
cctr.removeAllListeners();
// From now on you are done with this Concatenator instance. You can
// use `finalFile` parameter to access the final (concatenated) file.
});
// ...
- The instance should be 'ready' to
start
the concatenation. This is because the Concatenator tries to calculate chunks size (via first chunk) and last chunk size, and then depending on them the total size of the concatenated file. All of the file system methods are asynchronous. - Normally the constructor takes one argument and concatenate rest of the chunks to first chunk. What this means is that unless a destination filepath is given to the constructor Concatenator will not create a file to concatenate all the chunks into that. Constructor accepts a destination filepath as 2nd argument. This destination file path tells the Concatenator to create a file and concatenate all of the chunks into that (destination) file. Of course destination file can be somewhere else in the file system. See
examples.js
. - Also the constructor accepts an
options
object. Viaoptions
you can pass chunk size (in bytes) to ease the size calculation process for Concatenator, and pass the filename of final (concatenated) file's name. All of the options can be omitted. Ifoptions.chunkSize
omitted, Concatenator tries to calculate the chunk size by first chunk's size. Ifoptions.filename
omitted, Concatenator first try to decide the file name for the final file via destination filepath argument. If this is not the case, then the final file name will be the first chunk's file name.
- Calling
start
on not ready instance will not start the concatenation. Instead emits an error saying 'The instance is not ready yet.'. You should listen 'ready' event tostart
the concatenation. - First argument of the constructor must be array. The order of the array reflects the order of the chunks, hence the chunks will be concatenated to final file in this order.
MIT License
Copyright (c) 2017 Ozan Müyesseroğlu ozanmuyes@gmail.com
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.