Streamer.php 3.65 KB
Newer Older
Victor Dubiniuk's avatar
Victor Dubiniuk committed
1
2
<?php
/**
Lukas Reschke's avatar
Lukas Reschke committed
3
 * @author Joas Schilling <nickvergessen@owncloud.com>
4
 * @author Thomas Müller <thomas.mueller@tmit.eu>
Victor Dubiniuk's avatar
Victor Dubiniuk committed
5
6
 * @author Victor Dubiniuk <dubiniuk@owncloud.com>
 *
Thomas Müller's avatar
Thomas Müller committed
7
 * @copyright Copyright (c) 2016, ownCloud, Inc.
Victor Dubiniuk's avatar
Victor Dubiniuk committed
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 * @license AGPL-3.0
 *
 * This code is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License, version 3,
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 */

24
25
namespace OC;

Thomas Müller's avatar
Thomas Müller committed
26
use ownCloud\TarStreamer\TarStreamer;
27
use ZipStreamer\ZipStreamer;
Victor Dubiniuk's avatar
Victor Dubiniuk committed
28

29
class Streamer {
Victor Dubiniuk's avatar
Victor Dubiniuk committed
30
31
	// array of regexp. Matching user agents will get tar instead of zip
	private $preferTarFor = [ '/macintosh|mac os x/i' ];
Victor Dubiniuk's avatar
Victor Dubiniuk committed
32

Victor Dubiniuk's avatar
Victor Dubiniuk committed
33
	// streamer instance
Victor Dubiniuk's avatar
Victor Dubiniuk committed
34
	private $streamerInstance;
Victor Dubiniuk's avatar
Victor Dubiniuk committed
35
	
Victor Dubiniuk's avatar
Victor Dubiniuk committed
36
	public function __construct(){
Victor Dubiniuk's avatar
Victor Dubiniuk committed
37
38
39
		/** @var \OCP\IRequest */
		$request = \OC::$server->getRequest();
		
40
		if ($request->isUserAgent($this->preferTarFor)) {
Victor Dubiniuk's avatar
Victor Dubiniuk committed
41
			$this->streamerInstance = new TarStreamer();
Victor Dubiniuk's avatar
Victor Dubiniuk committed
42
		} else {
43
			$this->streamerInstance = new ZipStreamer(['zip64' => PHP_INT_SIZE !== 4]);
Victor Dubiniuk's avatar
Victor Dubiniuk committed
44
45
46
		}
	}
	
Victor Dubiniuk's avatar
Victor Dubiniuk committed
47
48
	/**
	 * Send HTTP headers
Victor Dubiniuk's avatar
Victor Dubiniuk committed
49
	 * @param string $name 
Victor Dubiniuk's avatar
Victor Dubiniuk committed
50
	 */
Victor Dubiniuk's avatar
Victor Dubiniuk committed
51
52
	public function sendHeaders($name){
		$extension = $this->streamerInstance instanceof ZipStreamer ? '.zip' : '.tar';
53
54
		$fullName = $name . $extension;
		$this->streamerInstance->sendHeaders($fullName);
Victor Dubiniuk's avatar
Victor Dubiniuk committed
55
56
57
	}
	
	/**
Victor Dubiniuk's avatar
Victor Dubiniuk committed
58
	 * Stream directory recursively
Victor Dubiniuk's avatar
Victor Dubiniuk committed
59
60
61
	 * @param string $dir
	 * @param string $internalDir
	 */
Victor Dubiniuk's avatar
Victor Dubiniuk committed
62
	public function addDirRecursive($dir, $internalDir='') {
Victor Dubiniuk's avatar
Victor Dubiniuk committed
63
64
65
66
67
		$dirname = basename($dir);
		$rootDir = $internalDir . $dirname;
		if (!empty($rootDir)) {
			$this->streamerInstance->addEmptyDir($rootDir);
		}
Victor Dubiniuk's avatar
Victor Dubiniuk committed
68
		$internalDir .= $dirname . '/';
Victor Dubiniuk's avatar
Victor Dubiniuk committed
69
70
71
72
73
74
75
76
77
78
		// prevent absolute dirs
		$internalDir = ltrim($internalDir, '/');

		$files= \OC\Files\Filesystem::getDirectoryContent($dir);
		foreach($files as $file) {
			$filename = $file['name'];
			$file = $dir . '/' . $filename;
			if(\OC\Files\Filesystem::is_file($file)) {
				$filesize = \OC\Files\Filesystem::filesize($file);
				$fh = \OC\Files\Filesystem::fopen($file, 'r');
79
				$this->addFileFromStream($fh, $internalDir . $filename, $filesize);
Victor Dubiniuk's avatar
Victor Dubiniuk committed
80
81
				fclose($fh);
			}elseif(\OC\Files\Filesystem::is_dir($file)) {
Victor Dubiniuk's avatar
Victor Dubiniuk committed
82
				$this->addDirRecursive($file, $internalDir);
Victor Dubiniuk's avatar
Victor Dubiniuk committed
83
84
85
86
			}
		}
	}
	
Victor Dubiniuk's avatar
Victor Dubiniuk committed
87
88
89
	/**
	 * Add a file to the archive at the specified location and file name.
	 *
Thomas Müller's avatar
Thomas Müller committed
90
91
92
	 * @param string $stream Stream to read data from
	 * @param string $internalName Filepath and name to be used in the archive.
	 * @param int $size Filesize
Victor Dubiniuk's avatar
Victor Dubiniuk committed
93
94
95
	 * @return bool $success
	 */
	public function addFileFromStream($stream, $internalName, $size){
Victor Dubiniuk's avatar
Victor Dubiniuk committed
96
		if ($this->streamerInstance instanceof ZipStreamer) {
Victor Dubiniuk's avatar
Victor Dubiniuk committed
97
			return $this->streamerInstance->addFileFromStream($stream, $internalName);
Victor Dubiniuk's avatar
Victor Dubiniuk committed
98
		} else {
Victor Dubiniuk's avatar
Victor Dubiniuk committed
99
			return $this->streamerInstance->addFileFromStream($stream, $internalName, $size);
Victor Dubiniuk's avatar
Victor Dubiniuk committed
100
101
		}
	}
Thomas Müller's avatar
Thomas Müller committed
102

Victor Dubiniuk's avatar
Victor Dubiniuk committed
103
104
105
	/**
	 * Add an empty directory entry to the archive.
	 *
Thomas Müller's avatar
Thomas Müller committed
106
	 * @param string $dirName Directory Path and name to be added to the archive.
Victor Dubiniuk's avatar
Victor Dubiniuk committed
107
108
	 * @return bool $success
	 */
Victor Dubiniuk's avatar
Victor Dubiniuk committed
109
110
111
	public function addEmptyDir($dirName){
		return $this->streamerInstance->addEmptyDir($dirName);
	}
Thomas Müller's avatar
Thomas Müller committed
112

Victor Dubiniuk's avatar
Victor Dubiniuk committed
113
114
115
116
117
118
	/**
	 * Close the archive.
	 * A closed archive can no longer have new files added to it. After
	 * closing, the file is completely written to the output stream.
	 * @return bool $success
	 */
Victor Dubiniuk's avatar
Victor Dubiniuk committed
119
120
121
122
	public function finalize(){
		return $this->streamerInstance->finalize();
	}
}