Skip to content

Commit 796b4f6

Browse files
author
Robin McCorkell
committed
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.
1 parent 5d8f1a1 commit 796b4f6

File tree

10 files changed

+637
-11
lines changed

10 files changed

+637
-11
lines changed

apps/files_external/lib/config.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,9 @@ private static function getBackendStatus($class, $options, $isPersonal) {
454454
if (class_exists($class)) {
455455
try {
456456
$storage = new $class($options);
457-
return $storage->test($isPersonal);
457+
$result = $storage->test($isPersonal);
458+
$storage->setAvailability($result);
459+
return $result;
458460
} catch (Exception $exception) {
459461
\OCP\Util::logException('files_external', $exception);
460462
return false;

db_structure.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,18 @@
102102
<length>4</length>
103103
</field>
104104

105+
<field>
106+
<name>available</name>
107+
<type>boolean</type>
108+
<default>true</default>
109+
<notnull>true</notnull>
110+
</field>
111+
112+
<field>
113+
<name>last_checked</name>
114+
<type>integer</type>
115+
</field>
116+
105117
<index>
106118
<name>storages_id_index</name>
107119
<unique>true</unique>

lib/private/files/cache/storage.php

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
/**
33
* Copyright (c) 2013 Robin Appelman <[email protected]>
4+
* Copyright (c) 2015 Robin McCorkell <[email protected]>
45
* This file is licensed under the Affero General Public License version 3 or
56
* later.
67
* See the COPYING-README file.
@@ -21,26 +22,35 @@ class Storage {
2122

2223
/**
2324
* @param \OC\Files\Storage\Storage|string $storage
25+
* @param bool $isAvailable
2426
*/
25-
public function __construct($storage) {
27+
public function __construct($storage, $isAvailable = true) {
2628
if ($storage instanceof \OC\Files\Storage\Storage) {
2729
$this->storageId = $storage->getId();
2830
} else {
2931
$this->storageId = $storage;
3032
}
3133
$this->storageId = self::adjustStorageId($this->storageId);
3234

33-
$sql = 'SELECT `numeric_id` FROM `*PREFIX*storages` WHERE `id` = ?';
34-
$result = \OC_DB::executeAudited($sql, array($this->storageId));
35-
if ($row = $result->fetchRow()) {
35+
if ($row = self::getStorageById($this->storageId)) {
3636
$this->numericId = $row['numeric_id'];
3737
} else {
38-
$sql = 'INSERT INTO `*PREFIX*storages` (`id`) VALUES(?)';
39-
\OC_DB::executeAudited($sql, array($this->storageId));
38+
$sql = 'INSERT INTO `*PREFIX*storages` (`id`, `available`) VALUES(?, ?)';
39+
\OC_DB::executeAudited($sql, array($this->storageId, $isAvailable));
4040
$this->numericId = \OC_DB::insertid('*PREFIX*storages');
4141
}
4242
}
4343

44+
/**
45+
* @param string $storageId
46+
* @return array|null
47+
*/
48+
public static function getStorageById($storageId) {
49+
$sql = 'SELECT * FROM `*PREFIX*storages` WHERE `id` = ?';
50+
$result = \OC_DB::executeAudited($sql, array($storageId));
51+
return $result->fetchRow();
52+
}
53+
4454
/**
4555
* Adjusts the storage id to use md5 if too long
4656
* @param string $storageId storage id
@@ -81,15 +91,35 @@ public static function getStorageId($numericId) {
8191
public static function getNumericStorageId($storageId) {
8292
$storageId = self::adjustStorageId($storageId);
8393

84-
$sql = 'SELECT `numeric_id` FROM `*PREFIX*storages` WHERE `id` = ?';
85-
$result = \OC_DB::executeAudited($sql, array($storageId));
86-
if ($row = $result->fetchRow()) {
94+
if ($row = self::getStorageById($storageId)) {
8795
return $row['numeric_id'];
8896
} else {
8997
return null;
9098
}
9199
}
92100

101+
/**
102+
* @return array|null [ available, last_checked ]
103+
*/
104+
public function getAvailability() {
105+
if ($row = self::getStorageById($this->storageId)) {
106+
return [
107+
'available' => $row['available'],
108+
'last_checked' => $row['last_checked']
109+
];
110+
} else {
111+
return null;
112+
}
113+
}
114+
115+
/**
116+
* @param bool $isAvailable
117+
*/
118+
public function setAvailability($isAvailable) {
119+
$sql = 'UPDATE `*PREFIX*storages` SET `available` = ?, `last_checked` = ? WHERE `id` = ?';
120+
\OC_DB::executeAudited($sql, array($isAvailable, time(), $this->storageId));
121+
}
122+
93123
/**
94124
* @param string $storageId
95125
* @return bool

lib/private/files/storage/common.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,4 +451,21 @@ public function getDirectDownload($path) {
451451
return [];
452452
}
453453

454+
/**
455+
* Get availability of the storage
456+
*
457+
* @return array [ available, last_checked ]
458+
*/
459+
public function getAvailability() {
460+
return $this->getStorageCache()->getAvailability();
461+
}
462+
463+
/**
464+
* Set availability of the storage
465+
*
466+
* @param bool $isAvailable
467+
*/
468+
public function setAvailability($isAvailable) {
469+
$this->getStorageCache()->setAvailability($isAvailable);
470+
}
454471
}

0 commit comments

Comments
 (0)