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
0 commit comments