@@ -6,35 +6,35 @@ |
||
6 | 6 | use OCP\ICacheFactory; |
7 | 7 | |
8 | 8 | class IMagickSupport { |
9 | - private ICache $cache; |
|
10 | - private ?\Imagick $imagick; |
|
11 | - |
|
12 | - public function __construct(ICacheFactory $cacheFactory) { |
|
13 | - $this->cache = $cacheFactory->createLocal('imagick'); |
|
14 | - |
|
15 | - if (extension_loaded('imagick')) { |
|
16 | - $this->imagick = new \Imagick(); |
|
17 | - } else { |
|
18 | - $this->imagick = null; |
|
19 | - } |
|
20 | - } |
|
21 | - |
|
22 | - public function hasExtension(): bool { |
|
23 | - return !is_null($this->imagick); |
|
24 | - } |
|
25 | - |
|
26 | - public function supportsFormat(string $format): bool { |
|
27 | - if (is_null($this->imagick)) { |
|
28 | - return false; |
|
29 | - } |
|
30 | - |
|
31 | - $cached = $this->cache->get($format); |
|
32 | - if (!is_null($cached)) { |
|
33 | - return $cached; |
|
34 | - } |
|
35 | - |
|
36 | - $formatSupported = count($this->imagick->queryFormats($format)) === 1; |
|
37 | - $this->cache->set($format, $cached); |
|
38 | - return $formatSupported; |
|
39 | - } |
|
9 | + private ICache $cache; |
|
10 | + private ?\Imagick $imagick; |
|
11 | + |
|
12 | + public function __construct(ICacheFactory $cacheFactory) { |
|
13 | + $this->cache = $cacheFactory->createLocal('imagick'); |
|
14 | + |
|
15 | + if (extension_loaded('imagick')) { |
|
16 | + $this->imagick = new \Imagick(); |
|
17 | + } else { |
|
18 | + $this->imagick = null; |
|
19 | + } |
|
20 | + } |
|
21 | + |
|
22 | + public function hasExtension(): bool { |
|
23 | + return !is_null($this->imagick); |
|
24 | + } |
|
25 | + |
|
26 | + public function supportsFormat(string $format): bool { |
|
27 | + if (is_null($this->imagick)) { |
|
28 | + return false; |
|
29 | + } |
|
30 | + |
|
31 | + $cached = $this->cache->get($format); |
|
32 | + if (!is_null($cached)) { |
|
33 | + return $cached; |
|
34 | + } |
|
35 | + |
|
36 | + $formatSupported = count($this->imagick->queryFormats($format)) === 1; |
|
37 | + $this->cache->set($format, $cached); |
|
38 | + return $formatSupported; |
|
39 | + } |
|
40 | 40 | } |
@@ -26,10 +26,10 @@ |
||
26 | 26 | use Doctrine\DBAL\Logging\DebugStack; |
27 | 27 | |
28 | 28 | class BacktraceDebugStack extends DebugStack { |
29 | - public function startQuery($sql, ?array $params = null, ?array $types = null) { |
|
30 | - parent::startQuery($sql, $params, $types); |
|
31 | - $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); |
|
32 | - $this->queries[$this->currentQuery]['backtrace'] = $backtrace; |
|
33 | - $this->queries[$this->currentQuery]['start'] = $this->start; |
|
34 | - } |
|
29 | + public function startQuery($sql, ?array $params = null, ?array $types = null) { |
|
30 | + parent::startQuery($sql, $params, $types); |
|
31 | + $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); |
|
32 | + $this->queries[$this->currentQuery]['backtrace'] = $backtrace; |
|
33 | + $this->queries[$this->currentQuery]['start'] = $this->start; |
|
34 | + } |
|
35 | 35 | } |
@@ -9,46 +9,46 @@ |
||
9 | 9 | * Wrap a cache instance with an extra later of local, in-memory caching |
10 | 10 | */ |
11 | 11 | class WithLocalCache implements ICache { |
12 | - private ICache $inner; |
|
13 | - private CappedMemoryCache $cached; |
|
14 | - |
|
15 | - public function __construct(ICache $inner, int $localCapacity = 512) { |
|
16 | - $this->inner = $inner; |
|
17 | - $this->cached = new CappedMemoryCache($localCapacity); |
|
18 | - } |
|
19 | - |
|
20 | - public function get($key) { |
|
21 | - if (isset($this->cached[$key])) { |
|
22 | - return $this->cached[$key]; |
|
23 | - } else { |
|
24 | - $value = $this->inner->get($key); |
|
25 | - if (!is_null($value)) { |
|
26 | - $this->cached[$key] = $value; |
|
27 | - } |
|
28 | - return $value; |
|
29 | - } |
|
30 | - } |
|
31 | - |
|
32 | - public function set($key, $value, $ttl = 0) { |
|
33 | - $this->cached[$key] = $value; |
|
34 | - return $this->inner->set($key, $value, $ttl); |
|
35 | - } |
|
36 | - |
|
37 | - public function hasKey($key) { |
|
38 | - return isset($this->cached[$key]) || $this->inner->hasKey($key); |
|
39 | - } |
|
40 | - |
|
41 | - public function remove($key) { |
|
42 | - unset($this->cached[$key]); |
|
43 | - return $this->inner->remove($key); |
|
44 | - } |
|
45 | - |
|
46 | - public function clear($prefix = '') { |
|
47 | - $this->cached->clear(); |
|
48 | - return $this->inner->clear($prefix); |
|
49 | - } |
|
50 | - |
|
51 | - public static function isAvailable(): bool { |
|
52 | - return false; |
|
53 | - } |
|
12 | + private ICache $inner; |
|
13 | + private CappedMemoryCache $cached; |
|
14 | + |
|
15 | + public function __construct(ICache $inner, int $localCapacity = 512) { |
|
16 | + $this->inner = $inner; |
|
17 | + $this->cached = new CappedMemoryCache($localCapacity); |
|
18 | + } |
|
19 | + |
|
20 | + public function get($key) { |
|
21 | + if (isset($this->cached[$key])) { |
|
22 | + return $this->cached[$key]; |
|
23 | + } else { |
|
24 | + $value = $this->inner->get($key); |
|
25 | + if (!is_null($value)) { |
|
26 | + $this->cached[$key] = $value; |
|
27 | + } |
|
28 | + return $value; |
|
29 | + } |
|
30 | + } |
|
31 | + |
|
32 | + public function set($key, $value, $ttl = 0) { |
|
33 | + $this->cached[$key] = $value; |
|
34 | + return $this->inner->set($key, $value, $ttl); |
|
35 | + } |
|
36 | + |
|
37 | + public function hasKey($key) { |
|
38 | + return isset($this->cached[$key]) || $this->inner->hasKey($key); |
|
39 | + } |
|
40 | + |
|
41 | + public function remove($key) { |
|
42 | + unset($this->cached[$key]); |
|
43 | + return $this->inner->remove($key); |
|
44 | + } |
|
45 | + |
|
46 | + public function clear($prefix = '') { |
|
47 | + $this->cached->clear(); |
|
48 | + return $this->inner->clear($prefix); |
|
49 | + } |
|
50 | + |
|
51 | + public static function isAvailable(): bool { |
|
52 | + return false; |
|
53 | + } |
|
54 | 54 | } |
@@ -34,67 +34,67 @@ |
||
34 | 34 | use Sabre\HTTP\RequestInterface; |
35 | 35 | |
36 | 36 | class LockPlugin extends ServerPlugin { |
37 | - /** |
|
38 | - * Reference to main server object |
|
39 | - * |
|
40 | - * @var \Sabre\DAV\Server |
|
41 | - */ |
|
42 | - private $server; |
|
37 | + /** |
|
38 | + * Reference to main server object |
|
39 | + * |
|
40 | + * @var \Sabre\DAV\Server |
|
41 | + */ |
|
42 | + private $server; |
|
43 | 43 | |
44 | - /** |
|
45 | - * State of the lock |
|
46 | - * |
|
47 | - * @var bool |
|
48 | - */ |
|
49 | - private $isLocked; |
|
44 | + /** |
|
45 | + * State of the lock |
|
46 | + * |
|
47 | + * @var bool |
|
48 | + */ |
|
49 | + private $isLocked; |
|
50 | 50 | |
51 | - /** |
|
52 | - * {@inheritdoc} |
|
53 | - */ |
|
54 | - public function initialize(\Sabre\DAV\Server $server) { |
|
55 | - $this->server = $server; |
|
56 | - $this->server->on('beforeMethod:*', [$this, 'getLock'], 50); |
|
57 | - $this->server->on('afterMethod:*', [$this, 'releaseLock'], 50); |
|
58 | - $this->isLocked = false; |
|
59 | - } |
|
51 | + /** |
|
52 | + * {@inheritdoc} |
|
53 | + */ |
|
54 | + public function initialize(\Sabre\DAV\Server $server) { |
|
55 | + $this->server = $server; |
|
56 | + $this->server->on('beforeMethod:*', [$this, 'getLock'], 50); |
|
57 | + $this->server->on('afterMethod:*', [$this, 'releaseLock'], 50); |
|
58 | + $this->isLocked = false; |
|
59 | + } |
|
60 | 60 | |
61 | - public function getLock(RequestInterface $request) { |
|
62 | - // we can't listen on 'beforeMethod:PUT' due to order of operations with setting up the tree |
|
63 | - // so instead we limit ourselves to the PUT method manually |
|
64 | - if ($request->getMethod() !== 'PUT') { |
|
65 | - return; |
|
66 | - } |
|
67 | - try { |
|
68 | - $node = $this->server->tree->getNodeForPath($request->getPath()); |
|
69 | - } catch (NotFound $e) { |
|
70 | - return; |
|
71 | - } |
|
72 | - if ($node instanceof Node) { |
|
73 | - try { |
|
74 | - $node->acquireLock(ILockingProvider::LOCK_SHARED); |
|
75 | - } catch (LockedException $e) { |
|
76 | - throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
|
77 | - } |
|
78 | - $this->isLocked = true; |
|
79 | - } |
|
80 | - } |
|
61 | + public function getLock(RequestInterface $request) { |
|
62 | + // we can't listen on 'beforeMethod:PUT' due to order of operations with setting up the tree |
|
63 | + // so instead we limit ourselves to the PUT method manually |
|
64 | + if ($request->getMethod() !== 'PUT') { |
|
65 | + return; |
|
66 | + } |
|
67 | + try { |
|
68 | + $node = $this->server->tree->getNodeForPath($request->getPath()); |
|
69 | + } catch (NotFound $e) { |
|
70 | + return; |
|
71 | + } |
|
72 | + if ($node instanceof Node) { |
|
73 | + try { |
|
74 | + $node->acquireLock(ILockingProvider::LOCK_SHARED); |
|
75 | + } catch (LockedException $e) { |
|
76 | + throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
|
77 | + } |
|
78 | + $this->isLocked = true; |
|
79 | + } |
|
80 | + } |
|
81 | 81 | |
82 | - public function releaseLock(RequestInterface $request) { |
|
83 | - // don't try to release the lock if we never locked one |
|
84 | - if ($this->isLocked === false) { |
|
85 | - return; |
|
86 | - } |
|
87 | - if ($request->getMethod() !== 'PUT') { |
|
88 | - return; |
|
89 | - } |
|
90 | - try { |
|
91 | - $node = $this->server->tree->getNodeForPath($request->getPath()); |
|
92 | - } catch (NotFound $e) { |
|
93 | - return; |
|
94 | - } |
|
95 | - if ($node instanceof Node) { |
|
96 | - $node->releaseLock(ILockingProvider::LOCK_SHARED); |
|
97 | - $this->isLocked = false; |
|
98 | - } |
|
99 | - } |
|
82 | + public function releaseLock(RequestInterface $request) { |
|
83 | + // don't try to release the lock if we never locked one |
|
84 | + if ($this->isLocked === false) { |
|
85 | + return; |
|
86 | + } |
|
87 | + if ($request->getMethod() !== 'PUT') { |
|
88 | + return; |
|
89 | + } |
|
90 | + try { |
|
91 | + $node = $this->server->tree->getNodeForPath($request->getPath()); |
|
92 | + } catch (NotFound $e) { |
|
93 | + return; |
|
94 | + } |
|
95 | + if ($node instanceof Node) { |
|
96 | + $node->releaseLock(ILockingProvider::LOCK_SHARED); |
|
97 | + $this->isLocked = false; |
|
98 | + } |
|
99 | + } |
|
100 | 100 | } |
@@ -32,63 +32,63 @@ |
||
32 | 32 | * @since 26.0.0 |
33 | 33 | */ |
34 | 34 | class BeforeGroupChangedEvent extends Event { |
35 | - private IGroup $group; |
|
36 | - private string $feature; |
|
37 | - /** @var mixed */ |
|
38 | - private $value; |
|
39 | - /** @var mixed */ |
|
40 | - private $oldValue; |
|
35 | + private IGroup $group; |
|
36 | + private string $feature; |
|
37 | + /** @var mixed */ |
|
38 | + private $value; |
|
39 | + /** @var mixed */ |
|
40 | + private $oldValue; |
|
41 | 41 | |
42 | - /** |
|
43 | - * @since 26.0.0 |
|
44 | - */ |
|
45 | - public function __construct(IGroup $group, |
|
46 | - string $feature, |
|
47 | - $value, |
|
48 | - $oldValue = null) { |
|
49 | - parent::__construct(); |
|
50 | - $this->group = $group; |
|
51 | - $this->feature = $feature; |
|
52 | - $this->value = $value; |
|
53 | - $this->oldValue = $oldValue; |
|
54 | - } |
|
42 | + /** |
|
43 | + * @since 26.0.0 |
|
44 | + */ |
|
45 | + public function __construct(IGroup $group, |
|
46 | + string $feature, |
|
47 | + $value, |
|
48 | + $oldValue = null) { |
|
49 | + parent::__construct(); |
|
50 | + $this->group = $group; |
|
51 | + $this->feature = $feature; |
|
52 | + $this->value = $value; |
|
53 | + $this->oldValue = $oldValue; |
|
54 | + } |
|
55 | 55 | |
56 | - /** |
|
57 | - * |
|
58 | - * @since 26.0.0 |
|
59 | - * |
|
60 | - * @return IGroup |
|
61 | - */ |
|
62 | - public function getGroup(): IGroup { |
|
63 | - return $this->group; |
|
64 | - } |
|
56 | + /** |
|
57 | + * |
|
58 | + * @since 26.0.0 |
|
59 | + * |
|
60 | + * @return IGroup |
|
61 | + */ |
|
62 | + public function getGroup(): IGroup { |
|
63 | + return $this->group; |
|
64 | + } |
|
65 | 65 | |
66 | - /** |
|
67 | - * |
|
68 | - * @since 26.0.0 |
|
69 | - * |
|
70 | - * @return string |
|
71 | - */ |
|
72 | - public function getFeature(): string { |
|
73 | - return $this->feature; |
|
74 | - } |
|
66 | + /** |
|
67 | + * |
|
68 | + * @since 26.0.0 |
|
69 | + * |
|
70 | + * @return string |
|
71 | + */ |
|
72 | + public function getFeature(): string { |
|
73 | + return $this->feature; |
|
74 | + } |
|
75 | 75 | |
76 | - /** |
|
77 | - * @since 26.0.0 |
|
78 | - * |
|
79 | - * @return mixed |
|
80 | - */ |
|
81 | - public function getValue() { |
|
82 | - return $this->value; |
|
83 | - } |
|
76 | + /** |
|
77 | + * @since 26.0.0 |
|
78 | + * |
|
79 | + * @return mixed |
|
80 | + */ |
|
81 | + public function getValue() { |
|
82 | + return $this->value; |
|
83 | + } |
|
84 | 84 | |
85 | - /** |
|
86 | - * |
|
87 | - * @since 26.0.0 |
|
88 | - * |
|
89 | - * @return mixed |
|
90 | - */ |
|
91 | - public function getOldValue() { |
|
92 | - return $this->oldValue; |
|
93 | - } |
|
85 | + /** |
|
86 | + * |
|
87 | + * @since 26.0.0 |
|
88 | + * |
|
89 | + * @return mixed |
|
90 | + */ |
|
91 | + public function getOldValue() { |
|
92 | + return $this->oldValue; |
|
93 | + } |
|
94 | 94 | } |
@@ -31,32 +31,32 @@ |
||
31 | 31 | * @since 27.0.0 |
32 | 32 | */ |
33 | 33 | class AppEnableEvent extends Event { |
34 | - private string $appId; |
|
35 | - /** @var string[] */ |
|
36 | - private array $groupIds; |
|
37 | - |
|
38 | - /** |
|
39 | - * @param string[] $groupIds |
|
40 | - * @since 27.0.0 |
|
41 | - */ |
|
42 | - public function __construct(string $appId, array $groupIds = []) { |
|
43 | - parent::__construct(); |
|
44 | - |
|
45 | - $this->appId = $appId; |
|
46 | - $this->groupIds = $groupIds; |
|
47 | - } |
|
48 | - |
|
49 | - /** |
|
50 | - * @since 27.0.0 |
|
51 | - */ |
|
52 | - public function getAppId(): string { |
|
53 | - return $this->appId; |
|
54 | - } |
|
55 | - |
|
56 | - /** |
|
57 | - * @since 27.0.0 |
|
58 | - */ |
|
59 | - public function getGroupIds(): array { |
|
60 | - return $this->groupIds; |
|
61 | - } |
|
34 | + private string $appId; |
|
35 | + /** @var string[] */ |
|
36 | + private array $groupIds; |
|
37 | + |
|
38 | + /** |
|
39 | + * @param string[] $groupIds |
|
40 | + * @since 27.0.0 |
|
41 | + */ |
|
42 | + public function __construct(string $appId, array $groupIds = []) { |
|
43 | + parent::__construct(); |
|
44 | + |
|
45 | + $this->appId = $appId; |
|
46 | + $this->groupIds = $groupIds; |
|
47 | + } |
|
48 | + |
|
49 | + /** |
|
50 | + * @since 27.0.0 |
|
51 | + */ |
|
52 | + public function getAppId(): string { |
|
53 | + return $this->appId; |
|
54 | + } |
|
55 | + |
|
56 | + /** |
|
57 | + * @since 27.0.0 |
|
58 | + */ |
|
59 | + public function getGroupIds(): array { |
|
60 | + return $this->groupIds; |
|
61 | + } |
|
62 | 62 | } |
@@ -31,21 +31,21 @@ |
||
31 | 31 | * @since 27.0.0 |
32 | 32 | */ |
33 | 33 | class AppDisableEvent extends Event { |
34 | - private string $appId; |
|
34 | + private string $appId; |
|
35 | 35 | |
36 | - /** |
|
37 | - * @since 27.0.0 |
|
38 | - */ |
|
39 | - public function __construct(string $appId) { |
|
40 | - parent::__construct(); |
|
36 | + /** |
|
37 | + * @since 27.0.0 |
|
38 | + */ |
|
39 | + public function __construct(string $appId) { |
|
40 | + parent::__construct(); |
|
41 | 41 | |
42 | - $this->appId = $appId; |
|
43 | - } |
|
42 | + $this->appId = $appId; |
|
43 | + } |
|
44 | 44 | |
45 | - /** |
|
46 | - * @since 27.0.0 |
|
47 | - */ |
|
48 | - public function getAppId(): string { |
|
49 | - return $this->appId; |
|
50 | - } |
|
45 | + /** |
|
46 | + * @since 27.0.0 |
|
47 | + */ |
|
48 | + public function getAppId(): string { |
|
49 | + return $this->appId; |
|
50 | + } |
|
51 | 51 | } |
@@ -27,31 +27,31 @@ |
||
27 | 27 | use OCP\Files\Search\ISearchOperator; |
28 | 28 | |
29 | 29 | class QueryOptimizerStep { |
30 | - /** |
|
31 | - * Allow optimizer steps to inspect the entire query before starting processing |
|
32 | - * |
|
33 | - * @param ISearchOperator $operator |
|
34 | - * @return void |
|
35 | - */ |
|
36 | - public function inspectOperator(ISearchOperator $operator): void { |
|
37 | - if ($operator instanceof ISearchBinaryOperator) { |
|
38 | - foreach ($operator->getArguments() as $argument) { |
|
39 | - $this->inspectOperator($argument); |
|
40 | - } |
|
41 | - } |
|
42 | - } |
|
30 | + /** |
|
31 | + * Allow optimizer steps to inspect the entire query before starting processing |
|
32 | + * |
|
33 | + * @param ISearchOperator $operator |
|
34 | + * @return void |
|
35 | + */ |
|
36 | + public function inspectOperator(ISearchOperator $operator): void { |
|
37 | + if ($operator instanceof ISearchBinaryOperator) { |
|
38 | + foreach ($operator->getArguments() as $argument) { |
|
39 | + $this->inspectOperator($argument); |
|
40 | + } |
|
41 | + } |
|
42 | + } |
|
43 | 43 | |
44 | - /** |
|
45 | - * Allow optimizer steps to modify query operators |
|
46 | - * |
|
47 | - * @param ISearchOperator $operator |
|
48 | - * @return void |
|
49 | - */ |
|
50 | - public function processOperator(ISearchOperator &$operator) { |
|
51 | - if ($operator instanceof ISearchBinaryOperator) { |
|
52 | - foreach ($operator->getArguments() as $argument) { |
|
53 | - $this->processOperator($argument); |
|
54 | - } |
|
55 | - } |
|
56 | - } |
|
44 | + /** |
|
45 | + * Allow optimizer steps to modify query operators |
|
46 | + * |
|
47 | + * @param ISearchOperator $operator |
|
48 | + * @return void |
|
49 | + */ |
|
50 | + public function processOperator(ISearchOperator &$operator) { |
|
51 | + if ($operator instanceof ISearchBinaryOperator) { |
|
52 | + foreach ($operator->getArguments() as $argument) { |
|
53 | + $this->processOperator($argument); |
|
54 | + } |
|
55 | + } |
|
56 | + } |
|
57 | 57 | } |
@@ -34,196 +34,196 @@ |
||
34 | 34 | use OCP\Files\SimpleFS\ISimpleFile; |
35 | 35 | |
36 | 36 | class NewSimpleFile implements ISimpleFile { |
37 | - private Folder $parentFolder; |
|
38 | - private string $name; |
|
39 | - private ?File $file = null; |
|
40 | - |
|
41 | - /** |
|
42 | - * File constructor. |
|
43 | - */ |
|
44 | - public function __construct(Folder $parentFolder, string $name) { |
|
45 | - $this->parentFolder = $parentFolder; |
|
46 | - $this->name = $name; |
|
47 | - } |
|
48 | - |
|
49 | - /** |
|
50 | - * Get the name |
|
51 | - */ |
|
52 | - public function getName(): string { |
|
53 | - return $this->name; |
|
54 | - } |
|
55 | - |
|
56 | - /** |
|
57 | - * Get the size in bytes |
|
58 | - */ |
|
59 | - public function getSize(): int|float { |
|
60 | - if ($this->file) { |
|
61 | - return $this->file->getSize(); |
|
62 | - } else { |
|
63 | - return 0; |
|
64 | - } |
|
65 | - } |
|
66 | - |
|
67 | - /** |
|
68 | - * Get the ETag |
|
69 | - */ |
|
70 | - public function getETag(): string { |
|
71 | - if ($this->file) { |
|
72 | - return $this->file->getEtag(); |
|
73 | - } else { |
|
74 | - return ''; |
|
75 | - } |
|
76 | - } |
|
77 | - |
|
78 | - /** |
|
79 | - * Get the last modification time |
|
80 | - */ |
|
81 | - public function getMTime(): int { |
|
82 | - if ($this->file) { |
|
83 | - return $this->file->getMTime(); |
|
84 | - } else { |
|
85 | - return time(); |
|
86 | - } |
|
87 | - } |
|
88 | - |
|
89 | - /** |
|
90 | - * Get the content |
|
91 | - * |
|
92 | - * @throws NotFoundException |
|
93 | - * @throws NotPermittedException |
|
94 | - */ |
|
95 | - public function getContent(): string { |
|
96 | - if ($this->file) { |
|
97 | - $result = $this->file->getContent(); |
|
98 | - |
|
99 | - if ($result === false) { |
|
100 | - $this->checkFile(); |
|
101 | - } |
|
102 | - |
|
103 | - return $result; |
|
104 | - } else { |
|
105 | - return ''; |
|
106 | - } |
|
107 | - } |
|
108 | - |
|
109 | - /** |
|
110 | - * Overwrite the file |
|
111 | - * |
|
112 | - * @param string|resource $data |
|
113 | - * @throws NotPermittedException |
|
114 | - * @throws NotFoundException |
|
115 | - */ |
|
116 | - public function putContent($data): void { |
|
117 | - try { |
|
118 | - if ($this->file) { |
|
119 | - $this->file->putContent($data); |
|
120 | - } else { |
|
121 | - $this->file = $this->parentFolder->newFile($this->name, $data); |
|
122 | - } |
|
123 | - } catch (NotFoundException $e) { |
|
124 | - $this->checkFile(); |
|
125 | - } |
|
126 | - } |
|
127 | - |
|
128 | - /** |
|
129 | - * Sometimes there are some issues with the AppData. Most of them are from |
|
130 | - * user error. But we should handle them gracefully anyway. |
|
131 | - * |
|
132 | - * If for some reason the current file can't be found. We remove it. |
|
133 | - * Then traverse up and check all folders if they exists. This so that the |
|
134 | - * next request will have a valid appdata structure again. |
|
135 | - * |
|
136 | - * @throws NotFoundException |
|
137 | - */ |
|
138 | - private function checkFile(): void { |
|
139 | - if (!$this->file) { |
|
140 | - throw new NotFoundException('File not set'); |
|
141 | - } |
|
142 | - |
|
143 | - $cur = $this->file; |
|
144 | - |
|
145 | - while ($cur->stat() === false) { |
|
146 | - $parent = $cur->getParent(); |
|
147 | - try { |
|
148 | - $cur->delete(); |
|
149 | - } catch (NotFoundException $e) { |
|
150 | - // Just continue then |
|
151 | - } |
|
152 | - $cur = $parent; |
|
153 | - } |
|
154 | - |
|
155 | - if ($cur !== $this->file) { |
|
156 | - throw new NotFoundException('File does not exist'); |
|
157 | - } |
|
158 | - } |
|
159 | - |
|
160 | - |
|
161 | - /** |
|
162 | - * Delete the file |
|
163 | - * |
|
164 | - * @throws NotPermittedException |
|
165 | - */ |
|
166 | - public function delete(): void { |
|
167 | - if ($this->file) { |
|
168 | - $this->file->delete(); |
|
169 | - } |
|
170 | - } |
|
171 | - |
|
172 | - /** |
|
173 | - * Get the MimeType |
|
174 | - * |
|
175 | - * @return string |
|
176 | - */ |
|
177 | - public function getMimeType(): string { |
|
178 | - if ($this->file) { |
|
179 | - return $this->file->getMimeType(); |
|
180 | - } else { |
|
181 | - return 'text/plain'; |
|
182 | - } |
|
183 | - } |
|
184 | - |
|
185 | - /** |
|
186 | - * {@inheritDoc} |
|
187 | - */ |
|
188 | - public function getExtension(): string { |
|
189 | - if ($this->file) { |
|
190 | - return $this->file->getExtension(); |
|
191 | - } else { |
|
192 | - return \pathinfo($this->name, PATHINFO_EXTENSION); |
|
193 | - } |
|
194 | - } |
|
195 | - |
|
196 | - /** |
|
197 | - * Open the file as stream for reading, resulting resource can be operated as stream like the result from php's own fopen |
|
198 | - * |
|
199 | - * @return resource|false |
|
200 | - * @throws \OCP\Files\NotPermittedException |
|
201 | - * @since 14.0.0 |
|
202 | - */ |
|
203 | - public function read() { |
|
204 | - if ($this->file) { |
|
205 | - return $this->file->fopen('r'); |
|
206 | - } else { |
|
207 | - return fopen('php://temp', 'r'); |
|
208 | - } |
|
209 | - } |
|
210 | - |
|
211 | - /** |
|
212 | - * Open the file as stream for writing, resulting resource can be operated as stream like the result from php's own fopen |
|
213 | - * |
|
214 | - * @return resource|bool |
|
215 | - * @throws \OCP\Files\NotPermittedException |
|
216 | - * @since 14.0.0 |
|
217 | - */ |
|
218 | - public function write() { |
|
219 | - if ($this->file) { |
|
220 | - return $this->file->fopen('w'); |
|
221 | - } else { |
|
222 | - $source = fopen('php://temp', 'w+'); |
|
223 | - return CallbackWrapper::wrap($source, null, null, null, null, function () use ($source) { |
|
224 | - rewind($source); |
|
225 | - $this->putContent($source); |
|
226 | - }); |
|
227 | - } |
|
228 | - } |
|
37 | + private Folder $parentFolder; |
|
38 | + private string $name; |
|
39 | + private ?File $file = null; |
|
40 | + |
|
41 | + /** |
|
42 | + * File constructor. |
|
43 | + */ |
|
44 | + public function __construct(Folder $parentFolder, string $name) { |
|
45 | + $this->parentFolder = $parentFolder; |
|
46 | + $this->name = $name; |
|
47 | + } |
|
48 | + |
|
49 | + /** |
|
50 | + * Get the name |
|
51 | + */ |
|
52 | + public function getName(): string { |
|
53 | + return $this->name; |
|
54 | + } |
|
55 | + |
|
56 | + /** |
|
57 | + * Get the size in bytes |
|
58 | + */ |
|
59 | + public function getSize(): int|float { |
|
60 | + if ($this->file) { |
|
61 | + return $this->file->getSize(); |
|
62 | + } else { |
|
63 | + return 0; |
|
64 | + } |
|
65 | + } |
|
66 | + |
|
67 | + /** |
|
68 | + * Get the ETag |
|
69 | + */ |
|
70 | + public function getETag(): string { |
|
71 | + if ($this->file) { |
|
72 | + return $this->file->getEtag(); |
|
73 | + } else { |
|
74 | + return ''; |
|
75 | + } |
|
76 | + } |
|
77 | + |
|
78 | + /** |
|
79 | + * Get the last modification time |
|
80 | + */ |
|
81 | + public function getMTime(): int { |
|
82 | + if ($this->file) { |
|
83 | + return $this->file->getMTime(); |
|
84 | + } else { |
|
85 | + return time(); |
|
86 | + } |
|
87 | + } |
|
88 | + |
|
89 | + /** |
|
90 | + * Get the content |
|
91 | + * |
|
92 | + * @throws NotFoundException |
|
93 | + * @throws NotPermittedException |
|
94 | + */ |
|
95 | + public function getContent(): string { |
|
96 | + if ($this->file) { |
|
97 | + $result = $this->file->getContent(); |
|
98 | + |
|
99 | + if ($result === false) { |
|
100 | + $this->checkFile(); |
|
101 | + } |
|
102 | + |
|
103 | + return $result; |
|
104 | + } else { |
|
105 | + return ''; |
|
106 | + } |
|
107 | + } |
|
108 | + |
|
109 | + /** |
|
110 | + * Overwrite the file |
|
111 | + * |
|
112 | + * @param string|resource $data |
|
113 | + * @throws NotPermittedException |
|
114 | + * @throws NotFoundException |
|
115 | + */ |
|
116 | + public function putContent($data): void { |
|
117 | + try { |
|
118 | + if ($this->file) { |
|
119 | + $this->file->putContent($data); |
|
120 | + } else { |
|
121 | + $this->file = $this->parentFolder->newFile($this->name, $data); |
|
122 | + } |
|
123 | + } catch (NotFoundException $e) { |
|
124 | + $this->checkFile(); |
|
125 | + } |
|
126 | + } |
|
127 | + |
|
128 | + /** |
|
129 | + * Sometimes there are some issues with the AppData. Most of them are from |
|
130 | + * user error. But we should handle them gracefully anyway. |
|
131 | + * |
|
132 | + * If for some reason the current file can't be found. We remove it. |
|
133 | + * Then traverse up and check all folders if they exists. This so that the |
|
134 | + * next request will have a valid appdata structure again. |
|
135 | + * |
|
136 | + * @throws NotFoundException |
|
137 | + */ |
|
138 | + private function checkFile(): void { |
|
139 | + if (!$this->file) { |
|
140 | + throw new NotFoundException('File not set'); |
|
141 | + } |
|
142 | + |
|
143 | + $cur = $this->file; |
|
144 | + |
|
145 | + while ($cur->stat() === false) { |
|
146 | + $parent = $cur->getParent(); |
|
147 | + try { |
|
148 | + $cur->delete(); |
|
149 | + } catch (NotFoundException $e) { |
|
150 | + // Just continue then |
|
151 | + } |
|
152 | + $cur = $parent; |
|
153 | + } |
|
154 | + |
|
155 | + if ($cur !== $this->file) { |
|
156 | + throw new NotFoundException('File does not exist'); |
|
157 | + } |
|
158 | + } |
|
159 | + |
|
160 | + |
|
161 | + /** |
|
162 | + * Delete the file |
|
163 | + * |
|
164 | + * @throws NotPermittedException |
|
165 | + */ |
|
166 | + public function delete(): void { |
|
167 | + if ($this->file) { |
|
168 | + $this->file->delete(); |
|
169 | + } |
|
170 | + } |
|
171 | + |
|
172 | + /** |
|
173 | + * Get the MimeType |
|
174 | + * |
|
175 | + * @return string |
|
176 | + */ |
|
177 | + public function getMimeType(): string { |
|
178 | + if ($this->file) { |
|
179 | + return $this->file->getMimeType(); |
|
180 | + } else { |
|
181 | + return 'text/plain'; |
|
182 | + } |
|
183 | + } |
|
184 | + |
|
185 | + /** |
|
186 | + * {@inheritDoc} |
|
187 | + */ |
|
188 | + public function getExtension(): string { |
|
189 | + if ($this->file) { |
|
190 | + return $this->file->getExtension(); |
|
191 | + } else { |
|
192 | + return \pathinfo($this->name, PATHINFO_EXTENSION); |
|
193 | + } |
|
194 | + } |
|
195 | + |
|
196 | + /** |
|
197 | + * Open the file as stream for reading, resulting resource can be operated as stream like the result from php's own fopen |
|
198 | + * |
|
199 | + * @return resource|false |
|
200 | + * @throws \OCP\Files\NotPermittedException |
|
201 | + * @since 14.0.0 |
|
202 | + */ |
|
203 | + public function read() { |
|
204 | + if ($this->file) { |
|
205 | + return $this->file->fopen('r'); |
|
206 | + } else { |
|
207 | + return fopen('php://temp', 'r'); |
|
208 | + } |
|
209 | + } |
|
210 | + |
|
211 | + /** |
|
212 | + * Open the file as stream for writing, resulting resource can be operated as stream like the result from php's own fopen |
|
213 | + * |
|
214 | + * @return resource|bool |
|
215 | + * @throws \OCP\Files\NotPermittedException |
|
216 | + * @since 14.0.0 |
|
217 | + */ |
|
218 | + public function write() { |
|
219 | + if ($this->file) { |
|
220 | + return $this->file->fopen('w'); |
|
221 | + } else { |
|
222 | + $source = fopen('php://temp', 'w+'); |
|
223 | + return CallbackWrapper::wrap($source, null, null, null, null, function () use ($source) { |
|
224 | + rewind($source); |
|
225 | + $this->putContent($source); |
|
226 | + }); |
|
227 | + } |
|
228 | + } |
|
229 | 229 | } |