1<?php
2
3declare(strict_types=1);
4
5/**
6 * @copyright Copyright (c) 2020, Roeland Jago Douma <roeland@famdouma.nl>
7 *
8 * @author Morris Jobke <hey@morrisjobke.de>
9 * @author Roeland Jago Douma <roeland@famdouma.nl>
10 *
11 * @license GNU AGPL version 3 or any later version
12 *
13 * This program is free software: you can redistribute it and/or modify
14 * it under the terms of the GNU Affero General Public License as
15 * published by the Free Software Foundation, either version 3 of the
16 * License, or (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Affero General Public License for more details.
22 *
23 * You should have received a copy of the GNU Affero General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 *
26 */
27namespace OC\Preview\Storage;
28
29use OC\Files\AppData\AppData;
30use OC\SystemConfig;
31use OCP\Files\IRootFolder;
32use OCP\Files\NotFoundException;
33use OCP\Files\SimpleFS\ISimpleFolder;
34
35class Root extends AppData {
36	private $isMultibucketPreviewDistributionEnabled = false;
37	public function __construct(IRootFolder $rootFolder, SystemConfig $systemConfig) {
38		parent::__construct($rootFolder, $systemConfig, 'preview');
39
40		$this->isMultibucketPreviewDistributionEnabled = $systemConfig->getValue('objectstore.multibucket.preview-distribution', false) === true;
41	}
42
43
44	public function getFolder(string $name): ISimpleFolder {
45		$internalFolder = self::getInternalFolder($name);
46
47		try {
48			return parent::getFolder($internalFolder);
49		} catch (NotFoundException $e) {
50			/*
51			 * The new folder structure is not found.
52			 * Lets try the old one
53			 */
54		}
55
56		try {
57			return parent::getFolder($name);
58		} catch (NotFoundException $e) {
59			/*
60			 * The old folder structure is not found.
61			 * Lets try the multibucket fallback if available
62			 */
63			if ($this->isMultibucketPreviewDistributionEnabled) {
64				return parent::getFolder('old-multibucket/' . $internalFolder);
65			}
66
67			// when there is no further fallback just throw the exception
68			throw $e;
69		}
70	}
71
72	public function newFolder(string $name): ISimpleFolder {
73		$internalFolder = self::getInternalFolder($name);
74		return parent::newFolder($internalFolder);
75	}
76
77	/*
78	 * Do not allow directory listing on this special root
79	 * since it gets to big and time consuming
80	 */
81	public function getDirectoryListing(): array {
82		return [];
83	}
84
85	public static function getInternalFolder(string $name): string {
86		return implode('/', str_split(substr(md5($name), 0, 7))) . '/' . $name;
87	}
88}
89