Commit 69b6e7f2 authored by Giacomo Lavermicocca's avatar Giacomo Lavermicocca
Browse files

list files

parent f3b1e2be
Loading
Loading
Loading
Loading

.gitignore

0 → 100644
+1 −0
Changes for .gitignore: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
node_modules/
+4 −44
Changes for index.js: 4 added lines, 44 removed lines.
Original line number Diff line number Diff line
const express = require('express');
const app = express();
const dirTree = require('directory-tree');
const fs = require('fs');
const http = require('http');
const request = require('request');

//console.log(JSON.stringify(tree, null, 4));
var client = require('scp2');

function startHere() {

    var download = function (uri, filename, callback) {
        request.head(uri, function (err, res, body) {
            console.log('content-type:', res.headers['content-type']);
            console.log('content-length:', res.headers['content-length']);

            request(uri).pipe(fs.createWriteStream(filename)).on('close', callback);
        });
    };

    var tree = dirTree('/root/.config/transmission-daemon/torrents', { extensions: /\.torrent/ });

    for (var i = 0; i < tree.children.length; i++) {
        if (tree.children[i].type == 'file') {
            var lastSegment = tree.children[i].path;
            lastSegment = lastSegment.substring(lastSegment.lastIndexOf('/'));
            console.log(lastSegment);
            var newFileName = "/mnt1/stuff/Downloads/torrentWatch" + lastSegment.substring(0, lastSegment.lastIndexOf('.'));
            console.log(newFileName);

            download('http://192.168.5.3:3000/' + lastSegment, newFileName, function () {
                console.log('done');
            });
        }
    };
	console.log("Sono passato di qua...");
	client.scp('/root/.config/transmission-daemon/torrents/', 'root:viabadia1982!@10.8.0.10:8022:/mnt1/stuff/Downloads/torrentWatch/', function(err) {console.log(err);})
}

startHere();
setInterval(startHere, 60000);
 No newline at end of file

/* serves all the static files */
app.get(/^(.+)$/, function (req, res) {
    console.log('static file request : ' + req.params);
    //res.sendFile(__dirname + req.params[0]);
    res.sendFile(__dirname + req.params[0]);
});

app.get('/', (req, res) => res.send('Hello World!'))

app.listen(3000, () => console.log('Example app listening on port 3000!'))
 No newline at end of file
+55 −5
Changes for indexServer.js: 55 added lines, 5 removed lines.
Original line number Diff line number Diff line
const express = require('express');
const app = express();
const dirTree = require('directory-tree');
const fs = require('fs');
const http = require('http');
const request = require('request');
const path = require('path');

app.get('/getFiles', getFiles);

function getFiles(request, response) {
    var dirTree = ('/root/.config/transmission-daemon/torrents/');

    diretoryTreeToObj(dirTree, function (err, res) {
        if (err)
            console.error(err);

        console.log(JSON.stringify(res));
        response.send(JSON.stringify(res));
    });
    
}

/* serves all the static files */
app.get(/^(.+)$/, function (req, res) {
    console.log('static file request : ' + req.params);
    //res.sendFile(__dirname + req.params[0]);
    res.sendFile(__dirname + req.params[0]);
    res.sendFile('/root/.config/transmission-daemon/torrents' + req.params[0]);
});

app.get('/', (req, res) => res.send('Hello World!'))
var diretoryTreeToObj = function (dir, done) {
    var results = [];

    fs.readdir(dir, function (err, list) {
        if (err)
            return done(err);

        var pending = list.length;

        if (!pending)
            return done(null, { name: path.basename(dir), type: 'folder', children: results });

        list.forEach(function (file) {
            file = path.resolve(dir, file);
            fs.stat(file, function (err, stat) {
                if (stat && stat.isDirectory()) {
                    diretoryTreeToObj(file, function (err, res) {
                        results.push({
                            name: path.basename(file),
                            type: 'folder',
                            children: res
                        });
                        if (!--pending)
                            done(null, results);
                    });
                }
                else {
                    results.push({
                        type: 'file',
                        name: path.basename(file)
                    });
                    if (!--pending)
                        done(null, results);
                }
            });
        });
    });
};

