Blame view
sources/apps/files_archive/lib/storage.php
4.73 KB
|
42e4f8d60
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
<?php
/**
* Copyright (c) 2012 Robin Appelman <icewind@owncloud.com>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
namespace OC\Files\Storage;
class Archive extends Common {
/**
* underlying local storage used for missing functions
*
* @var \OC_Archive
*/
private $archive;
private $path;
private static $mounted = array();
private static $enableAutomount = true;
private static $rootView;
private function stripPath($path) { //files should never start with /
if (!$path || $path[0] == '/') {
$path = substr($path, 1);
}
return $path;
}
public function __construct($params) {
$this->archive = \OC_Archive::open($params['archive']);
$this->path = $params['archive'];
}
public function mkdir($path) {
$path = $this->stripPath($path);
return $this->archive->addFolder($path);
}
public function rmdir($path) {
$path = $this->stripPath($path);
return $this->archive->remove($path . '/');
}
public function opendir($path) {
if (substr($path, -1) !== '/') {
$path .= '/';
}
$path = $this->stripPath($path);
$files = $this->archive->getFolder($path);
$content = array();
foreach ($files as $file) {
if (substr($file, -1) == '/') {
$file = substr($file, 0, -1);
}
if ($file and strpos($file, '/') === false) {
$content[] = $file;
}
}
$id = md5($this->path . $path);
\OC\Files\Stream\Dir::register($id, $content);
return opendir('fakedir://' . $id);
}
public function stat($path) {
$ctime = -1;
$path = $this->stripPath($path);
if ($path == '') {
$stat = stat($this->path);
$stat['size'] = 0;
} else {
if ($this->is_dir($path)) {
$stat = array('size' => 0);
$stat['mtime'] = filemtime($this->path);
} else {
$stat = array();
$stat['mtime'] = $this->archive->mtime($path);
$stat['size'] = $this->archive->filesize($path);
if (!$stat['mtime']) {
$stat['mtime'] = time();
}
}
}
$stat['ctime'] = $ctime;
return $stat;
}
public function filetype($path) {
$path = $this->stripPath($path);
if ($path == '') {
return 'dir';
}
if (substr($path, -1) == '/') {
return $this->archive->fileExists($path) ? 'dir' : 'file';
} else {
return $this->archive->fileExists($path . '/') ? 'dir' : 'file';
}
}
public function isReadable($path) {
return is_readable($this->path);
}
public function isUpdatable($path) {
return is_writable($this->path);
}
public function file_exists($path) {
$path = $this->stripPath($path);
if ($path == '') {
return file_exists($this->path);
}
return $this->archive->fileExists($path);
}
public function unlink($path) {
$path = $this->stripPath($path);
return $this->archive->remove($path);
}
public function fopen($path, $mode) {
$path = $this->stripPath($path);
return $this->archive->getStream($path, $mode);
}
public function free_space($path) {
return 0;
}
public function touch($path, $mtime = null) {
if (is_null($mtime)) {
$tmpFile = \OCP\Files::tmpFile();
$this->archive->extractFile($path, $tmpFile);
$this->archive->addfile($path, $tmpFile);
return true;
} else {
return false; //not supported
}
}
private function toTmpFile($path) {
$tmpFile = \OCP\Files::tmpFile();
$this->archive->extractFile($path, $tmpFile);
return $tmpFile;
}
public function file_put_contents($path, $data) {
$path = $this->stripPath($path);
return $this->archive->addFile($path, $data);
}
public function file_get_contents($path) {
$path = $this->stripPath($path);
return $this->archive->getFile($path);
}
/**
* automount paths from file hooks
*
* @param array $params
*/
public static function autoMount($params) {
if (!self::$enableAutomount) {
return;
}
$path = $params['path'];
if (!self::$rootView) {
self::$rootView = new \OC_FilesystemView('');
}
self::$enableAutomount=false;//prevent recursion
$supported = array('zip', 'tar.gz', 'tar.bz2', 'tgz');
foreach ($supported as $type) {
$ext = '.' . $type . '/';
if (($pos = strpos(strtolower($path), $ext)) !== false) {
$archive = substr($path, 0, $pos + strlen($ext) - 1);
if (self::$rootView->file_exists($archive) and array_search($archive, self::$mounted) === false) {
$localArchive = self::$rootView->getLocalFile($archive);
\OC\Files\Filesystem::mount('\OC\Files\Storage\Archive', array('archive' => $localArchive), $archive . '/');
self::$mounted[] = $archive;
}
}
}
self::$enableAutomount = true;
}
public function rename($path1, $path2) {
return $this->archive->rename($path1, $path2);
}
public function hasUpdated($path, $time) {
return $this->filemtime($this->path) > $time;
}
public function getId() {
return 'archive::' . md5($this->path);
}
}
|