0.1.1 • Published 13 years ago
chunk-loader v0.1.1
chunk-loader
Utility for sending files from a Node.js client to an Express server. Files are split into individual chunks and posted separately. Any failed chunk is automatically retried. Great for large files that are uploaded over inconsistent network connections. Built-in support for file encryption.
Installation
npm install chunk-loader
Client
var client = require('chunk-loader).Client;
var upload = client.uploadFile('../path/to/file.txt', /* options */);
upload.on('progress', function(p) {
console.log('Progress:' + ((p.sent / p.total) * 100) + '%');
});
upload.on('complete', function() {
console.log('Done!');
});
upload.on('error', function(err) {
console.log('Upload error has occurred.');
});
Options
chunkSize
: defaults to65536
. The size of each individual chunk sent to the server.beginHost
: defaults tohttp://localhost:80
. The full host url used for the begin upload request. Thetag
option can be used to pass identifying information, in which case you may want this request to go over https while the file upload goes over http.uploadHost
: defaults tohttp://localhost:80
. The url where uploaded chunks are sent.retryLimit
: defaults to100
. The number of time a failed chunk will retry.encrypted
: defaults tofalse
. Whether or not to encrypt the file before sending. Files are encrypted using the file-encryptor module. Unique encryption keys are automatically generated and supplied by the server for every file.debug
: defaults tofalse
. Enables debug console logging when troubleshooting upload issues.tag
: defaults tonull
. Any custom data that should be sent to the server as part of the upload. This will typically be used to identify the image. Can also be used to supply credentials for server-side authentication.
Server
The server is designed to integrate into an existing Express application.
var express = require('express'),
app = express(),
server = require('chunk-loader').Server();
app.use(server.middleware(/* options */));
// Important! If you use bodyParser, chunk-loader must be applied first.
// app.use(express.bodyParser());
server.on('complete', function(file) {
console.log(file.savePath);
});
Options
saveDir
: defaults toos.tmpDir()
. Folder where uploaded files will be saved. Files will receive an automatically generated name.debug
: defaults tofalse
. Enables debug console logging when troubleshooting upload issues.
Server Authentication
The auth function can be overridden to prevent uploads based on custom criteria. If the callback is invoked with an error parameter, the upload will be cancelled. The tag property is custom data set by the chunk-loader client. It can be any arbitrary data.
server.auth = function(file, callback) {
if(file.tag.username !== 'username' && file.tag.password !== 'password') {
return callback(new Error('Invalid creds.');
}
callback();
};