Streamer.php 5.9 KB

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