diff --git a/projet/front/element-style.js b/projet/front/element-style.js index e61144cb28e5056ef6d00b6d5a21b55d99a81399..f5b089ea238aada16dc2f71d4938978ddc7179c9 100644 --- a/projet/front/element-style.js +++ b/projet/front/element-style.js @@ -2,7 +2,7 @@ function new_file(file_name, file_id) { content = ` <div class="file" id="${file_id}"> <p> - <button> + <button onclick="getInfo('${file_id}')"> <img src="/images/file_img.png" height="50px"> </button> <p>${file_name}</p> @@ -24,4 +24,27 @@ function new_folder (folder_name, path) { </div> `; document.getElementById("content-drive").innerHTML += content; +} + +function show_info(infos){ + let el = document.getElementById("info-content"); +/* +addedon: 1576691707 +file_id: "abcd" +file_name: "un" +lat: 46.2054 +lng: 6.1459 +login: "a" +paths: "/a" +*/ + el.innerHTML = ` + <p>File name : ${infos.file_name}</p> + <p>Path : ${infos.paths}</p> + <p>Owner : ${infos.login}</p> + <p>Localisation :<br> + lat : ${infos.lat}<br> + lng : ${infos.lng} + </p> + <p>Adresse : Mettre adresse avec gmap ou autre</p> + `; } \ No newline at end of file diff --git a/projet/front/index.html b/projet/front/index.html index 402b2f7b267e94a8a5d657d1e8a1af1e24400329..38431980c959ef2e43f81057b7516d4da44bb747 100644 --- a/projet/front/index.html +++ b/projet/front/index.html @@ -37,7 +37,9 @@ </div> <div id="info"> <h3>Infos</h3> - <button><img src="/images/file_img.png" height="50px"></button> + <div id="info-content"> + + </div> </div> </div> </body> diff --git a/projet/front/show-content.js b/projet/front/show-content.js index f24b312cc33645805dd16c30779a87ef8d765a09..c29889e63411ba343f516605318c214bca4badec 100644 --- a/projet/front/show-content.js +++ b/projet/front/show-content.js @@ -5,8 +5,16 @@ let token = null; function change_path(path) { document.getElementById("content-drive").innerHTML = ""; - $.ajax({url: 'change-path'+path, success: function(result){ - appendLinkToParent(path, result[0].parent); + $.ajax({url: 'change-path'+path+"/"+token, success: function(result){ + let p = path.split("/"); + if (p.length === 2){ + p = null; + } else { + p.pop() + 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); @@ -63,6 +71,10 @@ function disconnect(){ } function showSharedContent() { + console.log("shared content"); + let el = document.getElementById("shared"); + + el.style.display = 'block'; } @@ -71,11 +83,20 @@ function newFolder() { let path = document.getElementById("path-dir-drive").textContent; console.log('create-path' + path + "/" + foldername); $.ajax({url: 'create-path' + path + "/" + foldername + '/' + token, success: function(result){ - console.log(result); + change_path(path); }}); } function newFile() { filename = document.getElementById("filename").value; + el = document.getElementById("share-section"); console.log(filename); +} + +function getInfo(file_id){ + $.ajax({url: 'get-info?file_id=' + file_id + '&token=' + token, + success: function(result){ + show_info(result[0]); + } + }); } \ No newline at end of file diff --git a/projet/front/styles/style.css b/projet/front/styles/style.css index 9619a7b8586f34455881224bc6b26794cd0eb09a..60ff021fffbca5179b070f0ff839080cec6de196 100644 --- a/projet/front/styles/style.css +++ b/projet/front/styles/style.css @@ -6,6 +6,7 @@ button { border: none; + padding: 5px; } button:hover { diff --git a/projet/hyperdrive-rest.js b/projet/hyperdrive-rest.js index 9b80930bd5d13afc650d6e911426a0def3b19546..08464f78bbe9c3b82ec57023657fac9f70c32107 100644 --- a/projet/hyperdrive-rest.js +++ b/projet/hyperdrive-rest.js @@ -1,8 +1,9 @@ -const CryptoJS = require("crypto-js") +const CryptoJS = require("crypto-js"); +const http = require("http"); -const express = require('express') -const app = express() -const port = 8080 +const express = require('express'); +const app = express(); +const port = 8080; const sql = require('./sql-request'); @@ -259,12 +260,33 @@ app.get('/share/:file_id/:to_user', (req, res) => { * Request to upload a file */ 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) => { + res.setEncoding('utf8'); + res.on('data', function(chunk){ + console.log(chunk); + let ip = chunk; + http.get('http://ip-api.com/json/' + ip, (resp) => { + let data = ''; + resp.on('data', (chunk) => { + data += chunk; + }); + + resp.on('end', () => { + console.log(JSON.parse(data)); + }); + }); + }); + }); + res.send(`Request for an upload (${req.query['file']})`) -}) +}); app.get('/download/:file_id', (req, res) => { - // res.send(`Request for a download (${req.params['file_id']})`) - token = req.query["token"] file_id = req.params["file_id"] @@ -283,8 +305,6 @@ app.get('/download/:file_id', (req, res) => { else{ res.send("Can't download (please be connected).") } - - }) /** @@ -292,11 +312,17 @@ app.get('/download/:file_id', (req, res) => { * param */ app.get('/change-path*', (req, res) => { - - content = sql.changeDirectory('a', req.params['0'], - (content) => { - res.send(content); - }); + let path = req.params['0'].split("/"); + path.pop(); + path = path.join("/"); + let tok = req.params['0'].split("/").pop(); + let name = verify_token(tok); + if (name) { + content = sql.changeDirectory(name, path, + (content) => { + res.send(content); + }); + } }) app.get('/create-path*', (req, res) => { @@ -307,12 +333,34 @@ app.get('/create-path*', (req, res) => { path.pop(); path = path.join("/"); - let name = verify_token('token'); - sql.createPath(path, name, (resp, msg) => { - console.log(resp, msg); - res.send(resp + " : " + msg); - }); + let parent = path.split("/"); + parent.pop(); + parent = parent.join("/"); + + let name = verify_token(tok); + if (name) { + sql.addPath(path, name, parent, (resp, msg) => { + console.log(resp, msg); + res.send(resp + " : " + msg); + }); + } }) +/** + * Get info of a file + * @param {string} file_id Id of the file we want the infos + * @param {string} token Token of the client + */ +app.get('/get-info', (req, res) => { + let token = req.query["token"]; + let file_id = req.query["file_id"]; + let name = verify_token(token); + if (name){ + sql.getInfo(file_id, name, (resp, msg) => { + res.send(resp); + }); + } +}); + 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 f09db8b4733c2f5c54618f4236a4cd4acc00c950..e67bfc49461bf496340c8efe3b76014ddee271d3 100644 --- a/projet/sql-request.js +++ b/projet/sql-request.js @@ -13,6 +13,7 @@ var con = mysql.createConnection({ * This function check if a user exist in the DB * @param {string} login This is the login of the user * @param {string} pass This is the password of the user + * @param {function} callback Callback function */ function userExist(login, pass, callback){ const q = `SELECT login, passwd FROM Users WHERE login='${login}' AND passwd='${pass}';`; @@ -35,9 +36,10 @@ function userExist(login, pass, callback){ * This function insert a new user in the DB * @param {string} login This is the login of the user * @param {string} pass This is the password of the user + * @param {function} callback Callback function */ function insertUser(login, passwd, callback){ - let q = `INSERT INTO Users VALUES ('${login}', '${passwd}');`; + const q = `INSERT INTO Users VALUES ('${login}', '${passwd}');`; userExist(login, passwd, (userExist) => { if (!userExist){ con.query(q, (err, res) => { @@ -55,10 +57,11 @@ function insertUser(login, passwd, callback){ * @param {string} path Path to add * @param {string} login User's path * @param {string} parent Parent of the new folder + * @param {function} callback Callback function */ function addPath(path, login, parent, callback){ if (parent) parent = "'" + parent + "'"; - let q = `INSERT INTO Paths VALUES ('${path}', '${login}', ${parent});`; + const q = `INSERT INTO Paths VALUES ('${path}', '${login}', ${parent});`; con.query(q, function(err, res) { if (err) { console.log("Error while adding a new path"); @@ -95,8 +98,9 @@ function addUser(login, passwd) { * @param {string} callback callback function */ async function changeDirectory(login, path, callback){ - q = ` - SELECT Paths.paths, login, parent, Files.file_id, Files.file_name + console.log(login); + const q = ` + SELECT Paths.paths, Paths.login, parent, Files.file_id, Files.file_name FROM Paths LEFT JOIN Files ON Files.paths = Paths.paths WHERE Paths.login='${login}' @@ -128,7 +132,7 @@ async function changeDirectory(login, path, callback){ function verify_user_file_id(file_id, login){ return new Promise(resolve => { - let q = `SELECT (login) FROM Files as F LEFT JOIN Paths as P ON F.paths = P.paths WHERE F.file_id = '${file_id}';` + const q = `SELECT (login) FROM Files as F LEFT JOIN Paths as P ON F.paths = P.paths WHERE F.file_id = '${file_id}';` con.query(q, function(err, res) { if (err) { console.log("Error while veryfing file_id for user"); @@ -147,12 +151,18 @@ function verify_user_file_id(file_id, login){ }); } +/** + * Share a file with a user + * @param {string} login Login of the user who wants to share a file + * @param {string} to_user Login of the user who receive file to share + * @param {string} file_id Id of the file to share with to_user + */ async function addSharing(login, to_user, file_id){ verif = await verify_user_file_id(file_id, login); if (verif){ - let q = `INSERT INTO Shares VALUES ('${login}', '${to_user}', '${file_id}');`; + const q = `INSERT INTO Shares VALUES ('${login}', '${to_user}', '${file_id}');`; con.query(q, function(err, res) { if (err) { console.log("Error while adding a share path"); @@ -169,7 +179,7 @@ async function addSharing(login, to_user, file_id){ } async function verifyFileID(login, file_id, callback){ - let q = `SELECT (file_name) FROM Files as F LEFT JOIN Paths as P ON F.paths = P.paths WHERE F.file_id = '${file_id}' AND P.login = '${login}';` + const q = `SELECT (file_name) FROM Files as F LEFT JOIN Paths as P ON F.paths = P.paths WHERE F.file_id = '${file_id}' AND P.login = '${login}';` con.query(q, (err, res) => { if (err) { @@ -199,6 +209,22 @@ function createPath(path, user, callback) { }); } +/** + * + * @param {string} file_id + * @param {string} login + * @param {function} callback callback function + */ +function getInfo(file_id, login, callback){ + const q = `SELECT * FROM Files WHERE file_id='${file_id}' AND login='${login}';`; + con.query(q, (err, resp) => { + if (err) return callback(false, err); + console.log(resp); + + return callback(resp, resp); + }); +} + exports.userExist = userExist; exports.addUser = addUser; exports.addPath = addPath; @@ -206,3 +232,4 @@ exports.addSharing = addSharing; exports.changeDirectory = changeDirectory; exports.createPath = createPath; exports.verifyFileID = verifyFileID; +exports.getInfo = getInfo; \ No newline at end of file