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.

Streamer.php 5.4KB

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