Copy of Nextcloud richdocuments app, but with modifications so all traffic goes over clearnet/CJDNS/Tor/I2P/whatever instead of having a single default hostname.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

105 lines
2.9 KiB

  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Lukas Reschke <lukas@statuscode.ch>
  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. namespace OCA\Richdocuments\WOPI;
  22. use OCP\AppFramework\Utility\ITimeFactory;
  23. use OCP\Files\IAppData;
  24. use OCP\Files\NotFoundException;
  25. use OCP\Files\SimpleFS\ISimpleFolder;
  26. use OCP\Http\Client\IClientService;
  27. use OCP\IConfig;
  28. use OCP\IL10N;
  29. use OCP\Notification\IApp;
  30. class DiscoveryManager {
  31. /** @var IClientService */
  32. private $clientService;
  33. /** @var ISimpleFolder */
  34. private $appData;
  35. /** @var IConfig */
  36. private $config;
  37. /** @var IL10N */
  38. private $l10n;
  39. /** @var ITimeFactory */
  40. private $timeFactory;
  41. /**
  42. * @param IClientService $clientService
  43. * @param IAppData $appData
  44. * @param IConfig $config
  45. * @param IL10N $l10n
  46. * @param ITimeFactory $timeFactory
  47. */
  48. public function __construct(IClientService $clientService,
  49. IAppData $appData,
  50. IConfig $config,
  51. IL10N $l10n,
  52. ITimeFactory $timeFactory) {
  53. $this->clientService = $clientService;
  54. try {
  55. $this->appData = $appData->getFolder('richdocuments');
  56. } catch (NotFoundException $e) {
  57. $this->appData = $appData->newFolder('richdocuments');
  58. }
  59. $this->config = $config;
  60. $this->timeFactory = $timeFactory;
  61. }
  62. public function get() {
  63. // First check if there is a local valid discovery file
  64. try {
  65. $file = $this->appData->getFile('discovery.xml');
  66. $decodedFile = json_decode($file->getContent(), true);
  67. if($decodedFile['timestamp'] + 3600 > $this->timeFactory->getTime()) {
  68. return $decodedFile['data'];
  69. }
  70. } catch (NotFoundException $e) {
  71. $file = $this->appData->newFile('discovery.xml');
  72. }
  73. $remoteHost = $this->config->getAppValue('richdocuments', 'wopi_url');
  74. $wopiDiscovery = $remoteHost . '/hosting/discovery';
  75. $client = $this->clientService->newClient();
  76. try {
  77. $response = $client->get($wopiDiscovery);
  78. } catch (\Exception $e) {
  79. throw $e;
  80. }
  81. $responseBody = $response->getBody();
  82. $file->putContent(
  83. json_encode([
  84. 'data' => $responseBody,
  85. 'timestamp' => $this->timeFactory->getTime(),
  86. ])
  87. );
  88. return $responseBody;
  89. }
  90. public function refretch() {
  91. try {
  92. $this->appData->getFile('discovery.xml')->delete();
  93. } catch(\Exception $e) {}
  94. }
  95. }