PageRenderTime 493ms CodeModel.GetById 134ms app.highlight 47ms RepoModel.GetById 135ms app.codeStats 0ms

/lib/files/mount/mount.php

https://github.com/sezuan/core
PHP | 141 lines | 116 code | 6 blank | 19 comment | 6 complexity | b09672cefd2193cfc6a4211d8d3600e2 MD5 | raw file
  1<?php
  2/**
  3 * Copyright (c) 2012 Robin Appelman <icewind@owncloud.com>
  4 * This file is licensed under the Affero General Public License version 3 or
  5 * later.
  6 * See the COPYING-README file.
  7 */
  8
  9namespace OC\Files\Mount;
 10
 11use \OC\Files\Filesystem;
 12use OC\Files\Storage\Loader;
 13use OC\Files\Storage\Storage;
 14
 15class Mount {
 16	/**
 17	 * @var \OC\Files\Storage\Storage $storage
 18	 */
 19	private $storage = null;
 20	private $class;
 21	private $storageId;
 22	private $arguments = array();
 23	private $mountPoint;
 24
 25	/**
 26	 * @var \OC\Files\Storage\Loader $loader
 27	 */
 28	private $loader;
 29
 30	/**
 31	 * @param string | \OC\Files\Storage\Storage $storage
 32	 * @param string $mountpoint
 33	 * @param array $arguments (optional)\
 34	 * @param \OC\Files\Storage\Loader $loader
 35	 */
 36	public function __construct($storage, $mountpoint, $arguments = null, $loader = null) {
 37		if (is_null($arguments)) {
 38			$arguments = array();
 39		}
 40		if (is_null($loader)) {
 41			$this->loader = new Loader();
 42		} else {
 43			$this->loader = $loader;
 44		}
 45
 46		$mountpoint = $this->formatPath($mountpoint);
 47		if ($storage instanceof Storage) {
 48			$this->class = get_class($storage);
 49			$this->storage = $this->loader->wrap($mountpoint, $storage);
 50		} else {
 51			// Update old classes to new namespace
 52			if (strpos($storage, 'OC_Filestorage_') !== false) {
 53				$storage = '\OC\Files\Storage\\' . substr($storage, 15);
 54			}
 55			$this->class = $storage;
 56			$this->arguments = $arguments;
 57		}
 58		$this->mountPoint = $mountpoint;
 59	}
 60
 61	/**
 62	 * @return string
 63	 */
 64	public function getMountPoint() {
 65		return $this->mountPoint;
 66	}
 67
 68	/**
 69	 * create the storage that is mounted
 70	 *
 71	 * @return \OC\Files\Storage\Storage
 72	 */
 73	private function createStorage() {
 74		if (class_exists($this->class)) {
 75			try {
 76				return $this->loader->load($this->mountPoint, $this->class, $this->arguments);
 77			} catch (\Exception $exception) {
 78				\OC_Log::write('core', $exception->getMessage(), \OC_Log::ERROR);
 79				return null;
 80			}
 81		} else {
 82			\OC_Log::write('core', 'storage backend ' . $this->class . ' not found', \OC_Log::ERROR);
 83			return null;
 84		}
 85	}
 86
 87	/**
 88	 * @return \OC\Files\Storage\Storage
 89	 */
 90	public function getStorage() {
 91		if (is_null($this->storage)) {
 92			$this->storage = $this->createStorage();
 93		}
 94		return $this->storage;
 95	}
 96
 97	/**
 98	 * @return string
 99	 */
100	public function getStorageId() {
101		if (!$this->storageId) {
102			if (is_null($this->storage)) {
103				$storage = $this->createStorage(); //FIXME: start using exceptions
104				if (is_null($storage)) {
105					return null;
106				}
107				$this->storage = $storage;
108			}
109			$this->storageId = $this->storage->getId();
110			if (strlen($this->storageId) > 64) {
111				$this->storageId = md5($this->storageId);
112			}
113		}
114		return $this->storageId;
115	}
116
117	/**
118	 * @param string $path
119	 * @return string
120	 */
121	public function getInternalPath($path) {
122		if ($this->mountPoint === $path or $this->mountPoint . '/' === $path) {
123			$internalPath = '';
124		} else {
125			$internalPath = substr($path, strlen($this->mountPoint));
126		}
127		return $internalPath;
128	}
129
130	/**
131	 * @param string $path
132	 * @return string
133	 */
134	private function formatPath($path) {
135		$path = Filesystem::normalizePath($path);
136		if (strlen($path) > 1) {
137			$path .= '/';
138		}
139		return $path;
140	}
141}