Skip to content
Snippets Groups Projects
Commit 622233c7 authored by nicolas.paschoud's avatar nicolas.paschoud
Browse files

Show infos of a File

parent ebd0e9c0
No related branches found
No related tags found
No related merge requests found
......@@ -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>
......@@ -25,3 +25,26 @@ function new_folder (folder_name, path) {
`;
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
......@@ -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>
......
......@@ -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
......@@ -6,6 +6,7 @@
button {
border: none;
padding: 5px;
}
button:hover {
......
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'],
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) => {
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}!`))
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment