@@ -31,65 +31,65 @@ |
||
| 31 | 31 | use OCP\Files\Cache\ICacheEntry; |
| 32 | 32 | |
| 33 | 33 | class HomeCache extends Cache { |
| 34 | - /** |
|
| 35 | - * get the size of a folder and set it in the cache |
|
| 36 | - * |
|
| 37 | - * @param string $path |
|
| 38 | - * @param array $entry (optional) meta data of the folder |
|
| 39 | - * @return int |
|
| 40 | - */ |
|
| 41 | - public function calculateFolderSize($path, $entry = null) { |
|
| 42 | - if ($path !== '/' and $path !== '' and $path !== 'files' and $path !== 'files_trashbin' and $path !== 'files_versions') { |
|
| 43 | - return parent::calculateFolderSize($path, $entry); |
|
| 44 | - } elseif ($path === '' or $path === '/') { |
|
| 45 | - // since the size of / isn't used (the size of /files is used instead) there is no use in calculating it |
|
| 46 | - return 0; |
|
| 47 | - } |
|
| 34 | + /** |
|
| 35 | + * get the size of a folder and set it in the cache |
|
| 36 | + * |
|
| 37 | + * @param string $path |
|
| 38 | + * @param array $entry (optional) meta data of the folder |
|
| 39 | + * @return int |
|
| 40 | + */ |
|
| 41 | + public function calculateFolderSize($path, $entry = null) { |
|
| 42 | + if ($path !== '/' and $path !== '' and $path !== 'files' and $path !== 'files_trashbin' and $path !== 'files_versions') { |
|
| 43 | + return parent::calculateFolderSize($path, $entry); |
|
| 44 | + } elseif ($path === '' or $path === '/') { |
|
| 45 | + // since the size of / isn't used (the size of /files is used instead) there is no use in calculating it |
|
| 46 | + return 0; |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | - $totalSize = 0; |
|
| 50 | - if (is_null($entry)) { |
|
| 51 | - $entry = $this->get($path); |
|
| 52 | - } |
|
| 53 | - if ($entry && $entry['mimetype'] === 'httpd/unix-directory') { |
|
| 54 | - $id = $entry['fileid']; |
|
| 49 | + $totalSize = 0; |
|
| 50 | + if (is_null($entry)) { |
|
| 51 | + $entry = $this->get($path); |
|
| 52 | + } |
|
| 53 | + if ($entry && $entry['mimetype'] === 'httpd/unix-directory') { |
|
| 54 | + $id = $entry['fileid']; |
|
| 55 | 55 | |
| 56 | - $query = $this->connection->getQueryBuilder(); |
|
| 57 | - $query->selectAlias($query->func()->sum('size'), 'f1') |
|
| 58 | - ->from('filecache') |
|
| 59 | - ->where($query->expr()->eq('parent', $query->createNamedParameter($id))) |
|
| 60 | - ->andWhere($query->expr()->eq('storage', $query->createNamedParameter($this->getNumericStorageId()))) |
|
| 61 | - ->andWhere($query->expr()->gte('size', $query->createNamedParameter(0))); |
|
| 56 | + $query = $this->connection->getQueryBuilder(); |
|
| 57 | + $query->selectAlias($query->func()->sum('size'), 'f1') |
|
| 58 | + ->from('filecache') |
|
| 59 | + ->where($query->expr()->eq('parent', $query->createNamedParameter($id))) |
|
| 60 | + ->andWhere($query->expr()->eq('storage', $query->createNamedParameter($this->getNumericStorageId()))) |
|
| 61 | + ->andWhere($query->expr()->gte('size', $query->createNamedParameter(0))); |
|
| 62 | 62 | |
| 63 | - $result = $query->execute(); |
|
| 64 | - $row = $result->fetch(); |
|
| 65 | - $result->closeCursor(); |
|
| 63 | + $result = $query->execute(); |
|
| 64 | + $row = $result->fetch(); |
|
| 65 | + $result->closeCursor(); |
|
| 66 | 66 | |
| 67 | - if ($row) { |
|
| 68 | - list($sum) = array_values($row); |
|
| 69 | - $totalSize = 0 + $sum; |
|
| 70 | - $entry['size'] += 0; |
|
| 71 | - if ($entry['size'] !== $totalSize) { |
|
| 72 | - $this->update($id, ['size' => $totalSize]); |
|
| 73 | - } |
|
| 74 | - } |
|
| 75 | - } |
|
| 76 | - return $totalSize; |
|
| 77 | - } |
|
| 67 | + if ($row) { |
|
| 68 | + list($sum) = array_values($row); |
|
| 69 | + $totalSize = 0 + $sum; |
|
| 70 | + $entry['size'] += 0; |
|
| 71 | + if ($entry['size'] !== $totalSize) { |
|
| 72 | + $this->update($id, ['size' => $totalSize]); |
|
| 73 | + } |
|
| 74 | + } |
|
| 75 | + } |
|
| 76 | + return $totalSize; |
|
| 77 | + } |
|
| 78 | 78 | |
| 79 | - /** |
|
| 80 | - * @param string $path |
|
| 81 | - * @return ICacheEntry |
|
| 82 | - */ |
|
| 83 | - public function get($path) { |
|
| 84 | - $data = parent::get($path); |
|
| 85 | - if ($path === '' or $path === '/') { |
|
| 86 | - // only the size of the "files" dir counts |
|
| 87 | - $filesData = parent::get('files'); |
|
| 79 | + /** |
|
| 80 | + * @param string $path |
|
| 81 | + * @return ICacheEntry |
|
| 82 | + */ |
|
| 83 | + public function get($path) { |
|
| 84 | + $data = parent::get($path); |
|
| 85 | + if ($path === '' or $path === '/') { |
|
| 86 | + // only the size of the "files" dir counts |
|
| 87 | + $filesData = parent::get('files'); |
|
| 88 | 88 | |
| 89 | - if (isset($filesData['size'])) { |
|
| 90 | - $data['size'] = $filesData['size']; |
|
| 91 | - } |
|
| 92 | - } |
|
| 93 | - return $data; |
|
| 94 | - } |
|
| 89 | + if (isset($filesData['size'])) { |
|
| 90 | + $data['size'] = $filesData['size']; |
|
| 91 | + } |
|
| 92 | + } |
|
| 93 | + return $data; |
|
| 94 | + } |
|
| 95 | 95 | } |
@@ -44,176 +44,176 @@ |
||
| 44 | 44 | * @package OC\Files\Cache |
| 45 | 45 | */ |
| 46 | 46 | class Storage { |
| 47 | - /** @var StorageGlobal|null */ |
|
| 48 | - private static $globalCache = null; |
|
| 49 | - private $storageId; |
|
| 50 | - private $numericId; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * @return StorageGlobal |
|
| 54 | - */ |
|
| 55 | - public static function getGlobalCache() { |
|
| 56 | - if (is_null(self::$globalCache)) { |
|
| 57 | - self::$globalCache = new StorageGlobal(\OC::$server->getDatabaseConnection()); |
|
| 58 | - } |
|
| 59 | - return self::$globalCache; |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * @param \OC\Files\Storage\Storage|string $storage |
|
| 64 | - * @param bool $isAvailable |
|
| 65 | - * @throws \RuntimeException |
|
| 66 | - */ |
|
| 67 | - public function __construct($storage, $isAvailable = true) { |
|
| 68 | - if ($storage instanceof IStorage) { |
|
| 69 | - $this->storageId = $storage->getId(); |
|
| 70 | - } else { |
|
| 71 | - $this->storageId = $storage; |
|
| 72 | - } |
|
| 73 | - $this->storageId = self::adjustStorageId($this->storageId); |
|
| 74 | - |
|
| 75 | - if ($row = self::getStorageById($this->storageId)) { |
|
| 76 | - $this->numericId = (int)$row['numeric_id']; |
|
| 77 | - } else { |
|
| 78 | - $connection = \OC::$server->getDatabaseConnection(); |
|
| 79 | - $available = $isAvailable ? 1 : 0; |
|
| 80 | - if ($connection->insertIfNotExist('*PREFIX*storages', ['id' => $this->storageId, 'available' => $available])) { |
|
| 81 | - $this->numericId = (int)$connection->lastInsertId('*PREFIX*storages'); |
|
| 82 | - } else { |
|
| 83 | - if ($row = self::getStorageById($this->storageId)) { |
|
| 84 | - $this->numericId = (int)$row['numeric_id']; |
|
| 85 | - } else { |
|
| 86 | - throw new \RuntimeException('Storage could neither be inserted nor be selected from the database: ' . $this->storageId); |
|
| 87 | - } |
|
| 88 | - } |
|
| 89 | - } |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * @param string $storageId |
|
| 94 | - * @return array |
|
| 95 | - */ |
|
| 96 | - public static function getStorageById($storageId) { |
|
| 97 | - return self::getGlobalCache()->getStorageInfo($storageId); |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * Adjusts the storage id to use md5 if too long |
|
| 102 | - * @param string $storageId storage id |
|
| 103 | - * @return string unchanged $storageId if its length is less than 64 characters, |
|
| 104 | - * else returns the md5 of $storageId |
|
| 105 | - */ |
|
| 106 | - public static function adjustStorageId($storageId) { |
|
| 107 | - if (strlen($storageId) > 64) { |
|
| 108 | - return md5($storageId); |
|
| 109 | - } |
|
| 110 | - return $storageId; |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * Get the numeric id for the storage |
|
| 115 | - * |
|
| 116 | - * @return int |
|
| 117 | - */ |
|
| 118 | - public function getNumericId() { |
|
| 119 | - return $this->numericId; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * Get the string id for the storage |
|
| 124 | - * |
|
| 125 | - * @param int $numericId |
|
| 126 | - * @return string|null either the storage id string or null if the numeric id is not known |
|
| 127 | - */ |
|
| 128 | - public static function getStorageId($numericId) { |
|
| 129 | - $query = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
| 130 | - $query->select('id') |
|
| 131 | - ->from('storages') |
|
| 132 | - ->where($query->expr()->eq('numeric_id', $query->createNamedParameter($numericId))); |
|
| 133 | - $result = $query->execute(); |
|
| 134 | - $row = $result->fetch(); |
|
| 135 | - $result->closeCursor(); |
|
| 136 | - if ($row) { |
|
| 137 | - return $row['id']; |
|
| 138 | - } else { |
|
| 139 | - return null; |
|
| 140 | - } |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - /** |
|
| 144 | - * Get the numeric of the storage with the provided string id |
|
| 145 | - * |
|
| 146 | - * @param $storageId |
|
| 147 | - * @return int|null either the numeric storage id or null if the storage id is not knwon |
|
| 148 | - */ |
|
| 149 | - public static function getNumericStorageId($storageId) { |
|
| 150 | - $storageId = self::adjustStorageId($storageId); |
|
| 151 | - |
|
| 152 | - if ($row = self::getStorageById($storageId)) { |
|
| 153 | - return (int)$row['numeric_id']; |
|
| 154 | - } else { |
|
| 155 | - return null; |
|
| 156 | - } |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - /** |
|
| 160 | - * @return array|null [ available, last_checked ] |
|
| 161 | - */ |
|
| 162 | - public function getAvailability() { |
|
| 163 | - if ($row = self::getStorageById($this->storageId)) { |
|
| 164 | - return [ |
|
| 165 | - 'available' => (int)$row['available'] === 1, |
|
| 166 | - 'last_checked' => $row['last_checked'] |
|
| 167 | - ]; |
|
| 168 | - } else { |
|
| 169 | - return null; |
|
| 170 | - } |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - /** |
|
| 174 | - * @param bool $isAvailable |
|
| 175 | - * @param int $delay amount of seconds to delay reconsidering that storage further |
|
| 176 | - */ |
|
| 177 | - public function setAvailability($isAvailable, int $delay = 0) { |
|
| 178 | - $available = $isAvailable ? 1 : 0; |
|
| 179 | - |
|
| 180 | - $query = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
| 181 | - $query->update('storages') |
|
| 182 | - ->set('available', $query->createNamedParameter($available)) |
|
| 183 | - ->set('last_checked', $query->createNamedParameter(time() + $delay)) |
|
| 184 | - ->where($query->expr()->eq('id', $query->createNamedParameter($this->storageId))); |
|
| 185 | - $query->execute(); |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - /** |
|
| 189 | - * Check if a string storage id is known |
|
| 190 | - * |
|
| 191 | - * @param string $storageId |
|
| 192 | - * @return bool |
|
| 193 | - */ |
|
| 194 | - public static function exists($storageId) { |
|
| 195 | - return !is_null(self::getNumericStorageId($storageId)); |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - /** |
|
| 199 | - * remove the entry for the storage |
|
| 200 | - * |
|
| 201 | - * @param string $storageId |
|
| 202 | - */ |
|
| 203 | - public static function remove($storageId) { |
|
| 204 | - $storageId = self::adjustStorageId($storageId); |
|
| 205 | - $numericId = self::getNumericStorageId($storageId); |
|
| 206 | - |
|
| 207 | - $query = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
| 208 | - $query->delete('storages') |
|
| 209 | - ->where($query->expr()->eq('id', $query->createNamedParameter($storageId))); |
|
| 210 | - $query->execute(); |
|
| 211 | - |
|
| 212 | - if (!is_null($numericId)) { |
|
| 213 | - $query = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
| 214 | - $query->delete('filecache') |
|
| 215 | - ->where($query->expr()->eq('storage', $query->createNamedParameter($numericId))); |
|
| 216 | - $query->execute(); |
|
| 217 | - } |
|
| 218 | - } |
|
| 47 | + /** @var StorageGlobal|null */ |
|
| 48 | + private static $globalCache = null; |
|
| 49 | + private $storageId; |
|
| 50 | + private $numericId; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * @return StorageGlobal |
|
| 54 | + */ |
|
| 55 | + public static function getGlobalCache() { |
|
| 56 | + if (is_null(self::$globalCache)) { |
|
| 57 | + self::$globalCache = new StorageGlobal(\OC::$server->getDatabaseConnection()); |
|
| 58 | + } |
|
| 59 | + return self::$globalCache; |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * @param \OC\Files\Storage\Storage|string $storage |
|
| 64 | + * @param bool $isAvailable |
|
| 65 | + * @throws \RuntimeException |
|
| 66 | + */ |
|
| 67 | + public function __construct($storage, $isAvailable = true) { |
|
| 68 | + if ($storage instanceof IStorage) { |
|
| 69 | + $this->storageId = $storage->getId(); |
|
| 70 | + } else { |
|
| 71 | + $this->storageId = $storage; |
|
| 72 | + } |
|
| 73 | + $this->storageId = self::adjustStorageId($this->storageId); |
|
| 74 | + |
|
| 75 | + if ($row = self::getStorageById($this->storageId)) { |
|
| 76 | + $this->numericId = (int)$row['numeric_id']; |
|
| 77 | + } else { |
|
| 78 | + $connection = \OC::$server->getDatabaseConnection(); |
|
| 79 | + $available = $isAvailable ? 1 : 0; |
|
| 80 | + if ($connection->insertIfNotExist('*PREFIX*storages', ['id' => $this->storageId, 'available' => $available])) { |
|
| 81 | + $this->numericId = (int)$connection->lastInsertId('*PREFIX*storages'); |
|
| 82 | + } else { |
|
| 83 | + if ($row = self::getStorageById($this->storageId)) { |
|
| 84 | + $this->numericId = (int)$row['numeric_id']; |
|
| 85 | + } else { |
|
| 86 | + throw new \RuntimeException('Storage could neither be inserted nor be selected from the database: ' . $this->storageId); |
|
| 87 | + } |
|
| 88 | + } |
|
| 89 | + } |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * @param string $storageId |
|
| 94 | + * @return array |
|
| 95 | + */ |
|
| 96 | + public static function getStorageById($storageId) { |
|
| 97 | + return self::getGlobalCache()->getStorageInfo($storageId); |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * Adjusts the storage id to use md5 if too long |
|
| 102 | + * @param string $storageId storage id |
|
| 103 | + * @return string unchanged $storageId if its length is less than 64 characters, |
|
| 104 | + * else returns the md5 of $storageId |
|
| 105 | + */ |
|
| 106 | + public static function adjustStorageId($storageId) { |
|
| 107 | + if (strlen($storageId) > 64) { |
|
| 108 | + return md5($storageId); |
|
| 109 | + } |
|
| 110 | + return $storageId; |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * Get the numeric id for the storage |
|
| 115 | + * |
|
| 116 | + * @return int |
|
| 117 | + */ |
|
| 118 | + public function getNumericId() { |
|
| 119 | + return $this->numericId; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * Get the string id for the storage |
|
| 124 | + * |
|
| 125 | + * @param int $numericId |
|
| 126 | + * @return string|null either the storage id string or null if the numeric id is not known |
|
| 127 | + */ |
|
| 128 | + public static function getStorageId($numericId) { |
|
| 129 | + $query = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
| 130 | + $query->select('id') |
|
| 131 | + ->from('storages') |
|
| 132 | + ->where($query->expr()->eq('numeric_id', $query->createNamedParameter($numericId))); |
|
| 133 | + $result = $query->execute(); |
|
| 134 | + $row = $result->fetch(); |
|
| 135 | + $result->closeCursor(); |
|
| 136 | + if ($row) { |
|
| 137 | + return $row['id']; |
|
| 138 | + } else { |
|
| 139 | + return null; |
|
| 140 | + } |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + /** |
|
| 144 | + * Get the numeric of the storage with the provided string id |
|
| 145 | + * |
|
| 146 | + * @param $storageId |
|
| 147 | + * @return int|null either the numeric storage id or null if the storage id is not knwon |
|
| 148 | + */ |
|
| 149 | + public static function getNumericStorageId($storageId) { |
|
| 150 | + $storageId = self::adjustStorageId($storageId); |
|
| 151 | + |
|
| 152 | + if ($row = self::getStorageById($storageId)) { |
|
| 153 | + return (int)$row['numeric_id']; |
|
| 154 | + } else { |
|
| 155 | + return null; |
|
| 156 | + } |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + /** |
|
| 160 | + * @return array|null [ available, last_checked ] |
|
| 161 | + */ |
|
| 162 | + public function getAvailability() { |
|
| 163 | + if ($row = self::getStorageById($this->storageId)) { |
|
| 164 | + return [ |
|
| 165 | + 'available' => (int)$row['available'] === 1, |
|
| 166 | + 'last_checked' => $row['last_checked'] |
|
| 167 | + ]; |
|
| 168 | + } else { |
|
| 169 | + return null; |
|
| 170 | + } |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + /** |
|
| 174 | + * @param bool $isAvailable |
|
| 175 | + * @param int $delay amount of seconds to delay reconsidering that storage further |
|
| 176 | + */ |
|
| 177 | + public function setAvailability($isAvailable, int $delay = 0) { |
|
| 178 | + $available = $isAvailable ? 1 : 0; |
|
| 179 | + |
|
| 180 | + $query = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
| 181 | + $query->update('storages') |
|
| 182 | + ->set('available', $query->createNamedParameter($available)) |
|
| 183 | + ->set('last_checked', $query->createNamedParameter(time() + $delay)) |
|
| 184 | + ->where($query->expr()->eq('id', $query->createNamedParameter($this->storageId))); |
|
| 185 | + $query->execute(); |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + /** |
|
| 189 | + * Check if a string storage id is known |
|
| 190 | + * |
|
| 191 | + * @param string $storageId |
|
| 192 | + * @return bool |
|
| 193 | + */ |
|
| 194 | + public static function exists($storageId) { |
|
| 195 | + return !is_null(self::getNumericStorageId($storageId)); |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + /** |
|
| 199 | + * remove the entry for the storage |
|
| 200 | + * |
|
| 201 | + * @param string $storageId |
|
| 202 | + */ |
|
| 203 | + public static function remove($storageId) { |
|
| 204 | + $storageId = self::adjustStorageId($storageId); |
|
| 205 | + $numericId = self::getNumericStorageId($storageId); |
|
| 206 | + |
|
| 207 | + $query = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
| 208 | + $query->delete('storages') |
|
| 209 | + ->where($query->expr()->eq('id', $query->createNamedParameter($storageId))); |
|
| 210 | + $query->execute(); |
|
| 211 | + |
|
| 212 | + if (!is_null($numericId)) { |
|
| 213 | + $query = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
| 214 | + $query->delete('filecache') |
|
| 215 | + ->where($query->expr()->eq('storage', $query->createNamedParameter($numericId))); |
|
| 216 | + $query->execute(); |
|
| 217 | + } |
|
| 218 | + } |
|
| 219 | 219 | } |