Skip to content
Snippets Groups Projects
Commit 5a62c5f3 authored by noe.fleury's avatar noe.fleury :computer:
Browse files

Added sharing part

parent 9ce35bd6
No related branches found
No related tags found
No related merge requests found
......@@ -47,7 +47,8 @@ VALUES
("b", "test"),
("c", "test"),
("d", "test"),
("e", "test");
("e", "test"),
("noe", "prov");
INSERT INTO Paths
VALUES
......@@ -57,7 +58,8 @@ VALUES
("/c", "c", NULL),
("/c/test", "c", "/c"),
("/d", "d", NULL),
("/e", "e", NULL);
("/e", "e", NULL),
("/n", "noe", NULL);
INSERT INTO Files
VALUES
......@@ -66,7 +68,8 @@ VALUES
("@dfsg", "trois", "/c/test", 46.2054, 6.1459),
("gbvaf", "quatre", "/b", 46.2054, 6.1459),
("dsfgh", "cinq", "/d", 46.2054, 6.1459),
("sdfa", "six", "/e", 46.2054, 6.1459);
("sdfa", "six", "/e", 46.2054, 6.1459),
("liblb", "myfile", "/n", 46.2054, 6.1459);
INSERT INTO Shares
VALUES
......
......@@ -54,31 +54,47 @@ function JWT(pl_user, pl_pass){
}
function add_token(token) {
function add_token(token, user) {
if (valid_tokens.indexOf(token) === -1) {
valid_tokens.push(token.toString());
for (let i = 0; i < valid_tokens.length; i++) {
if(Object.keys(valid_tokens[i])[0] == token){
valid_tokens.splice(i, 1);
console.log("Unable to add token. (token already present)");
break;
}
else {
console.log("Unable to add token to valid_tokens. (token already present)");
}
obj = {}; obj[token.toString()] = user;
valid_tokens.push(obj);
console.log(valid_tokens)
console.log("Successfully added token.");
}
function remove_token(token) {
if (valid_tokens.indexOf(token) != -1) {
var index = valid_tokens.indexOf(token);
valid_tokens.splice(index, 1);
for (let i = 0; i < valid_tokens.length; i++) {
if(Object.keys(valid_tokens[i])[0] == token){
valid_tokens.splice(i, 1);
console.log("Successfully removed token from valid_tokens.");
return true;
}
else {
console.log("Unable to remove token from valid_tokens. (Token not present)");
}
console.log("Unable to remove token from valid_tokens. (Token not present)");
}
// verify the token
// return : user if exist
function verify_token(token) {
return (valid_tokens.indexOf(token) !== -1);
for (let i = 0; i < valid_tokens.length; i++) {
if(Object.keys(valid_tokens[i])[0] == token)
return valid_tokens[i][token];
}
return false;
}
......@@ -136,7 +152,7 @@ app.get('/login', (req, res) => {
"comment": `Password for user '${ user }' true.`
})
add_token(jwt.signedToken);
add_token(jwt.signedToken, user);
}
else {
......@@ -236,8 +252,27 @@ app.get('/register/', (req, res) => {
* param : user2
* user1 share a file_id with user2
*/
app.get('/share/:file_id', (req, res) => {
res.send(`Request for a file sharing (id: ${req.params['file_id']})`)
app.get('/share/:file_id/:to_user', (req, res) => {
token = req.query["token"]
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'];
sql.addSharing(user, to_user, file_id).then(function (r) {
res.send(r);
})
}
else{
res.send("Unable to share. Please provide a user to share with and a file_id.");
}
})
/**
......
......@@ -124,6 +124,53 @@ function changePath(login, path){
];
}
// verify if a file_id is at a user
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}';`
con.query(q, function(err, res) {
if (err) {
console.log("Error while veryfing file_id for user");
console.log(err);
resolve(false);
}
console.log(res.length)
// return new Promise(resolve => resolve("resolved"));
if (res.length > 0){
if (res[0].login == login)
resolve(true);
resolve(false);
}
resolve(false);
});
});
}
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}');`;
con.query(q, function(err, res) {
if (err) {
console.log("Error while adding a share path");
console.log(err);
return "Error sharing - (DB error)";
}
});
// return "New sharing (", login, " --> " + to_user + "( - file_id : " + file_id + ")) added succesfully !";
return "Sharing ok."
}
else{
return "Unable to share, this is not your file.";
}
}
exports.userExist = userExist;
exports.addUser = addUser;
exports.addPath = addPath;
exports.addSharing = addSharing;
\ 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