-
-
Notifications
You must be signed in to change notification settings - Fork 88
/
UtilController.php
131 lines (119 loc) · 3.92 KB
/
UtilController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
declare(strict_types=1);
namespace OCA\Memories;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
trait UtilController
{
/**
* Run a function and catch exceptions to return HTTP response.
*
* @param \Closure(): Http\Response $closure
*/
public static function guardEx(\Closure $closure): Http\Response
{
try {
return $closure();
} catch (\OCA\Memories\HttpResponseException $e) {
return $e->response;
} catch (\Exception $e) {
return new DataResponse([
'message' => $e->getMessage(),
], Http::STATUS_INTERNAL_SERVER_ERROR);
}
}
/**
* Return a callback response with guarded exceptions.
*
* @param \Closure(Http\IOutput): void $closure
*/
public static function guardExDirect(\Closure $closure): Http\Response
{
/** @psalm-suppress MissingTemplateParam */
return new class($closure) extends Http\Response implements Http\ICallbackResponse {
/**
* @param \Closure(Http\IOutput): void $closure
*/
public function __construct(private \Closure $closure)
{
parent::__construct();
}
public function callback(Http\IOutput $output): void
{
try {
($this->closure)($output);
} catch (\OCA\Memories\HttpResponseException $e) {
$res = $e->response;
$output->setHttpResponseCode($res->getStatus());
if ($res instanceof Http\DataResponse) {
$output->setHeader('Content-Type: application/json');
$output->setOutput(json_encode($res->getData()));
} else {
$output->setOutput($res->render());
}
} catch (\Exception $e) {
$output->setHttpResponseCode(Http::STATUS_INTERNAL_SERVER_ERROR);
$output->setHeader('Content-Type: application/json');
$output->setOutput(json_encode([
'message' => $e->getMessage(),
]));
}
}
};
}
/**
* Get the current user.
*
* @throws \OCA\Memories\HttpResponseException if the user is not logged in
*/
public static function getUser(): \OCP\IUser
{
return \OC::$server->get(\OCP\IUserSession::class)->getUser()
?? throw Exceptions::NotLoggedIn();
}
/**
* Get the current user ID.
*
* @throws \OCA\Memories\HttpResponseException if the user is not logged in
*/
public static function getUID(): string
{
return self::getUser()->getUID();
}
/**
* Check if the user is logged in.
*/
public static function isLoggedIn(): bool
{
return null !== \OC::$server->get(\OCP\IUserSession::class)->getUser();
}
/**
* Get a user's home folder.
*
* @param null|string $uid User ID, or null for the user
*
* @throws \OCA\Memories\HttpResponseException if the user is not logged in
*/
public static function getUserFolder(?string $uid = null): \OCP\Files\Folder
{
return \OC::$server->get(\OCP\Files\IRootFolder::class)
->getUserFolder($uid ?? self::getUID())
;
}
/**
* Get the language code for the current user.
*/
public static function getUserLang(): string
{
// Default language
$config = \OC::$server->get(\OCP\IConfig::class);
$default = $config->getSystemValue('default_language', 'en');
try {
// Get language of the user
return $config->getUserValue(self::getUID(), 'core', 'lang', $default);
} catch (\Exception) {
// Fallback to server language
return $default;
}
}
}