Streamer.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC;
  8. use OC\Files\Filesystem;
  9. use OCP\Files\File;
  10. use OCP\Files\Folder;
  11. use OCP\Files\InvalidPathException;
  12. use OCP\Files\IRootFolder;
  13. use OCP\Files\NotFoundException;
  14. use OCP\Files\NotPermittedException;
  15. use OCP\IRequest;
  16. use ownCloud\TarStreamer\TarStreamer;
  17. use Psr\Log\LoggerInterface;
  18. use ZipStreamer\ZipStreamer;
  19. class Streamer {
  20. // array of regexp. Matching user agents will get tar instead of zip
  21. private const UA_PREFERS_TAR = [ '/macintosh|mac os x/i' ];
  22. // streamer instance
  23. private $streamerInstance;
  24. public static function isUserAgentPreferTar(IRequest $request): bool {
  25. return $request->isUserAgent(self::UA_PREFERS_TAR);
  26. }
  27. /**
  28. * Streamer constructor.
  29. *
  30. * @param bool|IRequest $preferTar If true a tar stream is used.
  31. * For legacy reasons also a IRequest can be passed to detect this preference by user agent,
  32. * please migrate to `Streamer::isUserAgentPreferTar()` instead.
  33. * @param int|float $size The size of the files in bytes
  34. * @param int $numberOfFiles The number of files (and directories) that will
  35. * be included in the streamed file
  36. */
  37. public function __construct(IRequest|bool $preferTar, int|float $size, int $numberOfFiles) {
  38. if ($preferTar instanceof IRequest) {
  39. $preferTar = self::isUserAgentPreferTar($preferTar);
  40. }
  41. /**
  42. * zip32 constraints for a basic (without compression, volumes nor
  43. * encryption) zip file according to the Zip specification:
  44. * - No file size is larger than 4 bytes (file size < 4294967296); see
  45. * 4.4.9 uncompressed size
  46. * - The size of all files plus their local headers is not larger than
  47. * 4 bytes; see 4.4.16 relative offset of local header and 4.4.24
  48. * offset of start of central directory with respect to the starting
  49. * disk number
  50. * - The total number of entries (files and directories) in the zip file
  51. * is not larger than 2 bytes (number of entries < 65536); see 4.4.22
  52. * total number of entries in the central dir
  53. * - The size of the central directory is not larger than 4 bytes; see
  54. * 4.4.23 size of the central directory
  55. *
  56. * Due to all that, zip32 is used if the size is below 4GB and there are
  57. * less than 65536 files; the margin between 4*1000^3 and 4*1024^3
  58. * should give enough room for the extra zip metadata. Technically, it
  59. * would still be possible to create an invalid zip32 file (for example,
  60. * a zip file from files smaller than 4GB with a central directory
  61. * larger than 4GiB), but it should not happen in the real world.
  62. *
  63. * We also have to check for a size above 0. As negative sizes could be
  64. * from not fully scanned external storage. And then things fall apart
  65. * if somebody tries to package to much.
  66. */
  67. if ($preferTar) {
  68. // If TAR ball is preferred use it
  69. $this->streamerInstance = new TarStreamer();
  70. } elseif ($size > 0 && $size < 4 * 1000 * 1000 * 1000 && $numberOfFiles < 65536) {
  71. $this->streamerInstance = new ZipStreamer(['zip64' => false]);
  72. } else {
  73. $this->streamerInstance = new ZipStreamer(['zip64' => PHP_INT_SIZE !== 4]);
  74. }
  75. }
  76. /**
  77. * Send HTTP headers
  78. * @param string $name
  79. */
  80. public function sendHeaders($name) {
  81. header('X-Accel-Buffering: no');
  82. $extension = $this->streamerInstance instanceof ZipStreamer ? '.zip' : '.tar';
  83. $fullName = $name . $extension;
  84. $this->streamerInstance->sendHeaders($fullName);
  85. }
  86. /**
  87. * Stream directory recursively
  88. *
  89. * @param string $dir Directory path relative to root of current user
  90. * @throws NotFoundException
  91. * @throws NotPermittedException
  92. * @throws InvalidPathException
  93. */
  94. public function addDirRecursive(string $dir, string $internalDir = ''): void {
  95. $dirname = basename($dir);
  96. $rootDir = $internalDir . $dirname;
  97. if (!empty($rootDir)) {
  98. $this->streamerInstance->addEmptyDir($rootDir);
  99. }
  100. $internalDir .= $dirname . '/';
  101. // prevent absolute dirs
  102. $internalDir = ltrim($internalDir, '/');
  103. $userFolder = \OC::$server->get(IRootFolder::class)->get(Filesystem::getRoot());
  104. /** @var Folder $dirNode */
  105. $dirNode = $userFolder->get($dir);
  106. $files = $dirNode->getDirectoryListing();
  107. /** @var LoggerInterface $logger */
  108. $logger = \OC::$server->query(LoggerInterface::class);
  109. foreach ($files as $file) {
  110. if ($file instanceof File) {
  111. try {
  112. $fh = $file->fopen('r');
  113. if ($fh === false) {
  114. $logger->error('Unable to open file for stream: ' . print_r($file, true));
  115. continue;
  116. }
  117. } catch (NotPermittedException $e) {
  118. continue;
  119. }
  120. $this->addFileFromStream(
  121. $fh,
  122. $internalDir . $file->getName(),
  123. $file->getSize(),
  124. $file->getMTime()
  125. );
  126. fclose($fh);
  127. } elseif ($file instanceof Folder) {
  128. if ($file->isReadable()) {
  129. $this->addDirRecursive($dir . '/' . $file->getName(), $internalDir);
  130. }
  131. }
  132. }
  133. }
  134. /**
  135. * Add a file to the archive at the specified location and file name.
  136. *
  137. * @param resource $stream Stream to read data from
  138. * @param string $internalName Filepath and name to be used in the archive.
  139. * @param int|float $size Filesize
  140. * @param int|false $time File mtime as int, or false
  141. * @return bool $success
  142. */
  143. public function addFileFromStream($stream, string $internalName, int|float $size, $time): bool {
  144. $options = [];
  145. if ($time) {
  146. $options = [
  147. 'timestamp' => $time
  148. ];
  149. }
  150. if ($this->streamerInstance instanceof ZipStreamer) {
  151. return $this->streamerInstance->addFileFromStream($stream, $internalName, $options);
  152. } else {
  153. return $this->streamerInstance->addFileFromStream($stream, $internalName, $size, $options);
  154. }
  155. }
  156. /**
  157. * Add an empty directory entry to the archive.
  158. *
  159. * @param string $dirName Directory Path and name to be added to the archive.
  160. * @return bool $success
  161. */
  162. public function addEmptyDir($dirName) {
  163. return $this->streamerInstance->addEmptyDir($dirName);
  164. }
  165. /**
  166. * Close the archive.
  167. * A closed archive can no longer have new files added to it. After
  168. * closing, the file is completely written to the output stream.
  169. * @return bool $success
  170. */
  171. public function finalize() {
  172. return $this->streamerInstance->finalize();
  173. }
  174. }