1
0

Streamer.php 6.6 KB

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