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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2022 Vitor Mattos <vitor@php.rio>
*
* @author Vitor Mattos <vitor@php.rio>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Talk\Service;
use InvalidArgumentException;
use OC\User\NoUserException;
use OCA\Talk\Config;
use OCA\Talk\Exceptions\ParticipantNotFoundException;
use OCA\Talk\Participant;
use OCA\Talk\Room;
use OCP\Files\File;
use OCP\Files\Folder;
use OCP\Files\IMimeTypeDetector;
use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\Notification\IAction;
use OCP\Notification\IManager;
class RecordingService {
public const DEFAULT_ALLOWED_RECORDING_FORMATS = [
'audio/ogg' => ['ogg'],
'video/ogg' => ['ogv'],
'video/x-matroska' => ['mkv'],
];
public function __construct(
private IMimeTypeDetector $mimeTypeDetector,
private ParticipantService $participantService,
private IRootFolder $rootFolder,
private IManager $notificationManager,
private IL10N $l,
private IURLGenerator $urlGenerator,
private Config $config,
private RoomService $roomService
) {
}
public function start(Room $room, int $status): void {
$availableRecordingTypes = [Room::RECORDING_VIDEO, Room::RECORDING_AUDIO];
if (!in_array($status, $availableRecordingTypes)) {
throw new InvalidArgumentException('status');
}
if ($room->getCallRecording() !== Room::RECORDING_NONE) {
throw new InvalidArgumentException('recording');
}
if (!$room->getActiveSince() instanceof \DateTimeInterface) {
throw new InvalidArgumentException('call');
}
$this->roomService->setCallRecording($room, $status);
}
public function stop(Room $room): void {
if ($room->getCallRecording() === Room::RECORDING_NONE) {
throw new InvalidArgumentException('recording');
}
$this->roomService->setCallRecording($room);
}
public function store(Room $room, string $owner, array $file): void {
$content = $this->getContentFromFileArray($file);
$fileName = basename($file['name']);
$this->validateFileFormat($fileName, $content);
try {
$participant = $this->participantService->getParticipant($room, $owner);
} catch (ParticipantNotFoundException $e) {
throw new InvalidArgumentException('owner_participant');
}
try {
$recordingFolder = $this->getRecordingFolder($owner, $room->getToken());
$file = $recordingFolder->newFile($fileName, $content);
$this->notifyStoredRecording($room, $participant, $file);
} catch (NoUserException $e) {
throw new InvalidArgumentException('owner_invalid');
} catch (NotPermittedException $e) {
throw new InvalidArgumentException('owner_permission');
}
}
public function getContentFromFileArray(array $file): string {
if (
$file['error'] !== 0 ||
!is_uploaded_file($file['tmp_name'])
) {
throw new InvalidArgumentException('invalid_file');
}
$content = file_get_contents($file['tmp_name']);
unlink($file['tmp_name']);
if (!$content) {
throw new InvalidArgumentException('empty_file');
}
return $content;
}
public function validateFileFormat(string $fileName, $content): void {
$mimeType = $this->mimeTypeDetector->detectString($content);
$allowed = self::DEFAULT_ALLOWED_RECORDING_FORMATS;
if (!array_key_exists($mimeType, $allowed)) {
throw new InvalidArgumentException('file_mimetype');
}
$extension = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));
if (!$extension || !in_array($extension, $allowed[$mimeType])) {
throw new InvalidArgumentException('file_extension');
}
}
private function getRecordingFolder(string $owner, string $token): Folder {
$userFolder = $this->rootFolder->getUserFolder($owner);
$recordingRootFolderName = $this->config->getRecordingFolder($owner);
try {
/** @var \OCP\Files\Folder */
$recordingRootFolder = $userFolder->get($recordingRootFolderName);
} catch (NotFoundException $e) {
/** @var \OCP\Files\Folder */
$recordingRootFolder = $userFolder->newFolder($recordingRootFolderName);
}
try {
$recordingFolder = $recordingRootFolder->get($token);
} catch (NotFoundException $e) {
$recordingFolder = $recordingRootFolder->newFolder($token);
}
return $recordingFolder;
}
public function notifyStoredRecording(Room $room, Participant $participant, File $file): void {
$attendee = $participant->getAttendee();
$notificationLevel = $attendee->getNotificationLevel();
if ($notificationLevel === Participant::NOTIFY_NEVER) {
return;
}
$notification = $this->notificationManager->createNotification();
$shareAction = $notification->createAction()
->setParsedLabel($this->l->t('Share to chat'))
->setPrimary(true)
->setLink(
$this->urlGenerator->linkToRouteAbsolute(
'spreed.Chat.shareObjectToChat',
[
'token' => $room->getToken()
]
),
IAction::TYPE_POST
);
$notification
->setApp('spreed')
->setDateTime(new \DateTime())
->setObject('chat', $room->getToken())
->setUser($attendee->
|