Streamer.php 6.2 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 $preferTarFor = [ '/macintosh|mac os x/i' ];
  42. // streamer instance
  43. private $streamerInstance;
  44. /**
  45. * Streamer constructor.
  46. *
  47. * @param IRequest $request
  48. * @param int $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 $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. $extension = $this->streamerInstance instanceof ZipStreamer ? '.zip' : '.tar';
  93. $fullName = $name . $extension;
  94. $this->streamerInstance->sendHeaders($fullName);
  95. }
  96. /**
  97. * Stream directory recursively
  98. *
  99. * @throws NotFoundException
  100. * @throws NotPermittedException
  101. * @throws InvalidPathException
  102. */
  103. public function addDirRecursive(string $dir, string $internalDir = ''): void {
  104. $dirname = basename($dir);
  105. $rootDir = $internalDir . $dirname;
  106. if (!empty($rootDir)) {
  107. $this->streamerInstance->addEmptyDir($rootDir);
  108. }
  109. $internalDir .= $dirname . '/';
  110. // prevent absolute dirs
  111. $internalDir = ltrim($internalDir, '/');
  112. $userFolder = \OC::$server->getRootFolder()->get(Filesystem::getRoot());
  113. /** @var Folder $dirNode */
  114. $dirNode = $userFolder->get($dir);
  115. $files = $dirNode->getDirectoryListing();
  116. foreach ($files as $file) {
  117. if ($file instanceof File) {
  118. try {
  119. $fh = $file->fopen('r');
  120. } catch (NotPermittedException $e) {
  121. continue;
  122. }
  123. $this->addFileFromStream(
  124. $fh,
  125. $internalDir . $file->getName(),
  126. $file->getSize(),
  127. $file->getMTime()
  128. );
  129. fclose($fh);
  130. } elseif ($file instanceof Folder) {
  131. if ($file->isReadable()) {
  132. $this->addDirRecursive($dir . '/' . $file->getName(), $internalDir);
  133. }
  134. }
  135. }
  136. }
  137. /**
  138. * Add a file to the archive at the specified location and file name.
  139. *
  140. * @param string $stream Stream to read data from
  141. * @param string $internalName Filepath and name to be used in the archive.
  142. * @param int $size Filesize
  143. * @param int|bool $time File mtime as int, or false
  144. * @return bool $success
  145. */
  146. public function addFileFromStream($stream, $internalName, $size, $time) {
  147. $options = [];
  148. if ($time) {
  149. $options = [
  150. 'timestamp' => $time
  151. ];
  152. }
  153. if ($this->streamerInstance instanceof ZipStreamer) {
  154. return $this->streamerInstance->addFileFromStream($stream, $internalName, $options);
  155. } else {
  156. return $this->streamerInstance->addFileFromStream($stream, $internalName, $size, $options);
  157. }
  158. }
  159. /**
  160. * Add an empty directory entry to the archive.
  161. *
  162. * @param string $dirName Directory Path and name to be added to the archive.
  163. * @return bool $success
  164. */
  165. public function addEmptyDir($dirName) {
  166. return $this->streamerInstance->addEmptyDir($dirName);
  167. }
  168. /**
  169. * Close the archive.
  170. * A closed archive can no longer have new files added to it. After
  171. * closing, the file is completely written to the output stream.
  172. * @return bool $success
  173. */
  174. public function finalize() {
  175. return $this->streamerInstance->finalize();
  176. }
  177. }