app.listen(3000, () => console.log('Example app listening on port 3000!'))
+138 −0
Changes for package-lock.json: 138 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -39,6 +39,11 @@
      "resolved": "https://registry.npmjs.org/assert-plus/-/assert-plus-1.0.0.tgz",
      "integrity": "sha1-8S4PPF13sLHN2RRpQuTpbB5N1SU="
    },
    "async": {
      "version": "0.9.2",
      "resolved": "https://registry.npmjs.org/async/-/async-0.9.2.tgz",
      "integrity": "sha1-rqdNXmHB+JlhO/ZL2mbUx48v0X0="
    },
    "asynckit": {
      "version": "0.4.0",
      "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz",
@@ -54,6 +59,11 @@
      "resolved": "https://registry.npmjs.org/aws4/-/aws4-1.6.0.tgz",
      "integrity": "sha1-g+9cqGCysy5KDe7e6MdxudtXRx4="
    },
    "balanced-match": {
      "version": "1.0.0",
      "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz",
      "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c="
    },
    "bcrypt-pbkdf": {
      "version": "1.0.1",
      "resolved": "https://registry.npmjs.org/bcrypt-pbkdf/-/bcrypt-pbkdf-1.0.1.tgz",
@@ -88,6 +98,15 @@
        "hoek": "4.2.0"
      }
    },
    "brace-expansion": {
      "version": "1.1.8",
      "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.8.tgz",
      "integrity": "sha1-wHshHHyVLsH479Uad+8NHTmQopI=",
      "requires": {
        "balanced-match": "1.0.0",
        "concat-map": "0.0.1"
      }
    },
    "bytes": {
      "version": "3.0.0",
      "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.0.0.tgz",
@@ -111,6 +130,11 @@
        "delayed-stream": "1.0.0"
      }
    },
    "concat-map": {
      "version": "0.0.1",
      "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz",
      "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s="
    },
    "content-disposition": {
      "version": "0.5.2",
      "resolved": "https://registry.npmjs.org/content-disposition/-/content-disposition-0.5.2.tgz",
@@ -315,6 +339,11 @@
      "resolved": "https://registry.npmjs.org/fresh/-/fresh-0.5.2.tgz",
      "integrity": "sha1-PYyt2Q2XZWn6g1qx+OSyOhBWBac="
    },
    "fs.realpath": {
      "version": "1.0.0",
      "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz",
      "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8="
    },
    "getpass": {
      "version": "0.1.7",
      "resolved": "https://registry.npmjs.org/getpass/-/getpass-0.1.7.tgz",
@@ -323,6 +352,19 @@
        "assert-plus": "1.0.0"
      }
    },
    "glob": {
      "version": "7.0.6",
      "resolved": "https://registry.npmjs.org/glob/-/glob-7.0.6.tgz",
      "integrity": "sha1-IRuvr0nlJbjNkyYNFKsTYVKz9Xo=",
      "requires": {
        "fs.realpath": "1.0.0",
        "inflight": "1.0.6",
        "inherits": "2.0.3",
        "minimatch": "3.0.4",
        "once": "1.4.0",
        "path-is-absolute": "1.0.1"
      }
    },
    "har-schema": {
      "version": "2.0.0",
      "resolved": "https://registry.npmjs.org/har-schema/-/har-schema-2.0.0.tgz",
@@ -386,6 +428,15 @@
      "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.19.tgz",
      "integrity": "sha512-oTZqweIP51xaGPI4uPa56/Pri/480R+mo7SeU+YETByQNhDG55ycFyNLIgta9vXhILrxXDmF7ZGhqZIcuN0gJQ=="
    },
    "inflight": {
      "version": "1.0.6",
      "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz",
      "integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=",
      "requires": {
        "once": "1.4.0",
        "wrappy": "1.0.2"
      }
    },
    "inherits": {
      "version": "2.0.3",
      "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz",
@@ -401,6 +452,11 @@
      "resolved": "https://registry.npmjs.org/is-typedarray/-/is-typedarray-1.0.0.tgz",
      "integrity": "sha1-5HnICFjfDBsR3dppQPlgEfzaSpo="
    },
    "isarray": {
      "version": "0.0.1",
      "resolved": "https://registry.npmjs.org/isarray/-/isarray-0.0.1.tgz",
      "integrity": "sha1-ihis/Kmo9Bd+Cav8YDiTmwXR7t8="
    },
    "isstream": {
      "version": "0.1.2",
      "resolved": "https://registry.npmjs.org/isstream/-/isstream-0.1.2.tgz",
@@ -438,6 +494,11 @@
        "verror": "1.10.0"
      }
    },
    "lodash": {
      "version": "4.11.2",
      "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.11.2.tgz",
      "integrity": "sha1-1rQzixEKWOIdrlzrz9u/0rxM2zs="
    },
    "media-typer": {
      "version": "0.3.0",
      "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz",
@@ -471,6 +532,14 @@
        "mime-db": "1.30.0"
      }
    },
    "minimatch": {
      "version": "3.0.4",
      "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz",
      "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==",
      "requires": {
        "brace-expansion": "1.1.8"
      }
    },
    "ms": {
      "version": "2.0.0",
      "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
@@ -494,11 +563,24 @@
        "ee-first": "1.1.1"
      }
    },
    "once": {
      "version": "1.4.0",
      "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz",
      "integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=",
      "requires": {
        "wrappy": "1.0.2"
      }
    },
    "parseurl": {
      "version": "1.3.2",
      "resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.2.tgz",
      "integrity": "sha1-/CidTtiZMRlGDBViUyYs3I3mW/M="
    },
    "path-is-absolute": {
      "version": "1.0.1",
      "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz",
      "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18="
    },
    "path-to-regexp": {
      "version": "0.1.7",
      "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.1.7.tgz",
@@ -544,6 +626,17 @@
        "unpipe": "1.0.0"
      }
    },
    "readable-stream": {
      "version": "1.0.34",
      "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-1.0.34.tgz",
      "integrity": "sha1-Elgg40vIQtLyqq+v5MKRbuMsFXw=",
      "requires": {
        "core-util-is": "1.0.2",
        "inherits": "2.0.3",
        "isarray": "0.0.1",
        "string_decoder": "0.10.31"
      }
    },
    "request": {
      "version": "2.83.0",
      "resolved": "https://registry.npmjs.org/request/-/request-2.83.0.tgz",
@@ -578,6 +671,17 @@
      "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.1.tgz",
      "integrity": "sha512-kKvNJn6Mm93gAczWVJg7wH+wGYWNrDHdWvpUmHyEsgCtIwwo3bqPtV4tR5tuPaUhTOo/kvhVwd8XwwOllGYkbg=="
    },
    "scp2": {
      "version": "0.5.0",
      "resolved": "https://registry.npmjs.org/scp2/-/scp2-0.5.0.tgz",
      "integrity": "sha1-ZO50vDaF86TGKQ8tqMHjtO75Lo0=",
      "requires": {
        "async": "0.9.2",
        "glob": "7.0.6",
        "lodash": "4.11.2",
        "ssh2": "0.4.15"
      }
    },
    "send": {
      "version": "0.16.1",
      "resolved": "https://registry.npmjs.org/send/-/send-0.16.1.tgz",
@@ -622,6 +726,25 @@
        "hoek": "4.2.0"
      }
    },
    "ssh2": {
      "version": "0.4.15",
      "resolved": "https://registry.npmjs.org/ssh2/-/ssh2-0.4.15.tgz",
      "integrity": "sha1-B8b0EG2fe26m5N9jbGxT8fmBf/g=",
      "requires": {
        "readable-stream": "1.0.34",
        "ssh2-streams": "0.0.23"
      }
    },
    "ssh2-streams": {
      "version": "0.0.23",
      "resolved": "https://registry.npmjs.org/ssh2-streams/-/ssh2-streams-0.0.23.tgz",
      "integrity": "sha1-ru8wgxu1/Er2qj9tCiYaQTUxYSs=",
      "requires": {
        "asn1": "0.2.3",
        "readable-stream": "1.0.34",
        "streamsearch": "0.1.2"
      }
    },
    "sshpk": {
      "version": "1.13.1",
      "resolved": "https://registry.npmjs.org/sshpk/-/sshpk-1.13.1.tgz",
@@ -642,6 +765,16 @@
      "resolved": "https://registry.npmjs.org/statuses/-/statuses-1.3.1.tgz",
      "integrity": "sha1-+vUbnrdKrvOzrPStX2Gr8ky3uT4="
    },
    "streamsearch": {
      "version": "0.1.2",
      "resolved": "https://registry.npmjs.org/streamsearch/-/streamsearch-0.1.2.tgz",
      "integrity": "sha1-gIudDlb8Jz2Am6VzOOkpkZoanxo="
    },
    "string_decoder": {
      "version": "0.10.31",
      "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-0.10.31.tgz",
      "integrity": "sha1-YuIDvEF2bGwoyfyEMB2rHFMQ+pQ="
    },
    "stringstream": {
      "version": "0.0.5",
      "resolved": "https://registry.npmjs.org/stringstream/-/stringstream-0.0.5.tgz",
@@ -707,6 +840,11 @@
        "core-util-is": "1.0.2",
        "extsprintf": "1.3.0"
      }
    },
    "wrappy": {
      "version": "1.0.2",
      "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz",
      "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8="
    }
  }
}
+3 −2
Changes for package.json: 3 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -14,8 +14,9 @@
  "license": "ISC",
  "dependencies": {
    "directory-tree": "2.0.0",
    "express": "4.16.2",
    "request": "2.83.0"
    "express": "^4.16.2",
    "request": "2.83.0",
    "scp2": "^0.5.0"
  },
  "engines": {
    "node": "9.2.1"