Skip to content

Commit 70313cd

Browse files
authored
Merge pull request joni2back#261 from durasj/php-local-bridge-zip
Php local bridge zip
2 parents 41fd049 + 76ce4ac commit 70313cd

File tree

3 files changed

+45
-13
lines changed

3 files changed

+45
-13
lines changed

bridges/php-local/LocalBridge/FileManagerApi.php

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ public function postHandler($query, $request, $files)
174174
public function getHandler($queries)
175175
{
176176
$t = $this->translate;
177-
177+
178178
switch ($queries['action']) {
179179
case 'download':
180180
$downloaded = $this->downloadAction($queries['path']);
@@ -185,7 +185,17 @@ public function getHandler($queries)
185185
}
186186

187187
break;
188-
188+
189+
case 'downloadMultiple':
190+
$downloaded = $this->downloadMultipleAction($queries['items'], $queries['toFilename']);
191+
if ($downloaded === true) {
192+
exit;
193+
} else {
194+
$response = $this->simpleErrorResponse($t->file_not_found);
195+
}
196+
197+
break;
198+
189199
default:
190200
$response = $this->simpleErrorResponse($t->function_not_implemented);
191201
break;
@@ -221,6 +231,34 @@ private function downloadAction($path)
221231
return true;
222232
}
223233

234+
private function downloadMultipleAction($items, $archiveName)
235+
{
236+
$archivePath = tempnam('../', 'archive');
237+
238+
$zip = new \ZipArchive();
239+
if ($zip->open($archivePath, \ZipArchive::CREATE) !== true) {
240+
unlink($archivePath);
241+
return false;
242+
}
243+
244+
foreach ($items as $path) {
245+
$zip->addFile($this->basePath . $path, basename($path));
246+
}
247+
248+
$zip->close();
249+
250+
header("Content-Disposition: attachment; filename=\"$archiveName\"");
251+
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
252+
header("Content-Type: application/zip");
253+
header('Pragma: public');
254+
header('Content-Length: ' . filesize($archivePath));
255+
readfile($archivePath);
256+
257+
unlink($archivePath);
258+
259+
return true;
260+
}
261+
224262
private function uploadAction($path, $files)
225263
{
226264
$path = $this->basePath . $path;
@@ -390,8 +428,8 @@ private function compressAction($paths, $destination, $archiveName)
390428
{
391429
$archivePath = $this->basePath . $destination . $archiveName;
392430

393-
$zip = new ZipArchive();
394-
if ($zip->open($archivePath, ZipArchive::CREATE) !== true) {
431+
$zip = new \ZipArchive();
432+
if ($zip->open($archivePath, \ZipArchive::CREATE) !== true) {
395433
return false;
396434
}
397435

@@ -407,7 +445,7 @@ private function extractAction($destination, $archivePath, $folderName)
407445
$archivePath = $this->basePath . $archivePath;
408446
$folderPath = $this->basePath . rtrim($destination, '/') . '/' . $folderName;
409447

410-
$zip = new ZipArchive;
448+
$zip = new \ZipArchive;
411449
if ($zip->open($archivePath) === false) {
412450
return 'unsupported';
413451
}

bridges/php-local/LocalBridge/Rest.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -158,18 +158,12 @@ private function parseBody()
158158
$files = $_FILES;
159159
}
160160

161-
//In case of PUT request or request with json body
161+
//In case of request with json body
162162
if ($data === null) {
163163
if (isset($_SERVER["CONTENT_TYPE"]) && strpos($_SERVER["CONTENT_TYPE"], 'application/json') !== false) {
164164
$input = file_get_contents('php://input');
165165

166166
$data = json_decode($input, true);
167-
} else {
168-
$stream = [];
169-
new stream($stream);
170-
171-
$data = $stream['post'];
172-
$files = $stream['file'];
173167
}
174168
}
175169

bridges/php-local/index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* PHP Local filesystem bridge for angular-filemanager
66
*
77
* @author Jakub Ďuraš <[email protected]>
8-
* @version 0.1.0
8+
* @version 0.2.0
99
*/
1010
include 'LocalBridge/Response.php';
1111
include 'LocalBridge/Rest.php';

0 commit comments

Comments
 (0)