@@ -39,152 +39,152 @@ |
||
39 | 39 | use ZipStreamer\ZipStreamer; |
40 | 40 | |
41 | 41 | class Streamer { |
42 | - // array of regexp. Matching user agents will get tar instead of zip |
|
43 | - private $preferTarFor = [ '/macintosh|mac os x/i' ]; |
|
42 | + // array of regexp. Matching user agents will get tar instead of zip |
|
43 | + private $preferTarFor = [ '/macintosh|mac os x/i' ]; |
|
44 | 44 | |
45 | - // streamer instance |
|
46 | - private $streamerInstance; |
|
45 | + // streamer instance |
|
46 | + private $streamerInstance; |
|
47 | 47 | |
48 | - /** |
|
49 | - * Streamer constructor. |
|
50 | - * |
|
51 | - * @param IRequest $request |
|
52 | - * @param int $size The size of the files in bytes |
|
53 | - * @param int $numberOfFiles The number of files (and directories) that will |
|
54 | - * be included in the streamed file |
|
55 | - */ |
|
56 | - public function __construct(IRequest $request, int $size, int $numberOfFiles) { |
|
48 | + /** |
|
49 | + * Streamer constructor. |
|
50 | + * |
|
51 | + * @param IRequest $request |
|
52 | + * @param int $size The size of the files in bytes |
|
53 | + * @param int $numberOfFiles The number of files (and directories) that will |
|
54 | + * be included in the streamed file |
|
55 | + */ |
|
56 | + public function __construct(IRequest $request, int $size, int $numberOfFiles) { |
|
57 | 57 | |
58 | - /** |
|
59 | - * zip32 constraints for a basic (without compression, volumes nor |
|
60 | - * encryption) zip file according to the Zip specification: |
|
61 | - * - No file size is larger than 4 bytes (file size < 4294967296); see |
|
62 | - * 4.4.9 uncompressed size |
|
63 | - * - The size of all files plus their local headers is not larger than |
|
64 | - * 4 bytes; see 4.4.16 relative offset of local header and 4.4.24 |
|
65 | - * offset of start of central directory with respect to the starting |
|
66 | - * disk number |
|
67 | - * - The total number of entries (files and directories) in the zip file |
|
68 | - * is not larger than 2 bytes (number of entries < 65536); see 4.4.22 |
|
69 | - * total number of entries in the central dir |
|
70 | - * - The size of the central directory is not larger than 4 bytes; see |
|
71 | - * 4.4.23 size of the central directory |
|
72 | - * |
|
73 | - * Due to all that, zip32 is used if the size is below 4GB and there are |
|
74 | - * less than 65536 files; the margin between 4*1000^3 and 4*1024^3 |
|
75 | - * should give enough room for the extra zip metadata. Technically, it |
|
76 | - * would still be possible to create an invalid zip32 file (for example, |
|
77 | - * a zip file from files smaller than 4GB with a central directory |
|
78 | - * larger than 4GiB), but it should not happen in the real world. |
|
79 | - * |
|
80 | - * We also have to check for a size above 0. As negative sizes could be |
|
81 | - * from not fully scanned external storage. And then things fall apart |
|
82 | - * if somebody tries to package to much. |
|
83 | - */ |
|
84 | - if ($size > 0 && $size < 4 * 1000 * 1000 * 1000 && $numberOfFiles < 65536) { |
|
85 | - $this->streamerInstance = new ZipStreamer(['zip64' => false]); |
|
86 | - } elseif ($request->isUserAgent($this->preferTarFor)) { |
|
87 | - $this->streamerInstance = new TarStreamer(); |
|
88 | - } else { |
|
89 | - $this->streamerInstance = new ZipStreamer(['zip64' => PHP_INT_SIZE !== 4]); |
|
90 | - } |
|
91 | - } |
|
58 | + /** |
|
59 | + * zip32 constraints for a basic (without compression, volumes nor |
|
60 | + * encryption) zip file according to the Zip specification: |
|
61 | + * - No file size is larger than 4 bytes (file size < 4294967296); see |
|
62 | + * 4.4.9 uncompressed size |
|
63 | + * - The size of all files plus their local headers is not larger than |
|
64 | + * 4 bytes; see 4.4.16 relative offset of local header and 4.4.24 |
|
65 | + * offset of start of central directory with respect to the starting |
|
66 | + * disk number |
|
67 | + * - The total number of entries (files and directories) in the zip file |
|
68 | + * is not larger than 2 bytes (number of entries < 65536); see 4.4.22 |
|
69 | + * total number of entries in the central dir |
|
70 | + * - The size of the central directory is not larger than 4 bytes; see |
|
71 | + * 4.4.23 size of the central directory |
|
72 | + * |
|
73 | + * Due to all that, zip32 is used if the size is below 4GB and there are |
|
74 | + * less than 65536 files; the margin between 4*1000^3 and 4*1024^3 |
|
75 | + * should give enough room for the extra zip metadata. Technically, it |
|
76 | + * would still be possible to create an invalid zip32 file (for example, |
|
77 | + * a zip file from files smaller than 4GB with a central directory |
|
78 | + * larger than 4GiB), but it should not happen in the real world. |
|
79 | + * |
|
80 | + * We also have to check for a size above 0. As negative sizes could be |
|
81 | + * from not fully scanned external storage. And then things fall apart |
|
82 | + * if somebody tries to package to much. |
|
83 | + */ |
|
84 | + if ($size > 0 && $size < 4 * 1000 * 1000 * 1000 && $numberOfFiles < 65536) { |
|
85 | + $this->streamerInstance = new ZipStreamer(['zip64' => false]); |
|
86 | + } elseif ($request->isUserAgent($this->preferTarFor)) { |
|
87 | + $this->streamerInstance = new TarStreamer(); |
|
88 | + } else { |
|
89 | + $this->streamerInstance = new ZipStreamer(['zip64' => PHP_INT_SIZE !== 4]); |
|
90 | + } |
|
91 | + } |
|
92 | 92 | |
93 | - /** |
|
94 | - * Send HTTP headers |
|
95 | - * @param string $name |
|
96 | - */ |
|
97 | - public function sendHeaders($name) { |
|
98 | - $extension = $this->streamerInstance instanceof ZipStreamer ? '.zip' : '.tar'; |
|
99 | - $fullName = $name . $extension; |
|
100 | - $this->streamerInstance->sendHeaders($fullName); |
|
101 | - } |
|
93 | + /** |
|
94 | + * Send HTTP headers |
|
95 | + * @param string $name |
|
96 | + */ |
|
97 | + public function sendHeaders($name) { |
|
98 | + $extension = $this->streamerInstance instanceof ZipStreamer ? '.zip' : '.tar'; |
|
99 | + $fullName = $name . $extension; |
|
100 | + $this->streamerInstance->sendHeaders($fullName); |
|
101 | + } |
|
102 | 102 | |
103 | - /** |
|
104 | - * Stream directory recursively |
|
105 | - * |
|
106 | - * @throws NotFoundException |
|
107 | - * @throws NotPermittedException |
|
108 | - * @throws InvalidPathException |
|
109 | - */ |
|
110 | - public function addDirRecursive(string $dir, string $internalDir = ''): void { |
|
111 | - $dirname = basename($dir); |
|
112 | - $rootDir = $internalDir . $dirname; |
|
113 | - if (!empty($rootDir)) { |
|
114 | - $this->streamerInstance->addEmptyDir($rootDir); |
|
115 | - } |
|
116 | - $internalDir .= $dirname . '/'; |
|
117 | - // prevent absolute dirs |
|
118 | - $internalDir = ltrim($internalDir, '/'); |
|
103 | + /** |
|
104 | + * Stream directory recursively |
|
105 | + * |
|
106 | + * @throws NotFoundException |
|
107 | + * @throws NotPermittedException |
|
108 | + * @throws InvalidPathException |
|
109 | + */ |
|
110 | + public function addDirRecursive(string $dir, string $internalDir = ''): void { |
|
111 | + $dirname = basename($dir); |
|
112 | + $rootDir = $internalDir . $dirname; |
|
113 | + if (!empty($rootDir)) { |
|
114 | + $this->streamerInstance->addEmptyDir($rootDir); |
|
115 | + } |
|
116 | + $internalDir .= $dirname . '/'; |
|
117 | + // prevent absolute dirs |
|
118 | + $internalDir = ltrim($internalDir, '/'); |
|
119 | 119 | |
120 | - $userFolder = \OC::$server->getRootFolder()->get(Filesystem::getRoot()); |
|
121 | - /** @var Folder $dirNode */ |
|
122 | - $dirNode = $userFolder->get($dir); |
|
123 | - $files = $dirNode->getDirectoryListing(); |
|
120 | + $userFolder = \OC::$server->getRootFolder()->get(Filesystem::getRoot()); |
|
121 | + /** @var Folder $dirNode */ |
|
122 | + $dirNode = $userFolder->get($dir); |
|
123 | + $files = $dirNode->getDirectoryListing(); |
|
124 | 124 | |
125 | - foreach ($files as $file) { |
|
126 | - if ($file instanceof File) { |
|
127 | - try { |
|
128 | - $fh = $file->fopen('r'); |
|
129 | - } catch (NotPermittedException $e) { |
|
130 | - continue; |
|
131 | - } |
|
132 | - $this->addFileFromStream( |
|
133 | - $fh, |
|
134 | - $internalDir . $file->getName(), |
|
135 | - $file->getSize(), |
|
136 | - $file->getMTime() |
|
137 | - ); |
|
138 | - fclose($fh); |
|
139 | - } elseif ($file instanceof Folder) { |
|
140 | - if ($file->isReadable()) { |
|
141 | - $this->addDirRecursive($dir . '/' . $file->getName(), $internalDir); |
|
142 | - } |
|
143 | - } |
|
144 | - } |
|
145 | - } |
|
125 | + foreach ($files as $file) { |
|
126 | + if ($file instanceof File) { |
|
127 | + try { |
|
128 | + $fh = $file->fopen('r'); |
|
129 | + } catch (NotPermittedException $e) { |
|
130 | + continue; |
|
131 | + } |
|
132 | + $this->addFileFromStream( |
|
133 | + $fh, |
|
134 | + $internalDir . $file->getName(), |
|
135 | + $file->getSize(), |
|
136 | + $file->getMTime() |
|
137 | + ); |
|
138 | + fclose($fh); |
|
139 | + } elseif ($file instanceof Folder) { |
|
140 | + if ($file->isReadable()) { |
|
141 | + $this->addDirRecursive($dir . '/' . $file->getName(), $internalDir); |
|
142 | + } |
|
143 | + } |
|
144 | + } |
|
145 | + } |
|
146 | 146 | |
147 | - /** |
|
148 | - * Add a file to the archive at the specified location and file name. |
|
149 | - * |
|
150 | - * @param string $stream Stream to read data from |
|
151 | - * @param string $internalName Filepath and name to be used in the archive. |
|
152 | - * @param int $size Filesize |
|
153 | - * @param int|bool $time File mtime as int, or false |
|
154 | - * @return bool $success |
|
155 | - */ |
|
156 | - public function addFileFromStream($stream, $internalName, $size, $time) { |
|
157 | - $options = []; |
|
158 | - if ($time) { |
|
159 | - $options = [ |
|
160 | - 'timestamp' => $time |
|
161 | - ]; |
|
162 | - } |
|
147 | + /** |
|
148 | + * Add a file to the archive at the specified location and file name. |
|
149 | + * |
|
150 | + * @param string $stream Stream to read data from |
|
151 | + * @param string $internalName Filepath and name to be used in the archive. |
|
152 | + * @param int $size Filesize |
|
153 | + * @param int|bool $time File mtime as int, or false |
|
154 | + * @return bool $success |
|
155 | + */ |
|
156 | + public function addFileFromStream($stream, $internalName, $size, $time) { |
|
157 | + $options = []; |
|
158 | + if ($time) { |
|
159 | + $options = [ |
|
160 | + 'timestamp' => $time |
|
161 | + ]; |
|
162 | + } |
|
163 | 163 | |
164 | - if ($this->streamerInstance instanceof ZipStreamer) { |
|
165 | - return $this->streamerInstance->addFileFromStream($stream, $internalName, $options); |
|
166 | - } else { |
|
167 | - return $this->streamerInstance->addFileFromStream($stream, $internalName, $size, $options); |
|
168 | - } |
|
169 | - } |
|
164 | + if ($this->streamerInstance instanceof ZipStreamer) { |
|
165 | + return $this->streamerInstance->addFileFromStream($stream, $internalName, $options); |
|
166 | + } else { |
|
167 | + return $this->streamerInstance->addFileFromStream($stream, $internalName, $size, $options); |
|
168 | + } |
|
169 | + } |
|
170 | 170 | |
171 | - /** |
|
172 | - * Add an empty directory entry to the archive. |
|
173 | - * |
|
174 | - * @param string $dirName Directory Path and name to be added to the archive. |
|
175 | - * @return bool $success |
|
176 | - */ |
|
177 | - public function addEmptyDir($dirName) { |
|
178 | - return $this->streamerInstance->addEmptyDir($dirName); |
|
179 | - } |
|
171 | + /** |
|
172 | + * Add an empty directory entry to the archive. |
|
173 | + * |
|
174 | + * @param string $dirName Directory Path and name to be added to the archive. |
|
175 | + * @return bool $success |
|
176 | + */ |
|
177 | + public function addEmptyDir($dirName) { |
|
178 | + return $this->streamerInstance->addEmptyDir($dirName); |
|
179 | + } |
|
180 | 180 | |
181 | - /** |
|
182 | - * Close the archive. |
|
183 | - * A closed archive can no longer have new files added to it. After |
|
184 | - * closing, the file is completely written to the output stream. |
|
185 | - * @return bool $success |
|
186 | - */ |
|
187 | - public function finalize() { |
|
188 | - return $this->streamerInstance->finalize(); |
|
189 | - } |
|
181 | + /** |
|
182 | + * Close the archive. |
|
183 | + * A closed archive can no longer have new files added to it. After |
|
184 | + * closing, the file is completely written to the output stream. |
|
185 | + * @return bool $success |
|
186 | + */ |
|
187 | + public function finalize() { |
|
188 | + return $this->streamerInstance->finalize(); |
|
189 | + } |
|
190 | 190 | } |