Skip to content

Instantly share code, notes, and snippets.

@yulioaj290
Forked from jorgelbg/packages.json
Last active February 13, 2020 12:52
Show Gist options
  • Save yulioaj290/79e4886eccf434ba19bb92875fd1f0f4 to your computer and use it in GitHub Desktop.
Save yulioaj290/79e4886eccf434ba19bb92875fd1f0f4 to your computer and use it in GitHub Desktop.
NodeJS OwnCloud Uploader

OwnCloud uploader

Requirements

  • NodeJS 8.4.0 or higher.
  • NPM 5.3.0 or higher.
  • Bzip2 1.0.6 or higher.

To prepare the environment for the script follow this steps (based on Ubuntu 14.04):

  1. (Optional) Update your linux repositories and upgrade all packages.
$ sudo apt-get update
$ sudo apt-get upgrade
  1. Install bzip2 on your system, it is neccesary to unzip node package modules.
$ sudo apt-get install bzip2
  1. Install PhantomJS dependencies on your Debian/Ubuntu (or Linux) system.
$ sudo apt-get install libfontconfig1 fontconfig libfontconfig1-dev libfreetype6-dev
  1. Install node dependencies included into the package.json.
$ npm install
  1. Verify the packages "phantomjs" and "casperjs" have been installed and linked on executable binaries system folder '/usr/bin/'.
$ phantomjs -v
$ casperjs -v

or

$ phantomjs --version
$ casperjs --version

Note: If some of this packages were trouble, you must create a symbolic link of their executables.

$ ln -s /absolute/path/to/project/node_modules/phantomjs/bin/phantomjs /usr/bin/phantomjs
$ ln -s /absolute/path/to/project/node_modules/casperjs/bin/casperjs /usr/bin/casperjs
  • Check again the step 4.
  1. Config the user credentials inside the "uploader.js" file.
* const USER = 'testuser';
* const PASSWORD = 'TestPassw0rd';
  1. Execute the script, specifying the folder of the file to upload.
$ casperjs --ignore-ssl-errors=true uploader.js <dir>
{
"version": "0.0.1",
"private": true,
"name": "owncloud-uploader",
"description": "OwnCloud uploader",
"repository": "https://github.com/jorgelbg/owncloud-uploader",
"license": "MIT",
"devDependencies": {
"casperjs": "^1.1.1",
"http-server": "^0.6.1",
"karma": "^0.12.16",
"karma-chrome-launcher": "^0.1.4",
"karma-jasmine": "^0.1.5",
"phantomjs": "^1.9.19",
"protractor": "~1.0.0",
"shelljs": "^0.2.6",
"tmp": "0.0.23"
},
"scripts": {
"prestart": "npm install",
"start": "http-server -a 0.0.0.0 -p 8000",
"pretest": "npm install",
"test": "node node_modules/karma/bin/karma start test/karma.conf.js",
"test-single-run": "node node_modules/karma/bin/karma start test/karma.conf.js --single-run",
"preupdate-webdriver": "npm install",
"update-webdriver": "webdriver-manager update",
"preprotractor": "npm run update-webdriver",
"protractor": "protractor test/protractor-conf.js",
"update-index-async": "node -e \"require('shelljs/global'); sed('-i', /\\/\\/@@NG_LOADER_START@@[\\s\\S]*\\/\\/@@NG_LOADER_END@@/, '//@@NG_LOADER_START@@\\n' + '\\n//@@NG_LOADER_END@@', 'app/index-async.html');\""
}
}
/**
* Copyright 2016 Jorge Luis Betanourt
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
var fs = require('fs')
var args = require('system').args
var casper = require('casper').create({
verbose: true,
pageSettings: {
webSecurityEnabled: false,
// resourceTimeout: 10000,
}
});
const URL = 'https://correo.uci.cu/owncloud/';
const USER = 'testuser';
const PASSWORD = 'TestPassw0rd';
if (args.length < 5) {
casper.echo("Usage: casperjs --ignore-ssl-errors=true script.js <dir>")
casper.exit(0)
}
BASEDIR = args[4]
if (BASEDIR[BASEDIR.length - 1] != '/') {
BASEDIR = BASEDIR + '/'
}
// wait 7 seconds before giving up
casper.options.waitTimeout = 10000000
casper.onResourceTimeout = function(request) {
console.log('Response (#' + request.id + '): ' + JSON.stringify(request));
};
casper.userAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X); Safari');
casper.on('started', function () {
this.page.customHeaders = { 'Accept': '*/*' }
});
casper.on('resource.error',function (request) {
this.echo('[Res ID: ' + request.id + ' | URL: ' + request.url + ' | Error Message: ' + request.errorString + ']');
});
casper.start(URL, function() {
this.echo(this.getTitle());
this.fill('form[name="login"]', {
user: USER,
password: PASSWORD
}, true);
})
casper.then(function () {
this.echo(this.getTitle());
// this.capture('pepe.png')
// check if are any notifications
var info = this.getElementInfo('#notification')
if (info.visible) {
this.echo("There is some error, probably your storage is full")
this.exit(-1)
}
})
casper.then(function () {
var list = fs.list(BASEDIR)
for (var i = 0; i < list.length; i++) {
var filename = BASEDIR + list[i]
if (fs.isFile(filename)) {
this.echo(filename)
var info = this.getElementInfo('#notification')
if (info.visible) {
this.echo("There is some error, probably your storage is full")
this.exit(-1)
}
this.page.uploadFile('input[type="file"]', filename);
this.echo("Processing ..." + filename)
this.waitWhileVisible('button.stop.icon-close', function () {
// this.capture('pepe.png');
this.echo('Done!');
})
}
}
})
casper.run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment