| @@ -2,15 +2,13 @@ | ||
| 2 | 2 | |
| 3 | 3 | namespace WPIDE\App\Controllers\FileManager; |
| 4 | 4 | |
| 5 | 5 | use WPIDE\App\Config\Config; |
| 6 | -use WPIDE\App\Helpers\ImageStateData; | |
| 7 | 6 | use WPIDE\App\Kernel\Request; |
| 8 | 7 | use WPIDE\App\Kernel\Response; |
| 9 | 8 | use WPIDE\App\Kernel\StreamedResponse; |
| 10 | 9 | use WPIDE\App\Services\Archiver\ArchiverInterface; |
| 11 | 10 | use WPIDE\App\Services\Auth\AuthInterface; |
| 12 | -use WPIDE\App\Services\Session\SessionStorageInterface as Session; | |
| 13 | 11 | use WPIDE\App\Services\Storage\Filesystem; |
| 14 | 12 | use WPIDE\App\Services\Tmpfs\TmpfsInterface; |
| 15 | 13 | use Symfony\Component\HttpFoundation\HeaderUtils; |
| 16 | 14 | use Symfony\Component\Mime\MimeTypes; |
| @@ -30,13 +28,8 @@ | ||
| 30 | 28 | */ |
| 31 | 29 | protected $user; |
| 32 | 30 | |
| 33 | 31 | /** |
| 34 | - * @var Session | |
| 35 | - */ | |
| 36 | - protected $session; | |
| 37 | - | |
| 38 | - /** | |
| 39 | 32 | * @var Config |
| 40 | 33 | */ |
| 41 | 34 | protected $config; |
| 42 | 35 | |
| @@ -45,16 +38,19 @@ | ||
| 45 | 38 | */ |
| 46 | 39 | protected $storage; |
| 47 | 40 | |
| 48 | 41 | /** |
| 42 | + * @var | |
| 43 | + */ | |
| 44 | + protected $separator; | |
| 45 | + | |
| 46 | + /** | |
| 49 | 47 | * @param Config $config |
| 50 | - * @param Session $session | |
| 51 | 48 | * @param AuthInterface $auth |
| 52 | 49 | * @param Filesystem $storage |
| 53 | 50 | */ |
| 54 | - public function __construct(Config $config, Session $session, AuthInterface $auth, Filesystem $storage) | |
| 51 | + public function __construct(Config $config, AuthInterface $auth, Filesystem $storage) | |
| 55 | 52 | { |
| 56 | - $this->session = $session; | |
| 57 | 53 | $this->config = $config; |
| 58 | 54 | $this->auth = $auth; |
| 59 | 55 | |
| 60 | 56 | $this->user = $this->auth->user() ?: $this->auth->getGuest(); |
| @@ -60,8 +56,10 @@ | ||
| 60 | 56 | $this->user = $this->auth->user() ?: $this->auth->getGuest(); |
| 61 | 57 | |
| 62 | 58 | $this->storage = $storage; |
| 63 | 59 | $this->storage->setPathPrefix($this->user->getHomeDir()); |
| 60 | + | |
| 61 | + $this->separator = $this->storage->getSeparator(); | |
| 64 | 62 | } |
| 65 | 63 | |
| 66 | 64 | /** |
| 67 | 65 | * @param Request $request |
| @@ -83,9 +81,9 @@ | ||
| 83 | 81 | } catch (\Exception $e) { |
| 84 | 82 | return $response->redirect('/'); |
| 85 | 83 | } |
| 86 | 84 | |
| 87 | - $this->downloadFile($file, $request, $streamedResponse); | |
| 85 | + $this->downloadFile($file, $request, $response, $streamedResponse); | |
| 88 | 86 | } |
| 89 | 87 | |
| 90 | 88 | /** |
| 91 | 89 | * @param $file array |
| @@ -92,19 +90,24 @@ | ||
| 92 | 90 | * @param Request $request |
| 93 | 91 | * @param StreamedResponse $streamedResponse |
| 94 | 92 | * @return void |
| 95 | 93 | */ |
| 96 | - public function downloadFile(array $file, Request $request, StreamedResponse $streamedResponse) | |
| 94 | + public function downloadFile(array $file, Request $request, Response $response, StreamedResponse $streamedResponse) | |
| 97 | 95 | { |
| 98 | 96 | |
| 97 | + if($file['stream'] === false) { | |
| 98 | + return $response->json('Cannot read file, please check file permissions!', 422); | |
| 99 | + } | |
| 100 | + | |
| 99 | 101 | $streamedResponse->setCallback(function () use ($file) { |
| 100 | 102 | // @codeCoverageIgnoreStart |
| 101 | - set_time_limit(0); | |
| 103 | + @set_time_limit(0); | |
| 104 | + | |
| 102 | 105 | if ($file['stream']) { |
| 103 | 106 | while (! feof($file['stream'])) { |
| 104 | 107 | echo fread($file['stream'], 1024 * 8); |
| 105 | - ob_flush(); | |
| 106 | - flush(); | |
| 108 | + @ob_flush(); | |
| 109 | + @flush(); | |
| 107 | 110 | } |
| 108 | 111 | fclose($file['stream']); |
| 109 | 112 | } |
| 110 | 113 | // @codeCoverageIgnoreEnd |
| @@ -152,11 +155,8 @@ | ||
| 152 | 155 | ); |
| 153 | 156 | } |
| 154 | 157 | // @codeCoverageIgnoreEnd |
| 155 | 158 | |
| 156 | - // close session, so we can continue streaming, note: dev is single-threaded | |
| 157 | - $this->session->save(); | |
| 158 | - | |
| 159 | 159 | $streamedResponse->send(); |
| 160 | 160 | } |
| 161 | 161 | |
| 162 | 162 | /** |
| @@ -180,11 +180,8 @@ | ||
| 180 | 180 | $uniqid = $request->input('uniqid'); |
| 181 | 181 | $archiver->setArchive($this->storage, $uniqid); |
| 182 | 182 | } |
| 183 | 183 | |
| 184 | - // close session | |
| 185 | - $this->session->save(); | |
| 186 | - | |
| 187 | 184 | foreach ($items as $item) { |
| 188 | 185 | if ($item->type == 'dir') { |
| 189 | 186 | if($recursive) { |
| 190 | 187 | $archiver->addDirectoryFromStorage($item->path, $destination); |
| @@ -219,18 +216,20 @@ | ||
| 219 | 216 | { |
| 220 | 217 | |
| 221 | 218 | |
| 222 | 219 | $uniqid = (string) preg_replace('/[^0-9a-zA-Z_]/', '', (string) $request->input('uniqid')); |
| 220 | + $name = $request->input('name', $this->config->get('file.default_archive_name')); | |
| 221 | + | |
| 223 | 222 | $file = $tmpfs->readStream($uniqid); |
| 224 | 223 | |
| 225 | 224 | $streamedResponse->setCallback(function () use ($file, $tmpfs, $uniqid) { |
| 226 | 225 | // @codeCoverageIgnoreStart |
| 227 | - set_time_limit(0); | |
| 226 | + @set_time_limit(0); | |
| 228 | 227 | if ($file['stream']) { |
| 229 | 228 | while (! feof($file['stream'])) { |
| 230 | 229 | echo fread($file['stream'], 1024 * 8); |
| 231 | - ob_flush(); | |
| 232 | - flush(); | |
| 230 | + @ob_flush(); | |
| 231 | + @flush(); | |
| 233 | 232 | } |
| 234 | 233 | fclose($file['stream']); |
| 235 | 234 | } |
| 236 | 235 | $tmpfs->remove($uniqid); |
| @@ -240,10 +239,10 @@ | ||
| 240 | 239 | $streamedResponse->headers->set( |
| 241 | 240 | 'Content-Disposition', |
| 242 | 241 | HeaderUtils::makeDisposition( |
| 243 | 242 | HeaderUtils::DISPOSITION_ATTACHMENT, |
| 244 | - $this->config->get('file.default_archive_name'), | |
| 245 | - 'archive.zip' | |
| 243 | + $name, | |
| 244 | + $this->config->get('file.default_archive_name') | |
| 246 | 245 | ) |
| 247 | 246 | ); |
| 248 | 247 | $streamedResponse->headers->set( |
| 249 | 248 | 'Content-Type', |
| @@ -258,10 +257,8 @@ | ||
| 258 | 257 | 'Content-Length', |
| 259 | 258 | $file['filesize'] |
| 260 | 259 | ); |
| 261 | 260 | } |
| 262 | - // close session, so we can continue streaming, note: dev is single-threaded | |
| 263 | - $this->session->save(); | |
| 264 | 261 | |
| 265 | 262 | $streamedResponse->send(); |
| 266 | 263 | } |
| 267 | 264 | |