@@ -28,105 +28,105 @@ |
||
| 28 | 28 | use ZipStreamer\ZipStreamer; |
| 29 | 29 | |
| 30 | 30 | class Streamer { |
| 31 | - // array of regexp. Matching user agents will get tar instead of zip |
|
| 32 | - private $preferTarFor = [ '/macintosh|mac os x/i' ]; |
|
| 31 | + // array of regexp. Matching user agents will get tar instead of zip |
|
| 32 | + private $preferTarFor = [ '/macintosh|mac os x/i' ]; |
|
| 33 | 33 | |
| 34 | - // streamer instance |
|
| 35 | - private $streamerInstance; |
|
| 34 | + // streamer instance |
|
| 35 | + private $streamerInstance; |
|
| 36 | 36 | |
| 37 | - public function __construct(){ |
|
| 38 | - /** @var \OCP\IRequest */ |
|
| 39 | - $request = \OC::$server->getRequest(); |
|
| 37 | + public function __construct(){ |
|
| 38 | + /** @var \OCP\IRequest */ |
|
| 39 | + $request = \OC::$server->getRequest(); |
|
| 40 | 40 | |
| 41 | - if ($request->isUserAgent($this->preferTarFor)) { |
|
| 42 | - $this->streamerInstance = new TarStreamer(); |
|
| 43 | - } else { |
|
| 44 | - $this->streamerInstance = new ZipStreamer(); |
|
| 45 | - } |
|
| 46 | - } |
|
| 41 | + if ($request->isUserAgent($this->preferTarFor)) { |
|
| 42 | + $this->streamerInstance = new TarStreamer(); |
|
| 43 | + } else { |
|
| 44 | + $this->streamerInstance = new ZipStreamer(); |
|
| 45 | + } |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * Send HTTP headers |
|
| 50 | - * @param string $name |
|
| 51 | - */ |
|
| 52 | - public function sendHeaders($name){ |
|
| 53 | - $extension = $this->streamerInstance instanceof ZipStreamer ? '.zip' : '.tar'; |
|
| 54 | - $fullName = $name . $extension; |
|
| 55 | - $this->streamerInstance->sendHeaders($fullName); |
|
| 56 | - } |
|
| 48 | + /** |
|
| 49 | + * Send HTTP headers |
|
| 50 | + * @param string $name |
|
| 51 | + */ |
|
| 52 | + public function sendHeaders($name){ |
|
| 53 | + $extension = $this->streamerInstance instanceof ZipStreamer ? '.zip' : '.tar'; |
|
| 54 | + $fullName = $name . $extension; |
|
| 55 | + $this->streamerInstance->sendHeaders($fullName); |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - /** |
|
| 59 | - * Stream directory recursively |
|
| 60 | - * @param string $dir |
|
| 61 | - * @param string $internalDir |
|
| 62 | - */ |
|
| 63 | - public function addDirRecursive($dir, $internalDir='') { |
|
| 64 | - $dirname = basename($dir); |
|
| 65 | - $rootDir = $internalDir . $dirname; |
|
| 66 | - if (!empty($rootDir)) { |
|
| 67 | - $this->streamerInstance->addEmptyDir($rootDir); |
|
| 68 | - } |
|
| 69 | - $internalDir .= $dirname . '/'; |
|
| 70 | - // prevent absolute dirs |
|
| 71 | - $internalDir = ltrim($internalDir, '/'); |
|
| 58 | + /** |
|
| 59 | + * Stream directory recursively |
|
| 60 | + * @param string $dir |
|
| 61 | + * @param string $internalDir |
|
| 62 | + */ |
|
| 63 | + public function addDirRecursive($dir, $internalDir='') { |
|
| 64 | + $dirname = basename($dir); |
|
| 65 | + $rootDir = $internalDir . $dirname; |
|
| 66 | + if (!empty($rootDir)) { |
|
| 67 | + $this->streamerInstance->addEmptyDir($rootDir); |
|
| 68 | + } |
|
| 69 | + $internalDir .= $dirname . '/'; |
|
| 70 | + // prevent absolute dirs |
|
| 71 | + $internalDir = ltrim($internalDir, '/'); |
|
| 72 | 72 | |
| 73 | - $files= \OC\Files\Filesystem::getDirectoryContent($dir); |
|
| 74 | - foreach($files as $file) { |
|
| 75 | - $filename = $file['name']; |
|
| 76 | - $file = $dir . '/' . $filename; |
|
| 77 | - if(\OC\Files\Filesystem::is_file($file)) { |
|
| 78 | - $filesize = \OC\Files\Filesystem::filesize($file); |
|
| 79 | - $fileTime = \OC\Files\Filesystem::filemtime($file); |
|
| 80 | - $fh = \OC\Files\Filesystem::fopen($file, 'r'); |
|
| 81 | - $this->addFileFromStream($fh, $internalDir . $filename, $filesize, $fileTime); |
|
| 82 | - fclose($fh); |
|
| 83 | - }elseif(\OC\Files\Filesystem::is_dir($file)) { |
|
| 84 | - $this->addDirRecursive($file, $internalDir); |
|
| 85 | - } |
|
| 86 | - } |
|
| 87 | - } |
|
| 73 | + $files= \OC\Files\Filesystem::getDirectoryContent($dir); |
|
| 74 | + foreach($files as $file) { |
|
| 75 | + $filename = $file['name']; |
|
| 76 | + $file = $dir . '/' . $filename; |
|
| 77 | + if(\OC\Files\Filesystem::is_file($file)) { |
|
| 78 | + $filesize = \OC\Files\Filesystem::filesize($file); |
|
| 79 | + $fileTime = \OC\Files\Filesystem::filemtime($file); |
|
| 80 | + $fh = \OC\Files\Filesystem::fopen($file, 'r'); |
|
| 81 | + $this->addFileFromStream($fh, $internalDir . $filename, $filesize, $fileTime); |
|
| 82 | + fclose($fh); |
|
| 83 | + }elseif(\OC\Files\Filesystem::is_dir($file)) { |
|
| 84 | + $this->addDirRecursive($file, $internalDir); |
|
| 85 | + } |
|
| 86 | + } |
|
| 87 | + } |
|
| 88 | 88 | |
| 89 | - /** |
|
| 90 | - * Add a file to the archive at the specified location and file name. |
|
| 91 | - * |
|
| 92 | - * @param string $stream Stream to read data from |
|
| 93 | - * @param string $internalName Filepath and name to be used in the archive. |
|
| 94 | - * @param int $size Filesize |
|
| 95 | - * @param int|bool $time File mtime as int, or false |
|
| 96 | - * @return bool $success |
|
| 97 | - */ |
|
| 98 | - public function addFileFromStream($stream, $internalName, $size, $time) { |
|
| 99 | - $options = []; |
|
| 100 | - if ($time) { |
|
| 101 | - $options = [ |
|
| 102 | - 'timestamp' => $time |
|
| 103 | - ]; |
|
| 104 | - } |
|
| 89 | + /** |
|
| 90 | + * Add a file to the archive at the specified location and file name. |
|
| 91 | + * |
|
| 92 | + * @param string $stream Stream to read data from |
|
| 93 | + * @param string $internalName Filepath and name to be used in the archive. |
|
| 94 | + * @param int $size Filesize |
|
| 95 | + * @param int|bool $time File mtime as int, or false |
|
| 96 | + * @return bool $success |
|
| 97 | + */ |
|
| 98 | + public function addFileFromStream($stream, $internalName, $size, $time) { |
|
| 99 | + $options = []; |
|
| 100 | + if ($time) { |
|
| 101 | + $options = [ |
|
| 102 | + 'timestamp' => $time |
|
| 103 | + ]; |
|
| 104 | + } |
|
| 105 | 105 | |
| 106 | - if ($this->streamerInstance instanceof ZipStreamer) { |
|
| 107 | - return $this->streamerInstance->addFileFromStream($stream, $internalName, $options); |
|
| 108 | - } else { |
|
| 109 | - return $this->streamerInstance->addFileFromStream($stream, $internalName, $size, $options); |
|
| 110 | - } |
|
| 111 | - } |
|
| 106 | + if ($this->streamerInstance instanceof ZipStreamer) { |
|
| 107 | + return $this->streamerInstance->addFileFromStream($stream, $internalName, $options); |
|
| 108 | + } else { |
|
| 109 | + return $this->streamerInstance->addFileFromStream($stream, $internalName, $size, $options); |
|
| 110 | + } |
|
| 111 | + } |
|
| 112 | 112 | |
| 113 | - /** |
|
| 114 | - * Add an empty directory entry to the archive. |
|
| 115 | - * |
|
| 116 | - * @param string $dirName Directory Path and name to be added to the archive. |
|
| 117 | - * @return bool $success |
|
| 118 | - */ |
|
| 119 | - public function addEmptyDir($dirName){ |
|
| 120 | - return $this->streamerInstance->addEmptyDir($dirName); |
|
| 121 | - } |
|
| 113 | + /** |
|
| 114 | + * Add an empty directory entry to the archive. |
|
| 115 | + * |
|
| 116 | + * @param string $dirName Directory Path and name to be added to the archive. |
|
| 117 | + * @return bool $success |
|
| 118 | + */ |
|
| 119 | + public function addEmptyDir($dirName){ |
|
| 120 | + return $this->streamerInstance->addEmptyDir($dirName); |
|
| 121 | + } |
|
| 122 | 122 | |
| 123 | - /** |
|
| 124 | - * Close the archive. |
|
| 125 | - * A closed archive can no longer have new files added to it. After |
|
| 126 | - * closing, the file is completely written to the output stream. |
|
| 127 | - * @return bool $success |
|
| 128 | - */ |
|
| 129 | - public function finalize(){ |
|
| 130 | - return $this->streamerInstance->finalize(); |
|
| 131 | - } |
|
| 123 | + /** |
|
| 124 | + * Close the archive. |
|
| 125 | + * A closed archive can no longer have new files added to it. After |
|
| 126 | + * closing, the file is completely written to the output stream. |
|
| 127 | + * @return bool $success |
|
| 128 | + */ |
|
| 129 | + public function finalize(){ |
|
| 130 | + return $this->streamerInstance->finalize(); |
|
| 131 | + } |
|
| 132 | 132 | } |