diff --git a/Cabinet/api/module.php b/Cabinet/api/module.php index 193b9ac..0815a24 100644 --- a/Cabinet/api/module.php +++ b/Cabinet/api/module.php @@ -29,10 +29,15 @@ class Cabinet extends Module case 'createFolder': $this->createFolder(); break; + + case 'download': + $this->response = $this->download($this->request->filePath); + break; + } } - protected function getDirectoryContents() + private function getDirectoryContents() { $dir = $this->request->directory; @@ -43,7 +48,7 @@ class Cabinet extends Module $obj = array("name" => $file, "directory" => is_dir($dir . '/' . $file), "path" => realpath($dir . '/' . $file), "permissions" => substr(sprintf('%o', fileperms($dir . '/' . $file)), -4), - "size" => filesize($dir . '/' . $file)); + "size" => $this->readableFileSize($dir . '/' . $file)); array_push($contents, $obj); } $success = true; @@ -53,7 +58,7 @@ class Cabinet extends Module } - protected function getParentDirectory() + private function getParentDirectory() { $dir = $this->request->directory; $success = false; @@ -68,20 +73,13 @@ class Cabinet extends Module } - protected function deleteFile() + private function deleteFile() { $f = $this->request->file; $success = false; if (file_exists($f)) { - if (!is_dir($f)) { - unlink($f); - } else { - foreach (preg_grep('/^([^.])/', scandir($f)) as $file) { - unlink($f . '/' . $file); - } - rmdir($f); - } + exec("rm -rf " . escapeshellarg($f)); } if (!file_exists($f)) { @@ -92,7 +90,7 @@ class Cabinet extends Module } - protected function editFile() + private function editFile() { $f = $this->request->file; $data = $this->request->contents; @@ -106,22 +104,24 @@ class Cabinet extends Module $this->response = array("success" => $success); } - protected function getFileContents() + private function getFileContents() { $f = $this->request->file; $success = false; $content = ""; + $size = "0 Bytes"; if (file_exists($f)) { $success = true; $content = file_get_contents($f); + $size = $this->readableFileSize($f); } - $this->response = array("success" => $success, "content" => $content); + $this->response = array("success" => $success, "content" => $content, "size" => $size); } - protected function createFolder() + private function createFolder() { $dir = $this->request->directory; $name = $this->request->name; @@ -135,4 +135,39 @@ class Cabinet extends Module $this->response = array("success" => $success); } + /** + * Download a file + * @param: The path to the file to download + * @return array : array + */ + private function download($filePath) + { + if (file_exists($filePath)) { + return array("success" => true, "message" => null, "download" => $this->downloadFile($filePath)); + } else { + return array("success" => false, "message" => "File does not exist", "download" => null); + } + } + + /** + * Get the size of a file and add a unit to the end of it. + * @param $file: The file to get size of + * @return string: File size plus unit. Exp: 3.14M + */ + private function readableFileSize($file) { + $size = filesize($file); + + if ($size == null) + return "0 Bytes"; + + if ($size < 1024) { + return "{$size} Bytes"; + } else if ($size >= 1024 && $size < 1024*1024) { + return round($size / 1024, 2) . "K"; + } else if ($size >= 1024*1024) { + return round($size / (1024*1024), 2) . "M"; + } + return "{$size} Bytes"; + } + } \ No newline at end of file diff --git a/Cabinet/js/module.js b/Cabinet/js/module.js index 2528531..ed1e336 100644 --- a/Cabinet/js/module.js +++ b/Cabinet/js/module.js @@ -11,11 +11,11 @@ registerController("CabinetController", ['$api', '$scope', function($api, $scope $scope.showMessage = function(msgTitle, msgBody) { $scope.message = {title: msgTitle, body: msgBody}; $('#messageModal').modal("show"); - } + }; $scope.submitChangeDirectory = function(directory) { console.log(directory); - } + }; $scope.getDirectoryContents = function(dir) { $api.request({ @@ -38,7 +38,7 @@ registerController("CabinetController", ['$api', '$scope', function($api, $scope $scope.showMessage("Error Loading Directory", "There was an error loading directory contents. Please verify that the directory you are navigating to exists."); } }); - } + }; $scope.goToParentDirctory = function() { $api.request({ @@ -53,14 +53,14 @@ registerController("CabinetController", ['$api', '$scope', function($api, $scope $scope.showMessage("Error Finding Parent Directory", "An error occured while trying to find the parent directory. Please verify that the directory you are navigating to exists."); } }); - } + }; $scope.requestDeleteFile = function(file) { $scope.deleteFile.name = file.name; $scope.deleteFile.path = file.path; $scope.deleteFile.directory = file.directory; console.log($scope.deleteFile); - } + }; $scope.sendDeleteFile = function() { $api.request({ @@ -75,7 +75,7 @@ registerController("CabinetController", ['$api', '$scope', function($api, $scope $scope.showMessage("Error Deleting File", "An error occured while trying to delete the file " + $scope.deleteFile.path + ". Please verify that this file exists and you have permission to delete it."); } }); - } + }; $scope.requestEditFile = function(file) { $api.request({ @@ -84,12 +84,12 @@ registerController("CabinetController", ['$api', '$scope', function($api, $scope file: file.path }, function(response) { if (response.success == true) { - $scope.editFile = {name: file.name, path: file.path, content: response.content}; + $scope.editFile = {name: file.name, path: file.path, content: response.content, size: response.size}; } else { $scope.showMessage("Error Loading File Contents", "An error occured while trying to load the file " + file.name + ". Please verify that this file exists and you have permission to edit it."); } }); - } + }; $scope.sendEditFile = function() { $api.request({ @@ -105,7 +105,7 @@ registerController("CabinetController", ['$api', '$scope', function($api, $scope $scope.showMessage("Error Saving File", "An error occured while trying to save the file " + $scope.editFile.name + ". Please verify that this file exists and you have permission to edit it."); } }); - } + }; $scope.createFolder = function() { $api.request({ @@ -121,7 +121,21 @@ registerController("CabinetController", ['$api', '$scope', function($api, $scope $scope.showMessage("Error Creating Directory", "An error occured while trying to create the folder " + $scope.newFolder.name + ". Please verify that you have permission to create new items in this directory."); } }); - } + }; + + $scope.download = function(filePath) { + $api.request({ + module: "Cabinet", + action: "download", + filePath: filePath + }, function (response) { + if (!response.success) { + $scope.showMessage("Error", response.message); + return; + } + window.location = "/api/?download=" + response.download; + }) + }; $scope.getDirectoryContents($scope.currentDirectory); diff --git a/Cabinet/module.html b/Cabinet/module.html index 299d2ea..08bb12b 100644 --- a/Cabinet/module.html +++ b/Cabinet/module.html @@ -3,32 +3,34 @@