Blame view

sources/lib/private/files/mount/mount.php 3.29 KB
03e52840d   Kload   Init
1
2
3
4
5
6
7
  <?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.
   */
31b7f2792   Kload   Upgrade to ownclo...
8
  namespace OC\Files\Mount;
03e52840d   Kload   Init
9

31b7f2792   Kload   Upgrade to ownclo...
10
11
12
  use \OC\Files\Filesystem;
  use OC\Files\Storage\Loader;
  use OC\Files\Storage\Storage;
03e52840d   Kload   Init
13

31b7f2792   Kload   Upgrade to ownclo...
14
  class Mount {
03e52840d   Kload   Init
15
16
17
18
19
20
21
22
23
24
  	/**
  	 * @var \OC\Files\Storage\Storage $storage
  	 */
  	private $storage = null;
  	private $class;
  	private $storageId;
  	private $arguments = array();
  	private $mountPoint;
  
  	/**
31b7f2792   Kload   Upgrade to ownclo...
25
26
27
28
29
30
  	 * @var \OC\Files\Storage\Loader $loader
  	 */
  	private $loader;
  
  	/**
  	 * @param string | \OC\Files\Storage\Storage $storage
03e52840d   Kload   Init
31
  	 * @param string $mountpoint
31b7f2792   Kload   Upgrade to ownclo...
32
33
  	 * @param array $arguments (optional)\
  	 * @param \OC\Files\Storage\Loader $loader
03e52840d   Kload   Init
34
  	 */
31b7f2792   Kload   Upgrade to ownclo...
35
  	public function __construct($storage, $mountpoint, $arguments = null, $loader = null) {
03e52840d   Kload   Init
36
37
38
  		if (is_null($arguments)) {
  			$arguments = array();
  		}
31b7f2792   Kload   Upgrade to ownclo...
39
40
41
42
43
  		if (is_null($loader)) {
  			$this->loader = new Loader();
  		} else {
  			$this->loader = $loader;
  		}
03e52840d   Kload   Init
44

31b7f2792   Kload   Upgrade to ownclo...
45
46
  		$mountpoint = $this->formatPath($mountpoint);
  		if ($storage instanceof Storage) {
03e52840d   Kload   Init
47
  			$this->class = get_class($storage);
31b7f2792   Kload   Upgrade to ownclo...
48
  			$this->storage = $this->loader->wrap($mountpoint, $storage);
03e52840d   Kload   Init
49
50
51
52
53
54
55
56
57
  		} else {
  			// Update old classes to new namespace
  			if (strpos($storage, 'OC_Filestorage_') !== false) {
  				$storage = '\OC\Files\Storage\\' . substr($storage, 15);
  			}
  			$this->class = $storage;
  			$this->arguments = $arguments;
  		}
  		$this->mountPoint = $mountpoint;
03e52840d   Kload   Init
58
59
60
61
62
63
64
65
66
67
  	}
  
  	/**
  	 * @return string
  	 */
  	public function getMountPoint() {
  		return $this->mountPoint;
  	}
  
  	/**
31b7f2792   Kload   Upgrade to ownclo...
68
69
  	 * create the storage that is mounted
  	 *
03e52840d   Kload   Init
70
71
72
73
74
  	 * @return \OC\Files\Storage\Storage
  	 */
  	private function createStorage() {
  		if (class_exists($this->class)) {
  			try {
31b7f2792   Kload   Upgrade to ownclo...
75
  				return $this->loader->load($this->mountPoint, $this->class, $this->arguments);
03e52840d   Kload   Init
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
  			} catch (\Exception $exception) {
  				\OC_Log::write('core', $exception->getMessage(), \OC_Log::ERROR);
  				return null;
  			}
  		} else {
  			\OC_Log::write('core', 'storage backend ' . $this->class . ' not found', \OC_Log::ERROR);
  			return null;
  		}
  	}
  
  	/**
  	 * @return \OC\Files\Storage\Storage
  	 */
  	public function getStorage() {
  		if (is_null($this->storage)) {
  			$this->storage = $this->createStorage();
  		}
  		return $this->storage;
  	}
  
  	/**
  	 * @return string
  	 */
  	public function getStorageId() {
  		if (!$this->storageId) {
  			if (is_null($this->storage)) {
  				$storage = $this->createStorage(); //FIXME: start using exceptions
  				if (is_null($storage)) {
  					return null;
  				}
  				$this->storage = $storage;
  			}
  			$this->storageId = $this->storage->getId();
  			if (strlen($this->storageId) > 64) {
  				$this->storageId = md5($this->storageId);
  			}
  		}
  		return $this->storageId;
  	}
  
  	/**
  	 * @param string $path
  	 * @return string
  	 */
  	public function getInternalPath($path) {
  		if ($this->mountPoint === $path or $this->mountPoint . '/' === $path) {
  			$internalPath = '';
  		} else {
  			$internalPath = substr($path, strlen($this->mountPoint));
  		}
  		return $internalPath;
  	}
  
  	/**
  	 * @param string $path
  	 * @return string
  	 */
31b7f2792   Kload   Upgrade to ownclo...
133
  	private function formatPath($path) {
03e52840d   Kload   Init
134
135
136
137
138
139
140
141
  		$path = Filesystem::normalizePath($path);
  		if (strlen($path) > 1) {
  			$path .= '/';
  		}
  		return $path;
  	}
  
  	/**
31b7f2792   Kload   Upgrade to ownclo...
142
  	 * @param callable $wrapper
03e52840d   Kload   Init
143
  	 */
31b7f2792   Kload   Upgrade to ownclo...
144
145
  	public function wrapStorage($wrapper) {
  		$this->storage = $wrapper($this->mountPoint, $this->storage);
03e52840d   Kload   Init
146
147
  	}
  }