1
0

Streamer.php 5.7 KB

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