Streamer.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Daniel Calviño Sánchez <danxuliu@gmail.com>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author szaimen <szaimen@e.mail.de>
  11. * @author Thomas Müller <thomas.mueller@tmit.eu>
  12. * @author Victor Dubiniuk <dubiniuk@owncloud.com>
  13. *
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. namespace OC;
  30. use OC\Files\Filesystem;
  31. use OCP\Files\File;
  32. use OCP\Files\Folder;
  33. use OCP\Files\InvalidPathException;
  34. use OCP\Files\NotFoundException;
  35. use OCP\Files\NotPermittedException;
  36. use OCP\IRequest;
  37. use ownCloud\TarStreamer\TarStreamer;
  38. use ZipStreamer\ZipStreamer;
  39. class Streamer {
  40. // array of regexp. Matching user agents will get tar instead of zip
  41. private array $preferTarFor = [ '/macintosh|mac os x/i' ];
  42. // streamer instance
  43. private $streamerInstance;
  44. /**
  45. * Streamer constructor.
  46. *
  47. * @param IRequest $request
  48. * @param int|float $size The size of the files in bytes
  49. * @param int $numberOfFiles The number of files (and directories) that will
  50. * be included in the streamed file
  51. */
  52. public function __construct(IRequest $request, int|float $size, int $numberOfFiles) {
  53. /**
  54. * zip32 constraints for a basic (without compression, volumes nor
  55. * encryption) zip file according to the Zip specification:
  56. * - No file size is larger than 4 bytes (file size < 4294967296); see
  57. * 4.4.9 uncompressed size
  58. * - The size of all files plus their local headers is not larger than
  59. * 4 bytes; see 4.4.16 relative offset of local header and 4.4.24
  60. * offset of start of central directory with respect to the starting
  61. * disk number
  62. * - The total number of entries (files and directories) in the zip file
  63. * is not larger than 2 bytes (number of entries < 65536); see 4.4.22
  64. * total number of entries in the central dir
  65. * - The size of the central directory is not larger than 4 bytes; see
  66. * 4.4.23 size of the central directory
  67. *
  68. * Due to all that, zip32 is used if the size is below 4GB and there are
  69. * less than 65536 files; the margin between 4*1000^3 and 4*1024^3
  70. * should give enough room for the extra zip metadata. Technically, it
  71. * would still be possible to create an invalid zip32 file (for example,
  72. * a zip file from files smaller than 4GB with a central directory
  73. * larger than 4GiB), but it should not happen in the real world.
  74. *
  75. * We also have to check for a size above 0. As negative sizes could be
  76. * from not fully scanned external storage. And then things fall apart
  77. * if somebody tries to package to much.
  78. */
  79. if ($size > 0 && $size < 4 * 1000 * 1000 * 1000 && $numberOfFiles < 65536) {
  80. $this->streamerInstance = new ZipStreamer(['zip64' => false]);
  81. } elseif ($request->isUserAgent($this->preferTarFor)) {
  82. $this->streamerInstance = new TarStreamer();
  83. } else {
  84. $this->streamerInstance = new ZipStreamer(['zip64' => PHP_INT_SIZE !== 4]);
  85. }
  86. }
  87. /**
  88. * Send HTTP headers
  89. * @param string $name
  90. */
  91. public function sendHeaders($name) {
  92. header('X-Accel-Buffering: no');
  93. $extension = $this->streamerInstance instanceof ZipStreamer ? '.zip' : '.tar';
  94. $fullName = $name . $extension;
  95. $this->streamerInstance->sendHeaders($fullName);
  96. }
  97. /**
  98. * Stream directory recursively
  99. *
  100. * @throws NotFoundException
  101. * @throws NotPermittedException
  102. * @throws InvalidPathException
  103. */
  104. public function addDirRecursive(string $dir, string $internalDir = ''): void {
  105. $dirname = basename($dir);
  106. $rootDir = $internalDir . $dirname;
  107. if (!empty($rootDir)) {
  108. $this->streamerInstance->addEmptyDir($rootDir);
  109. }
  110. $internalDir .= $dirname . '/';
  111. // prevent absolute dirs
  112. $internalDir = ltrim($internalDir, '/');
  113. $userFolder = \OC::$server->getRootFolder()->get(Filesystem::getRoot());
  114. /** @var Folder $dirNode */
  115. $dirNode = $userFolder->get($dir);
  116. $files = $dirNode->getDirectoryListing();
  117. foreach ($files as $file) {
  118. if ($file instanceof File) {
  119. try {
  120. $fh = $file->fopen('r');
  121. } catch (NotPermittedException $e) {
  122. continue;
  123. }
  124. $this->addFileFromStream(
  125. $fh,
  126. $internalDir . $file->getName(),
  127. $file->getSize(),
  128. $file->getMTime()
  129. );
  130. fclose($fh);
  131. } elseif ($file instanceof Folder) {
  132. if ($file->isReadable()) {
  133. $this->addDirRecursive($dir . '/' . $file->getName(), $internalDir);
  134. }
  135. }
  136. }
  137. }
  138. /**
  139. * Add a file to the archive at the specified location and file name.
  140. *
  141. * @param resource $stream Stream to read data from
  142. * @param string $internalName Filepath and name to be used in the archive.
  143. * @param int|float $size Filesize
  144. * @param int|false $time File mtime as int, or false
  145. * @return bool $success
  146. */
  147. public function addFileFromStream($stream, string $internalName, int|float $size, $time): bool {
  148. $options = [];
  149. if ($time) {
  150. $options = [
  151. 'timestamp' => $time
  152. ];
  153. }
  154. if ($this->streamerInstance instanceof ZipStreamer) {
  155. return $this->streamerInstance->addFileFromStream($stream, $internalName, $options);
  156. } else {
  157. return $this->streamerInstance->addFileFromStream($stream, $internalName, $size, $options);
  158. }
  159. }
  160. /**
  161. * Add an empty directory entry to the archive.
  162. *
  163. * @param string $dirName Directory Path and name to be added to the archive.
  164. * @return bool $success
  165. */
  166. public function addEmptyDir($dirName) {
  167. return $this->streamerInstance->addEmptyDir($dirName);
  168. }
  169. /**
  170. * Close the archive.
  171. * A closed archive can no longer have new files added to it. After
  172. * closing, the file is completely written to the output stream.
  173. * @return bool $success
  174. */
  175. public function finalize() {
  176. return $this->streamerInstance->finalize();
  177. }
  178. }