Giter Site home page Giter Site logo

jscape-rest-uploader's Introduction

jscape-rest-uploader

Node.js REST client for JSCAPE file server. Allows resuming multipart file uploads.

var JscapeRestUploader = require('jscape-rest-uploader');
var uploader = new JscapeRestUploader(
  'https://ftp.example.com',
  'username',
  'secretPassword',
  'your-auth-domain'
);

// Get files in the root. Automaticly login if not authenticated
// Each instance of JscapeRestUploader has it's own auth cookie jar.
uploader.listFiles('/').then(function (list, response) {
  console.log("status: ", response.statusCode);
  list.forEach(function (file) {
    console.log("path: ",                 file.path);
    console.log("name: ",                 file.name);
    console.log("type: ",                 file.type);
    console.log("readable: ",             file.readable);
    console.log("writable: ",             file.writable);
    console.log("size: ",                 file.size);
    console.log("lastModificationDate: ", file.lastModificationDate);
  });
});

Uploads

// You can also login, wait, then upload
uploader.login().then(function () {
  uploader.uploadResumable(localFilepath, targetDirectory, newFilename).then(
    () => {console.log("COMPLETED");},
    function(e) {console.log("FAILURE:", e);},
    function(p) {
      console.log(p.percentage + `%\t${p.transferred} / ${p.length}`);
    }
  );  
}, function (error) {

});

Resumable uploads cut the file into several segments that are sent by the same number of PUT requests to the server so that if the connection fails, the user doesn't have to start the upload from zero. By default the chunk size is 10MB but you can configure that if your users have a slow connections or using really big files files.

The code will look for a file with the same name as targetFilename inside targetDirectory. Care should be taken so that you don't have collisions with pre-existing files.

Upload Buffers

You can also send a buffer instead of a file path with uploadBuffer(). This is usefull for creating a json file without writing to the local disk.

// You can also login, wait, then upload
uploader.login().then(function () {
  var jsonBuffer = new Buffer('{"fromBuffer": true}');
  uploader.uploadBuffer(jsonBuffer, targetDirectory, newFilename).then(
    () => {console.log("COMPLETED");},
  );
}, function (error) {

});

Configuration

  • Skip SSL validation errors: JscapeRestUploader.Config.rejectUnauthorized = false;
  • Set uploadResumable segment size: JscapeRestUploader.Config.uploadSegmentSize = 1024 * 1024 * 5;// 5MB

Scripts

  • Directory listing: With npm, npm run-script ls / -l or with node, node scripts/ls.js /remote-dir
  • Upload file: npm run-script upload source.jpg final.jpg /destination
  • Delete file: npm run-script delete /destination/final.jpg

These scripts will authenticate by looking for a secrets.js file that looks like this:

module.exports = {
  host:     'https://ftp.example.com/',
  username: 'jimmy',
  password: 'secret',
  domain:   'Acme Inc.'
}

You can change your shell NODE_PATH to include the directory where secrets.js can be found:

export NODE_PATH=".:./config"

Test files

To make a dummy test file to upload try openssl rand -out dummy-md.pcap -base64 $((250000000 * 3/4)) to make a 250MB file.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.