<commit_before>/**
 * Nanocloud turns any traditional software into a cloud solution, without
 * changing or redeveloping existing source code.
 *
 * Copyright (C) 2016 Nanocloud Software
 *
 * This file is part of Nanocloud.
 *
 * Nanocloud is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * Nanocloud is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General
 * Public License
 * along with this program.  If not, see
 * <http://www.gnu.org/licenses/>.
 */

const http = require("http");
const fs = require("fs");
const request = require("request-promise");
const Promise = require("bluebird");

module.exports = {

  exec: function(hostname, port, cmd, stdin) {
    let options = {
      url: "http://" + hostname + ":" + port + "/exec",
      headers: {
        'Content-Type': 'application/json'
      },
      json: true,
      body: {
        "command": cmd,
        "stdin": stdin
      },
      method: 'POST'
    };

    return request(options);
  },

  files: function(storage, filename, path) {
    let options = {
      url: "http://" + storage.hostname + ":" + storage.port + "/files?path=" + encodeURI(path),
      method: 'GET'
    };

    return request(options).then((result) => {
      return JSON.parse(result);
    });
  },

  download: function(storage, path) {
    let options = {
      host: storage.hostname,
      path: "/files?path=" + encodeURI(path),
      port: storage.port,
      method: 'GET',
    };

    return new Promise(function(resolve, reject) {
      let req = http.request(options, function(response) {
        if (response.statusCode !== 200) {
          return reject(response);
        }
        return resolve(response);
      });
      req.end();
    });
  },

  upload: function(storage, file) {
    let options = {
      host: storage.hostname,
      path: "/upload?filename=" + encodeURI(file.filename) + "&username=" + storage.username,
      port: storage.port,
      method: 'POST'
    };

    return new Promise(function(resolve, reject) {
      let readableStream = fs.createReadStream(file.fd);
      readableStream.pipe(http.request(options, (response) => {
        if (response.statusCode !== 200) {
          return reject(response);
        }<commit_msg>Add documentation for PlazaService.<commit_after>/**
 * Nanocloud turns any traditional software into a cloud solution, without
 * changing or redeveloping existing source code.
 *
 * Copyright (C) 2016 Nanocloud Software
 *
 * This file is part of Nanocloud.
 *
 * Nanocloud is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * Nanocloud is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General
 * Public License
 * along with this program.  If not, see
 * <http://www.gnu.org/licenses/>.
 */

const http = require("http");
const fs = require("fs");
const request = require("request-promise");
const Promise = require("bluebird");

module.exports = {

  /**
   * exec
   *
   * Exec a windwows command with Plaza
   *
   * @param {String} storage hostname
   * @param {string} listening storage port
   * @param {[]string} commands to execute
   * @return {Promise} Request to Plaza
   */
  exec: function(hostname, port, cmd, stdin) {
    let options = {
      url: "http://" + hostname + ":" + port + "/exec",
      headers: {
        'Content-Type': 'application/json'
      },
      json: true,
      body: {
        "command": cmd,
        "stdin": stdin
      },
      method: 'POST'
    };

    return request(options);
  },

  /**
   * files
   *
   * Ask a list of files
   *
   * @param {Object} storage used by user
   * @param {string} path where we ask files
   * @return {Promise} Request to Plaza
   */
  files: function(storage, path) {
    let options = {
      url: "http://" + storage.hostname + ":" + storage.port + "/files?path=" + encodeURI(path),
      method: 'GET'
    };

    return request(options).then((result) => {
      return JSON.parse(result);
    });
  },

  /**
   * download
   *
   * Ask to download a file
   *
   * @param {Object} storage used by user
   * @param {string} path of file
   * @return {Promise} Request to Plaza
   */
  download: function(storage, path) {
    let options = {
      host: storage.hostname,
      path: "/files?path=" + encodeURI(path),
      port: storage.port,
      method: 'GET',
    };

    return new Promise(function(resolve, reject) {
      let req = http.request(options, function(response) {
        if (response.statusCode !== 200) {
          return reject(response);
        }
        return resolve(response);
      });
      req.end();
    });
  },

  /**
   * upload
   *
   * Ask to upload a file
   *
   * @param {Object} storage used by user
   * @param {Object} Object of the new file
   * @return {Promise} Request to Plaza
   */
  upload: function(storage, file) {
    let options = {
      host: storage.hostname,
      path: "/upload?filename=" + encodeURI(file.filename) + "&username=" + storage.username,
      port: storage.port,
      method: 'POST'
    };

    return new Promise(function(resolve, reject) {
      let readableStream = fs.createReadStream(file.fd);
      readableStream.pipe(http.request(options, (response) => {
        if (response.statusCode !== 200) {
          return reject(response);
        }
{'fraction_non_alphanumeric': 0.12438625204582651, 'fraction_numerical': 0.003600654664484452, 'mean_word_length': 2.6991525423728815, 'pattern_counts': {'":': 8, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3922688', 'n_tokens_mistral': 1802, 'n_tokens_neox': 1737, 'n_words': 588}