From fea0178c85cca6c95cdd8a92fe24c3fd81fec742 Mon Sep 17 00:00:00 2001 From: adam Date: Fri, 12 Feb 2021 20:47:42 +0200 Subject: checks if filename exists in the directory before creating the filenode --- php/database.php | 21 +++++++++++++++------ php/upload.php | 5 +++++ 2 files changed, 20 insertions(+), 6 deletions(-) (limited to 'php') diff --git a/php/database.php b/php/database.php index 789ea86..23ff602 100644 --- a/php/database.php +++ b/php/database.php @@ -135,12 +135,15 @@ require_once "node.php"; } /*if both are not null returns the id of the node with the name name in the directory_id node*/ - function get_node_id($name,$directory_id) + function get_node_id(string $name,int $directory_id) { - $statement=$this->pdo->prepare( - "select nl.node_id as id from node_links nl - inner join nodes n on n.node_id=nl.node_id - where name=:name and directory_id=:directory_id)"); + $statement=$this->pdo->prepare(" + select node_links.node_id as id + from node_links + inner join nodes on + nodes.node_id=node_links.node_id + where node_links.name=:name and node_links.directory_id=:directory_id + "); $statement->bindParam(':name',$name); $statement->bindParam(':directory_id',$directory_id); if($statement->execute()==false) @@ -149,7 +152,7 @@ require_once "node.php"; return NULL; } $hold=$statement->fetch(PDO::FETCH_ASSOC); - if($hold==false) + if(gettype($hold)!="array") { return NULL; }else @@ -344,6 +347,12 @@ require_once "node.php"; return "error"; } + /*check if node with given name exists*/ + if($this->get_node_id($filename,$dir_id)!=NULL) + { + error_log("filename taken"); + return "filename taken"; + } /*generate the node*/ $code=$this->get_random_node_name(""); if($filename==NULL)return "error"; diff --git a/php/upload.php b/php/upload.php index ee5d1ae..1672e9e 100644 --- a/php/upload.php +++ b/php/upload.php @@ -25,6 +25,11 @@ if($codename=="error") http_response_code(400); exit(0); } +if($codename=="filename taken") +{ + http_response_code(409); + exit(0); +} move_uploaded_file($file['tmp_name'], "$storage_root/$codename"); http_response_code(200); -- cgit v1.2.3