From ebe0acbba2cc64deb280553045db1405568122c7 Mon Sep 17 00:00:00 2001 From: Nicolas Paschoud <nicolas.paschoud@etu.hesge.ch> Date: Wed, 18 Dec 2019 21:50:07 +0100 Subject: [PATCH] Show shared files --- projet/db/hyperdrive.sql | 9 ++---- projet/front/element-style.js | 6 ++-- projet/front/index.html | 6 ++++ projet/front/show-content.js | 34 +++++++++++--------- projet/front/styles/style.css | 12 +++++--- projet/hyperdrive-rest.js | 58 ++++++++++++++++++++++++----------- projet/sql-request.js | 45 ++++++++++++++++++++------- 7 files changed, 114 insertions(+), 56 deletions(-) diff --git a/projet/db/hyperdrive.sql b/projet/db/hyperdrive.sql index 6991f6c..b28469c 100644 --- a/projet/db/hyperdrive.sql +++ b/projet/db/hyperdrive.sql @@ -82,9 +82,6 @@ VALUES ("c", "e", "@dfsg"); -SELECT Paths.paths, login, parent, Files.file_id, Files.file_name -FROM Paths -LEFT JOIN Files ON Files.paths = Paths.paths -WHERE Paths.login='a' -AND Paths.paths='/a' -OR Paths.parent='/a'; \ No newline at end of file +SELECT pseudo_2, Files.file_id, Files.file_name +FROM Shares LEFT JOIN Files ON Shares.file_id=Files.file_id +WHERE pseudo_2='b'; \ No newline at end of file diff --git a/projet/front/element-style.js b/projet/front/element-style.js index f5b089e..08c942e 100644 --- a/projet/front/element-style.js +++ b/projet/front/element-style.js @@ -1,4 +1,4 @@ -function new_file(file_name, file_id) { +function new_file(file_name, file_id, document_id) { content = ` <div class="file" id="${file_id}"> <p> @@ -9,7 +9,7 @@ function new_file(file_name, file_id) { </p> </div> ` - document.getElementById("content-drive").innerHTML += content; + document.getElementById(document_id).innerHTML += content; } function new_folder (folder_name, path) { @@ -37,6 +37,7 @@ lng: 6.1459 login: "a" paths: "/a" */ + console.log(infos); el.innerHTML = ` <p>File name : ${infos.file_name}</p> <p>Path : ${infos.paths}</p> @@ -46,5 +47,6 @@ paths: "/a" lng : ${infos.lng} </p> <p>Adresse : Mettre adresse avec gmap ou autre</p> + <button onclick="download('${infos.file_id}')">Download</button> `; } \ No newline at end of file diff --git a/projet/front/index.html b/projet/front/index.html index 3843198..755d676 100644 --- a/projet/front/index.html +++ b/projet/front/index.html @@ -39,6 +39,12 @@ <h3>Infos</h3> <div id="info-content"> + </div> + </div> + <div id="shared"> + <h3>Shared with me</h3> + <div id="shared-content"> + </div> </div> </div> diff --git a/projet/front/show-content.js b/projet/front/show-content.js index c29889e..27fc2b1 100644 --- a/projet/front/show-content.js +++ b/projet/front/show-content.js @@ -14,10 +14,9 @@ function change_path(path) { p = p.join("/"); } appendLinkToParent(path, p); - console.log(result); for (let i in result){ if (result[i].file_id){ - new_file(result[i].file_name, result[i].file_id); + new_file(result[i].file_name, result[i].file_id, "content-drive"); } else { if (path !== result[i].paths){ let name = result[i].paths.split("/"); @@ -59,8 +58,7 @@ function login() { function disconnect(){ $.ajax({url: 'logout?token=' + token, success: function(result){ - my_login = username; - token = result.signedToken; + token = null; let el = document.getElementById("login-button"); el.innerHTML = "Login"; el.onclick = login; @@ -71,25 +69,29 @@ function disconnect(){ } function showSharedContent() { - console.log("shared content"); - let el = document.getElementById("shared"); - - el.style.display = 'block'; - + let el = document.getElementById("shared-content"); + $.ajax({url: '/show-shared-file?token=' + token, + success: function(result){ + console.log(result); + for (let i in result){ + new_file(result[i].file_name, result[i].file_id, "shared-content"); + } + } + }); } function newFolder() { let foldername = document.getElementById("foldername").value; let path = document.getElementById("path-dir-drive").textContent; - console.log('create-path' + path + "/" + foldername); - $.ajax({url: 'create-path' + path + "/" + foldername + '/' + token, success: function(result){ - change_path(path); - }}); + $.ajax({url: 'create-path' + path + "/" + foldername + '/' + token, + success: function(result){ + change_path(path); + } + }); } function newFile() { filename = document.getElementById("filename").value; - el = document.getElementById("share-section"); console.log(filename); } @@ -99,4 +101,8 @@ function getInfo(file_id){ show_info(result[0]); } }); +} + +function download(file_id){ + console.log("Download : " + file_id); } \ No newline at end of file diff --git a/projet/front/styles/style.css b/projet/front/styles/style.css index 60ff021..ee8ee4c 100644 --- a/projet/front/styles/style.css +++ b/projet/front/styles/style.css @@ -31,6 +31,12 @@ button:hover { float: left; } +#shared { + border: solid grey 2px; + width: 30%; + float: left; +} + .folder { float: left; } @@ -47,8 +53,4 @@ button:hover { .file p{ font-size: 10pt; text-align: center; -} - -/* #menu-more { - -} */ \ No newline at end of file +} \ No newline at end of file diff --git a/projet/hyperdrive-rest.js b/projet/hyperdrive-rest.js index 08464f7..db1154e 100644 --- a/projet/hyperdrive-rest.js +++ b/projet/hyperdrive-rest.js @@ -1,3 +1,4 @@ +"use strict"; const CryptoJS = require("crypto-js"); const http = require("http"); @@ -15,9 +16,9 @@ String.prototype.hashCode = function() { var hash = 0, i, chr; if (this.length === 0) return hash; for (i = 0; i < this.length; i++) { - chr = this.charCodeAt(i); - hash = ((hash << 5) - hash) + chr; - hash |= 0; // Convert to 32bit integer + chr = this.charCodeAt(i); + hash = ((hash << 5) - hash) + chr; + hash |= 0; // Convert to 32bit integer } return hash; }; @@ -49,8 +50,8 @@ function Signature(token){ function JWT(pl_user, pl_pass){ this.header = new Header("jwt", "HS512"); this.payload = new Payload(pl_user, pl_pass.hashCode()); - token = tob64(this.header) + "." + tob64(this.payload); - signature = new Signature("token"); + let token = tob64(this.header) + "." + tob64(this.payload); + let signature = new Signature("token"); this.signedToken = token + "." + signature; } @@ -64,7 +65,7 @@ function add_token(token, user) { break; } } - obj = {}; obj[token.toString()] = user; + let obj = {}; obj[token.toString()] = user; valid_tokens.push(obj); console.log(valid_tokens) @@ -133,7 +134,7 @@ function check_login(user, pass, userObject, res) { else{ if (userObject){ if (userObject.passwd == pass.hashCode()) { - jwt = new JWT(user, pass); + let jwt = new JWT(user, pass); res.send({ "route": "/login", @@ -166,7 +167,7 @@ function check_login(user, pass, userObject, res) { // resCode : [ 0: Token is valid, 1: Token is not valid, 3: Empty token ] app.get('/testmytoken', (req, res) => { - token = req.query['token']; + let token = req.query['token']; if (!token) { res.send({ @@ -193,7 +194,7 @@ app.get('/testmytoken', (req, res) => { // resCode : [ 0: Logout ok, 1: Already logged out, 3: Empty token ] app.get('/logout/', (req, res) => { - token = req.query['token']; + let token = req.query['token']; if (!token) { res.send({ @@ -240,13 +241,13 @@ app.get('/register/', (req, res) => { */ app.get('/share/:file_id/:to_user', (req, res) => { - token = req.query["token"] - user = verify_token(token); + let token = req.query["token"] + let user = verify_token(token); console.log("user : " + user) if (req.params['to_user'] && req.params['file_id']){ - to_user = req.params['to_user']; - file_id = req.params['file_id']; + let to_user = req.params['to_user']; + let file_id = req.params['file_id']; sql.addSharing(user, to_user, file_id).then(function (r) { res.send(r); }) @@ -262,7 +263,6 @@ app.get('/share/:file_id/:to_user', (req, res) => { app.get('/upload/', (req, res) => { let d = new Date(); let date_upload = d.getTime(); - filename = req.query[''] // "abcd", "un", "/a", 46.2054, 6.1459, date_upload http.get('http://bot.whatismyipaddress.com', (res) => { @@ -287,10 +287,10 @@ app.get('/upload/', (req, res) => { }); app.get('/download/:file_id', (req, res) => { - token = req.query["token"] - file_id = req.params["file_id"] + let token = req.query["token"] + let file_id = req.params["file_id"] - user = verify_token(token); + let user = verify_token(token); if (user){ sql.verifyFileID(user, file_id, (filename) => { console.log(filename) @@ -318,7 +318,7 @@ app.get('/change-path*', (req, res) => { let tok = req.params['0'].split("/").pop(); let name = verify_token(tok); if (name) { - content = sql.changeDirectory(name, path, + let content = sql.changeDirectory(name, path, (content) => { res.send(content); }); @@ -355,6 +355,7 @@ app.get('/get-info', (req, res) => { let token = req.query["token"]; let file_id = req.query["file_id"]; let name = verify_token(token); + console.log(token); if (name){ sql.getInfo(file_id, name, (resp, msg) => { res.send(resp); @@ -362,5 +363,26 @@ app.get('/get-info', (req, res) => { } }); +app.get('/show-shared-file', (req, res) => { + let token = req.query["token"]; + let name = verify_token(token); + if (name){ + sql.getSharedFile(name, (result) => { + if (result){ + let content = []; + for (let i in result){ + console.log(result[i]); + content.push({ + login: result[i].pseudo_2, + file_id: result[i].file_id, + file_name: result[i].file_name + }); + } + res.send(content); + } + }); + } +}); + app.use(express.static('front')); app.listen(port, () => console.log(`Hyperdrive listening on port ${port}!`)) diff --git a/projet/sql-request.js b/projet/sql-request.js index e67bfc4..06cb0aa 100644 --- a/projet/sql-request.js +++ b/projet/sql-request.js @@ -18,7 +18,10 @@ var con = mysql.createConnection({ function userExist(login, pass, callback){ const q = `SELECT login, passwd FROM Users WHERE login='${login}' AND passwd='${pass}';`; con.query(q, function (err, result) { - if (err) return false; + if (err) { + console.log(err); + return false; + } if (result.length > 0) { console.log("user exists"); return callback({ @@ -43,7 +46,10 @@ function insertUser(login, passwd, callback){ userExist(login, passwd, (userExist) => { if (!userExist){ con.query(q, (err, res) => { - if (err) return callback(false); + if (err) { + console.log(err); + return callback(false); + } if (!res) {console.log(res);return callback(false);} console.log("User", login, "inserted in the db"); return callback(true); @@ -187,7 +193,6 @@ async function verifyFileID(login, file_id, callback){ return callback(false); } - console.log(res) if (res.length > 0) filename = res[0].file_name; else @@ -204,27 +209,44 @@ function createPath(path, user, callback) { q = `INSERT INTO Paths VALUES ('${path}', '${user}', ${parent})`; con.query(q, (err, resp) => { - if (err) return callback(false, err); + if (err) { + console.log("Error while inserting Paths"); + return callback(false, err); + } return callback(true, resp); }); } /** - * - * @param {string} file_id - * @param {string} login + * Get information about a file + * @param {string} file_id Id of the file + * @param {string} login Owner of the file * @param {function} callback callback function */ function getInfo(file_id, login, callback){ - const q = `SELECT * FROM Files WHERE file_id='${file_id}' AND login='${login}';`; + const q = `SELECT * FROM Files WHERE file_id='${file_id}';`; con.query(q, (err, resp) => { if (err) return callback(false, err); - console.log(resp); - return callback(resp, resp); }); } +function getSharedFile(user, callback){ + const q = ` + SELECT pseudo_2, Files.file_id, Files.file_name + FROM Shares LEFT JOIN Files ON Shares.file_id=Files.file_id + WHERE pseudo_2='${user}';`; + + con.query(q, (err, resp) => { + if (err) { + console.log(err); + return callback(false, err); + } + console.log("Select ok"); + return callback(resp, resp); + }); +} + exports.userExist = userExist; exports.addUser = addUser; exports.addPath = addPath; @@ -232,4 +254,5 @@ exports.addSharing = addSharing; exports.changeDirectory = changeDirectory; exports.createPath = createPath; exports.verifyFileID = verifyFileID; -exports.getInfo = getInfo; \ No newline at end of file +exports.getInfo = getInfo; +exports.getSharedFile = getSharedFile; \ No newline at end of file -- GitLab