|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2016 Lukas Reschke <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* @license GNU AGPL version 3 or any later version |
|
6
|
|
|
* |
|
7
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
8
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
9
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
10
|
|
|
* License, or (at your option) any later version. |
|
11
|
|
|
* |
|
12
|
|
|
* This program is distributed in the hope that it will be useful, |
|
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15
|
|
|
* GNU Affero General Public License for more details. |
|
16
|
|
|
* |
|
17
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
18
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
19
|
|
|
* |
|
20
|
|
|
*/ |
|
21
|
|
|
|
|
22
|
|
|
namespace OC\App\AppStore\Fetcher; |
|
23
|
|
|
|
|
24
|
|
|
use OCP\AppFramework\Utility\ITimeFactory; |
|
25
|
|
|
use OCP\Files\IAppData; |
|
26
|
|
|
use OCP\Files\NotFoundException; |
|
27
|
|
|
use OCP\Http\Client\IClientService; |
|
28
|
|
|
use OCP\IConfig; |
|
29
|
|
|
|
|
30
|
|
|
abstract class Fetcher { |
|
31
|
|
|
const INVALIDATE_AFTER_SECONDS = 300; |
|
32
|
|
|
|
|
33
|
|
|
/** @var IAppData */ |
|
34
|
|
|
protected $appData; |
|
35
|
|
|
/** @var IClientService */ |
|
36
|
|
|
protected $clientService; |
|
37
|
|
|
/** @var ITimeFactory */ |
|
38
|
|
|
protected $timeFactory; |
|
39
|
|
|
/** @var IConfig */ |
|
40
|
|
|
protected $config; |
|
41
|
|
|
/** @var string */ |
|
42
|
|
|
protected $fileName; |
|
43
|
|
|
/** @var string */ |
|
44
|
|
|
protected $endpointUrl; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param IAppData $appData |
|
48
|
|
|
* @param IClientService $clientService |
|
49
|
|
|
* @param ITimeFactory $timeFactory |
|
50
|
|
|
* @param IConfig $config |
|
51
|
|
|
*/ |
|
52
|
|
|
public function __construct(IAppData $appData, |
|
53
|
|
|
IClientService $clientService, |
|
54
|
|
|
ITimeFactory $timeFactory, |
|
55
|
|
|
IConfig $config) { |
|
56
|
|
|
$this->appData = $appData; |
|
57
|
|
|
$this->clientService = $clientService; |
|
58
|
|
|
$this->timeFactory = $timeFactory; |
|
59
|
|
|
$this->config = $config; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Fetches the response from the server |
|
64
|
|
|
* |
|
65
|
|
|
* @return array |
|
66
|
|
|
*/ |
|
67
|
|
|
protected function fetch() { |
|
68
|
|
|
$client = $this->clientService->newClient(); |
|
69
|
|
|
$response = $client->get($this->endpointUrl); |
|
70
|
|
|
$responseJson = []; |
|
71
|
|
|
$responseJson['data'] = json_decode($response->getBody(), true); |
|
72
|
|
|
$responseJson['timestamp'] = $this->timeFactory->getTime(); |
|
73
|
|
|
$responseJson['ncversion'] = $this->config->getSystemValue('version'); |
|
74
|
|
|
return $responseJson; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Returns the array with the categories on the appstore server |
|
79
|
|
|
* |
|
80
|
|
|
* @return array |
|
81
|
|
|
*/ |
|
82
|
|
|
public function get() { |
|
83
|
|
|
$rootFolder = $this->appData->getFolder('/'); |
|
84
|
|
|
|
|
85
|
|
|
try { |
|
86
|
|
|
// File does already exists |
|
87
|
|
|
$file = $rootFolder->getFile($this->fileName); |
|
88
|
|
|
$jsonBlob = json_decode($file->getContent(), true); |
|
89
|
|
|
if(is_array($jsonBlob)) { |
|
90
|
|
|
/* |
|
91
|
|
|
* If the timestamp is older than 300 seconds request the files new |
|
92
|
|
|
* If the version changed (update!) also refresh |
|
93
|
|
|
*/ |
|
94
|
|
|
if((int)$jsonBlob['timestamp'] > ($this->timeFactory->getTime() - self::INVALIDATE_AFTER_SECONDS) && |
|
95
|
|
|
isset($jsonBlob['ncversion']) && $jsonBlob['ncversion'] === $this->config->getSystemValue('version', '0.0.0')) { |
|
96
|
|
|
return $jsonBlob['data']; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
} catch (NotFoundException $e) { |
|
100
|
|
|
// File does not already exists |
|
101
|
|
|
$file = $rootFolder->newFile($this->fileName); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
// Refresh the file content |
|
105
|
|
|
try { |
|
106
|
|
|
$responseJson = $this->fetch(); |
|
107
|
|
|
$file->putContent(json_encode($responseJson)); |
|
108
|
|
|
return json_decode($file->getContent(), true)['data']; |
|
109
|
|
|
} catch (\Exception $e) { |
|
110
|
|
|
return []; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|