You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

190 lines
6.2 KiB

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