From 9670833a5ddc3c24343202fda6f29875e9b3b10b Mon Sep 17 00:00:00 2001 From: Mario Date: Mon, 28 Dec 2020 10:12:25 +0000 Subject: files_ng: provide a fallback in case the server timed out on compressing the zip file --- Zotlabs/Module/Attach.php | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) (limited to 'Zotlabs/Module/Attach.php') diff --git a/Zotlabs/Module/Attach.php b/Zotlabs/Module/Attach.php index 0bf7689be..cf78ac690 100644 --- a/Zotlabs/Module/Attach.php +++ b/Zotlabs/Module/Attach.php @@ -14,6 +14,7 @@ class Attach extends Controller { $attach_ids = ((x($_REQUEST, 'attach_ids')) ? $_REQUEST['attach_ids'] : []); $attach_path = ((x($_REQUEST, 'attach_path')) ? $_REQUEST['attach_path'] : ''); + $download_token = ((x($_REQUEST, 'download_token')) ? $_REQUEST['download_token'] : ''); $channel_id = ((x($_REQUEST, 'channel_id')) ? intval($_REQUEST['channel_id']) : 0); $channel = channelx_by_n($channel_id); @@ -52,6 +53,9 @@ class Attach extends Controller { ]; Verify::create('zip_token', 0, $token, json_encode($meta)); + Verify::create('download_token', 0, $download_token, $token); + + json_return_and_die([ 'success' => true, @@ -69,12 +73,32 @@ class Attach extends Controller { return; } - if(argv(1) === 'download') { + $token = ((x($_REQUEST, 'token')) ? $_REQUEST['token'] : ''); + $download_token = ((x($_REQUEST, 'download_token')) ? $_REQUEST['download_token'] : ''); - $token = ((x($_REQUEST, 'token')) ? $_REQUEST['token'] : ''); + if(argv(1) === 'check') { + $meta = Verify::get_meta('download_token', 0, $download_token); + + if(! $meta) + killme(); + + json_return_and_die([ + 'success' => true, + 'token' => $meta + ]); + } + + if(argv(1) === 'download') { $meta = Verify::get_meta('zip_token', 0, $token); + + if(! $meta) + killme(); + $meta = json_decode($meta, true); + // make sure we remove the download_token in case we have not checked yet + Verify::get_meta('download_token', 0, $download_token); + header('Content-Type: application/zip'); header('Content-Disposition: attachment; filename="'. $meta['zip_filename'] . '"'); header('Content-Length: ' . filesize($meta['zip_path'])); @@ -82,7 +106,7 @@ class Attach extends Controller { $istream = fopen($meta['zip_path'], 'rb'); $ostream = fopen('php://output', 'wb'); if($istream && $ostream) { - pipe_streams($istream,$ostream); + pipe_streams($istream, $ostream); fclose($istream); fclose($ostream); } @@ -123,7 +147,7 @@ class Attach extends Controller { $istream = fopen('store/' . $c[0]['channel_address'] . '/' . $fname,'rb'); $ostream = fopen('php://output','wb'); if($istream && $ostream) { - pipe_streams($istream,$ostream); + pipe_streams($istream, $ostream); fclose($istream); fclose($ostream); } -- cgit v1.2.3 From 2c4fabba35267a590a86e5a173243dbf82be3606 Mon Sep 17 00:00:00 2001 From: Mario Date: Wed, 30 Dec 2020 14:12:08 +0000 Subject: store zip files without compression --- Zotlabs/Module/Attach.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'Zotlabs/Module/Attach.php') diff --git a/Zotlabs/Module/Attach.php b/Zotlabs/Module/Attach.php index cf78ac690..f70366448 100644 --- a/Zotlabs/Module/Attach.php +++ b/Zotlabs/Module/Attach.php @@ -55,8 +55,6 @@ class Attach extends Controller { Verify::create('zip_token', 0, $token, json_encode($meta)); Verify::create('download_token', 0, $download_token, $token); - - json_return_and_die([ 'success' => true, 'token' => $token @@ -196,6 +194,8 @@ class Attach extends Controller { else { $file_path = $r['data']['content']; $zip->addFile($file_path, $zip_path); + // compressing can be ressource intensive - just store the data + $zip->setCompressionName($zip_path, ZipArchive::CM_STORE); } } -- cgit v1.2.3 From 8db367c74353f2b86d0effeebfe84c7cc4f512ab Mon Sep 17 00:00:00 2001 From: Mario Date: Wed, 30 Dec 2020 14:30:39 +0000 Subject: remove fallback code - it will not be required if compression is dismissed --- Zotlabs/Module/Attach.php | 19 ------------------- 1 file changed, 19 deletions(-) (limited to 'Zotlabs/Module/Attach.php') diff --git a/Zotlabs/Module/Attach.php b/Zotlabs/Module/Attach.php index f70366448..172f6a4bc 100644 --- a/Zotlabs/Module/Attach.php +++ b/Zotlabs/Module/Attach.php @@ -14,8 +14,6 @@ class Attach extends Controller { $attach_ids = ((x($_REQUEST, 'attach_ids')) ? $_REQUEST['attach_ids'] : []); $attach_path = ((x($_REQUEST, 'attach_path')) ? $_REQUEST['attach_path'] : ''); - $download_token = ((x($_REQUEST, 'download_token')) ? $_REQUEST['download_token'] : ''); - $channel_id = ((x($_REQUEST, 'channel_id')) ? intval($_REQUEST['channel_id']) : 0); $channel = channelx_by_n($channel_id); @@ -53,7 +51,6 @@ class Attach extends Controller { ]; Verify::create('zip_token', 0, $token, json_encode($meta)); - Verify::create('download_token', 0, $download_token, $token); json_return_and_die([ 'success' => true, @@ -72,19 +69,6 @@ class Attach extends Controller { } $token = ((x($_REQUEST, 'token')) ? $_REQUEST['token'] : ''); - $download_token = ((x($_REQUEST, 'download_token')) ? $_REQUEST['download_token'] : ''); - - if(argv(1) === 'check') { - $meta = Verify::get_meta('download_token', 0, $download_token); - - if(! $meta) - killme(); - - json_return_and_die([ - 'success' => true, - 'token' => $meta - ]); - } if(argv(1) === 'download') { $meta = Verify::get_meta('zip_token', 0, $token); @@ -94,9 +78,6 @@ class Attach extends Controller { $meta = json_decode($meta, true); - // make sure we remove the download_token in case we have not checked yet - Verify::get_meta('download_token', 0, $download_token); - header('Content-Type: application/zip'); header('Content-Disposition: attachment; filename="'. $meta['zip_filename'] . '"'); header('Content-Length: ' . filesize($meta['zip_path'])); -- cgit v1.2.3 From dde0f3a4032d2a24f1afa6941452c7c4e933cda7 Mon Sep 17 00:00:00 2001 From: Mario Date: Fri, 15 Jan 2021 20:03:49 +0000 Subject: formatting --- Zotlabs/Module/Attach.php | 67 ++++++++++++++++++++++++----------------------- 1 file changed, 34 insertions(+), 33 deletions(-) (limited to 'Zotlabs/Module/Attach.php') diff --git a/Zotlabs/Module/Attach.php b/Zotlabs/Module/Attach.php index 172f6a4bc..5f5779b51 100644 --- a/Zotlabs/Module/Attach.php +++ b/Zotlabs/Module/Attach.php @@ -1,4 +1,5 @@ $zip_filename, - 'zip_path' => $zip_path + 'zip_path' => $zip_path ]; Verify::create('zip_token', 0, $token, json_encode($meta)); json_return_and_die([ 'success' => true, - 'token' => $token + 'token' => $token ]); } @@ -63,28 +64,28 @@ class Attach extends Controller { function get() { - if(argc() < 2) { - notice( t('Item not available.') . EOL); + if (argc() < 2) { + notice(t('Item not available.') . EOL); return; } $token = ((x($_REQUEST, 'token')) ? $_REQUEST['token'] : ''); - if(argv(1) === 'download') { + if (argv(1) === 'download') { $meta = Verify::get_meta('zip_token', 0, $token); - if(! $meta) + if (!$meta) killme(); $meta = json_decode($meta, true); header('Content-Type: application/zip'); - header('Content-Disposition: attachment; filename="'. $meta['zip_filename'] . '"'); + header('Content-Disposition: attachment; filename="' . $meta['zip_filename'] . '"'); header('Content-Length: ' . filesize($meta['zip_path'])); $istream = fopen($meta['zip_path'], 'rb'); $ostream = fopen('php://output', 'wb'); - if($istream && $ostream) { + if ($istream && $ostream) { pipe_streams($istream, $ostream); fclose($istream); fclose($ostream); @@ -94,10 +95,10 @@ class Attach extends Controller { killme(); } - $r = attach_by_hash(argv(1),get_observer_hash(),((argc() > 2) ? intval(argv(2)) : 0)); + $r = attach_by_hash(argv(1), get_observer_hash(), ((argc() > 2) ? intval(argv(2)) : 0)); - if(! $r['success']) { - notice( $r['message'] . EOL); + if (!$r['success']) { + notice($r['message'] . EOL); return; } @@ -105,27 +106,27 @@ class Attach extends Controller { intval($r['data']['uid']) ); - if(! $c) + if (!$c) return; - $unsafe_types = array('text/html','text/css','application/javascript'); + $unsafe_types = array('text/html', 'text/css', 'application/javascript'); - if(in_array($r['data']['filetype'],$unsafe_types) && (! channel_codeallowed($r['data']['uid']))) { - header('Content-Type: text/plain'); + if (in_array($r['data']['filetype'], $unsafe_types) && (!channel_codeallowed($r['data']['uid']))) { + header('Content-Type: text/plain'); } else { header('Content-Type: ' . $r['data']['filetype']); } header('Content-Disposition: attachment; filename="' . $r['data']['filename'] . '"'); - if(intval($r['data']['os_storage'])) { + if (intval($r['data']['os_storage'])) { $fname = $r['data']['content']; - if(strpos($fname,'store') !== false) - $istream = fopen($fname,'rb'); + if (strpos($fname, 'store') !== false) + $istream = fopen($fname, 'rb'); else - $istream = fopen('store/' . $c[0]['channel_address'] . '/' . $fname,'rb'); - $ostream = fopen('php://output','wb'); - if($istream && $ostream) { + $istream = fopen('store/' . $c[0]['channel_address'] . '/' . $fname, 'rb'); + $ostream = fopen('php://output', 'wb'); + if ($istream && $ostream) { pipe_streams($istream, $ostream); fclose($istream); fclose($ostream); @@ -140,14 +141,14 @@ class Attach extends Controller { public function zip_archive_handler($zip, $attach_ids, $attach_path, $pass = 1) { $observer_hash = get_observer_hash(); - $single = ((count($attach_ids) == 1) ? true : false); + $single = ((count($attach_ids) == 1) ? true : false); $download_name = 'download.zip'; - foreach($attach_ids as $attach_id) { + foreach ($attach_ids as $attach_id) { $r = attach_by_id($attach_id, $observer_hash); - if (! $r['success']) { + if (!$r['success']) { continue; } @@ -158,8 +159,8 @@ class Attach extends Controller { if ($attach_path) { $strip_str = $attach_path . '/'; - $count = strlen($strip_str); - $zip_path = substr($r['data']['display_path'], $count); + $count = strlen($strip_str); + $zip_path = substr($r['data']['display_path'], $count); } if ($r['data']['is_dir']) { -- cgit v1.2.3