-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Store storage availability in database
Storage status is saved in the database. Failed storages are rechecked every 10 minutes, while working storages are rechecked every request. Using the files_external app will recheck all external storages when the settings page is viewed, or whenever an external storage is saved.
- Loading branch information
Robin McCorkell
committed
Feb 9, 2015
1 parent
5d8f1a1
commit 796b4f6
Showing
10 changed files
with
637 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
<?php | ||
/** | ||
* Copyright (c) 2013 Robin Appelman <[email protected]> | ||
* Copyright (c) 2015 Robin McCorkell <[email protected]> | ||
* This file is licensed under the Affero General Public License version 3 or | ||
* later. | ||
* See the COPYING-README file. | ||
|
@@ -21,26 +22,35 @@ class Storage { | |
|
||
/** | ||
* @param \OC\Files\Storage\Storage|string $storage | ||
* @param bool $isAvailable | ||
*/ | ||
public function __construct($storage) { | ||
public function __construct($storage, $isAvailable = true) { | ||
if ($storage instanceof \OC\Files\Storage\Storage) { | ||
$this->storageId = $storage->getId(); | ||
} else { | ||
$this->storageId = $storage; | ||
} | ||
$this->storageId = self::adjustStorageId($this->storageId); | ||
|
||
$sql = 'SELECT `numeric_id` FROM `*PREFIX*storages` WHERE `id` = ?'; | ||
$result = \OC_DB::executeAudited($sql, array($this->storageId)); | ||
if ($row = $result->fetchRow()) { | ||
if ($row = self::getStorageById($this->storageId)) { | ||
$this->numericId = $row['numeric_id']; | ||
} else { | ||
$sql = 'INSERT INTO `*PREFIX*storages` (`id`) VALUES(?)'; | ||
\OC_DB::executeAudited($sql, array($this->storageId)); | ||
$sql = 'INSERT INTO `*PREFIX*storages` (`id`, `available`) VALUES(?, ?)'; | ||
\OC_DB::executeAudited($sql, array($this->storageId, $isAvailable)); | ||
$this->numericId = \OC_DB::insertid('*PREFIX*storages'); | ||
} | ||
} | ||
|
||
/** | ||
* @param string $storageId | ||
* @return array|null | ||
*/ | ||
public static function getStorageById($storageId) { | ||
$sql = 'SELECT * FROM `*PREFIX*storages` WHERE `id` = ?'; | ||
$result = \OC_DB::executeAudited($sql, array($storageId)); | ||
return $result->fetchRow(); | ||
} | ||
|
||
/** | ||
* Adjusts the storage id to use md5 if too long | ||
* @param string $storageId storage id | ||
|
@@ -81,15 +91,35 @@ public static function getStorageId($numericId) { | |
public static function getNumericStorageId($storageId) { | ||
$storageId = self::adjustStorageId($storageId); | ||
|
||
$sql = 'SELECT `numeric_id` FROM `*PREFIX*storages` WHERE `id` = ?'; | ||
$result = \OC_DB::executeAudited($sql, array($storageId)); | ||
if ($row = $result->fetchRow()) { | ||
if ($row = self::getStorageById($storageId)) { | ||
return $row['numeric_id']; | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* @return array|null [ available, last_checked ] | ||
*/ | ||
public function getAvailability() { | ||
if ($row = self::getStorageById($this->storageId)) { | ||
return [ | ||
'available' => $row['available'], | ||
'last_checked' => $row['last_checked'] | ||
]; | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* @param bool $isAvailable | ||
*/ | ||
public function setAvailability($isAvailable) { | ||
$sql = 'UPDATE `*PREFIX*storages` SET `available` = ?, `last_checked` = ? WHERE `id` = ?'; | ||
\OC_DB::executeAudited($sql, array($isAvailable, time(), $this->storageId)); | ||
} | ||
|
||
/** | ||
* @param string $storageId | ||
* @return bool | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.