Skip to content

Commit

Permalink
Merge pull request #9 from DreamlandOwO/master
Browse files Browse the repository at this point in the history
Use POST http verbs on save related endpoints
  • Loading branch information
n1crack authored Jan 30, 2024
2 parents 051d5e8 + b303f0e commit 922fcf2
Showing 1 changed file with 41 additions and 26 deletions.
67 changes: 41 additions & 26 deletions src/VueFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,32 @@ public function init($config): void
$query = $this->request->get('q');

$route_array = [
'index', 'newfolder', 'newfile', 'download', 'rename', 'move', 'delete', 'upload', 'archive',
'unarchive', 'preview', 'save', 'search',
'index' => 'get',
'download' => 'get',
'preview' => 'get',
'search' => 'get',
'newfolder' => 'post',
'newfile' => 'post',
'rename' => 'post',
'move' => 'post',
'delete' => 'post',
'upload' => 'post',
'archive' => 'post',
'unarchive' => 'post',
'save' => 'post',
];

if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
$response = new JsonResponse();
$response->headers->set('Access-Control-Allow-Origin', "*");
$response->headers->set('Access-Control-Allow-Headers', "*");
$response->send();
return;
}

try {
if (!in_array($query, $route_array, true)) {
if (!array_key_exists($query, $route_array)
|| $route_array[$query] !== strtolower($this->request->getMethod())) {
throw new Exception('The query does not have a valid method.');
}

Expand Down Expand Up @@ -209,7 +222,7 @@ public function search()
public function newfolder()
{
$path = $this->request->get('path');
$name = $this->request->get('name');
$name = $this->request->getPayload()->get('name');

if (!$name || !strpbrk($name, "\\/?%*:|\"<>") === false) {
throw new Exception('Invalid folder name.');
Expand All @@ -232,7 +245,7 @@ public function newfolder()
public function newfile()
{
$path = $this->request->get('path');
$name = $this->request->get('name');
$name = $this->request->getPayload()->get('name');

if (!$name || !strpbrk($name, "\\/?%*:|\"<>") === false) {
throw new Exception('Invalid file name.');
Expand All @@ -255,8 +268,8 @@ public function newfile()
*/
public function upload()
{
$name = $this->request->get('name');
$path = $this->request->get('path');
$name = $this->request->getPayload()->get('name');

$file = $this->request->files->get('file');
$stream = fopen($file->getRealPath(), 'r+');
Expand All @@ -281,7 +294,7 @@ public function preview()
public function save()
{
$path = $this->request->get('path');
$content = $this->request->get('content');
$content = $this->request->getPayload()->get('content');

$this->manager->write($path, $content);

Expand Down Expand Up @@ -318,8 +331,9 @@ public function download()
*/
public function rename()
{
$name = $this->request->get('name');
$from = $this->request->get('item');
$payload = $this->request->getPayload();
$name = $payload->get('name');
$from = $payload->get('item');
$path = $this->request->get('path');
$to = $path.DIRECTORY_SEPARATOR.$name;

Expand All @@ -334,21 +348,21 @@ public function rename()

public function move()
{
$to = $this->request->get('item');

$items = json_decode($this->request->get('items'));
$payload = $this->request->getPayload();
$to = $payload->get('item');
$items = $payload->all('items');

foreach ($items as $item) {
$target = $to.DIRECTORY_SEPARATOR.basename($item->path);
$target = $to.DIRECTORY_SEPARATOR.basename($item['path']);
if ($this->manager->fileExists($target) || $this->manager->directoryExists($target)) {
throw new Exception('One of the files is already exists.');
}
}

foreach ($items as $item) {
$target = $to.DIRECTORY_SEPARATOR.basename($item->path);
$target = $to.DIRECTORY_SEPARATOR.basename($item['path']);

$this->manager->move($item->path, $target);
$this->manager->move($item['path'], $target);
}

return $this->index();
Expand All @@ -360,13 +374,13 @@ public function move()
*/
public function delete()
{
$items = json_decode($this->request->get('items'));
$items = $this->request->getPayload()->all("items");

foreach ($items as $item) {
if ($item->type == 'dir') {
$this->manager->deleteDirectory($item->path);
if ($item['type'] == 'dir') {
$this->manager->deleteDirectory($item['path']);
} else {
$this->manager->delete($item->path);
$this->manager->delete($item['path']);
}
}

Expand All @@ -380,12 +394,13 @@ public function delete()
*/
public function archive()
{
$name = pathinfo($this->request->get('name'), PATHINFO_FILENAME);
$payload = $this->request->getPayload();
$name = pathinfo($payload->get('name'), PATHINFO_FILENAME);
if (!$name || !strpbrk($name, "\\/?%*:|\"<>") === false) {
throw new Exception('Invalid file name.');
}

$items = json_decode($this->request->get('items'), false, 512, JSON_THROW_ON_ERROR);
$items = $payload->all('items');
$name .= '.zip';
$path = $this->request->get('path').DIRECTORY_SEPARATOR.$name;
$zipFile = tempnam(sys_get_temp_dir(), $name);
Expand All @@ -403,17 +418,17 @@ public function archive()
);

foreach ($items as $item) {
if ($item->type == 'dir') {
$dirFiles = $this->manager->listContents($item->path, true)
if ($item['type'] == 'dir') {
$dirFiles = $this->manager->listContents($item['path'], true)
->filter(fn(StorageAttributes $attributes) => $attributes->isFile())
->toArray();
foreach ($dirFiles as $dirFile) {
$file = $this->manager->readStream($dirFile->path());
$zipStorage->writeStream(str_replace($this->request->get('path'), '', $dirFile->path()), $file);
}
} else {
$file = $this->manager->readStream($item->path);
$zipStorage->writeStream(str_replace($this->request->get('path'), '', $item->path), $file);
$file = $this->manager->readStream($item['path']);
$zipStorage->writeStream(str_replace($this->request->get('path'), '', $item['path']), $file);
}
}

Expand All @@ -433,7 +448,7 @@ public function archive()
*/
public function unarchive()
{
$zipItem = $this->request->get('item');
$zipItem = $this->request->getPayload()->get('item');

$zipStream = $this->manager->readStream($zipItem);

Expand Down

0 comments on commit 922fcf2

Please sign in to comment.