-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathAttachmentService.php
More file actions
707 lines (662 loc) Β· 22.5 KB
/
AttachmentService.php
File metadata and controls
707 lines (662 loc) Β· 22.5 KB
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2021 Julien Veyssier <eneiluj@posteo.net>
*
* @author Julien Veyssier <eneiluj@posteo.net>
*
* @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\Text\Service;
use OC\User\NoUserException;
use OCA\DAV\Connector\Sabre\PublicAuth;
use OCA\Files_Sharing\SharedStorage;
use OCA\Text\Controller\AttachmentController;
use OCA\Text\Db\Session;
use OCP\Constants;
use OCP\Files\File;
use OCP\Files\Folder;
use OCP\Files\IMimeTypeDetector;
use OCP\Files\InvalidPathException;
use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\Files\SimpleFS\ISimpleFile;
use OCP\IPreview;
use OCP\ISession;
use OCP\IURLGenerator;
use OCP\Lock\LockedException;
use OCP\Share\Exceptions\ShareNotFound;
use OCP\Share\IManager as ShareManager;
use OCP\Share\IShare;
use OCP\Util;
class AttachmentService {
public function __construct(private IRootFolder $rootFolder,
private ShareManager $shareManager,
private IPreview $previewManager,
private IMimeTypeDetector $mimeTypeDetector,
private IURLGenerator $urlGenerator,
private ISession $session) {
}
/**
* Get image content or preview from file name
*
* @throws InvalidPathException
* @throws NoUserException
* @throws NotFoundException
* @throws NotPermittedException
*/
public function getImageFile(int $documentId, string $imageFileName, string $userId, bool $preferRawImage): File|ISimpleFile|null {
$textFile = $this->getTextFile($documentId, $userId);
return $this->getImageFileContent($imageFileName, $textFile, $preferRawImage);
}
/**
* Get image content or preview from file id in public context
*
* @throws NotFoundException
* @throws NotPermittedException
* @throws InvalidPathException
* @throws NoUserException
*/
public function getImageFilePublic(int $documentId, string $imageFileName, string $shareToken, bool $preferRawImage): File|ISimpleFile|null {
$textFile = $this->getTextFilePublic($documentId, $shareToken);
return $this->getImageFileContent($imageFileName, $textFile, $preferRawImage);
}
/**
* @throws InvalidPathException
* @throws NoUserException
* @throws NotFoundException
* @throws NotPermittedException
*/
private function getImageFileContent(string $imageFileName, File $textFile, bool $preferRawImage): File|ISimpleFile|null {
$attachmentFolder = $this->getAttachmentDirectoryForFile($textFile, true);
$imageFile = $attachmentFolder->get($imageFileName);
if ($imageFile instanceof File && in_array($imageFile->getMimetype(), AttachmentController::IMAGE_MIME_TYPES, true)) {
// previews of gifs are static images, always provide the real gif
if ($imageFile->getMimetype() === 'image/gif') {
return $imageFile;
}
// we might prefer the raw image
if ($preferRawImage && in_array($imageFile->getMimetype(), AttachmentController::BROWSER_SUPPORTED_IMAGE_MIME_TYPES, true)) {
return $imageFile;
}
if ($this->previewManager->isMimeSupported($imageFile->getMimeType())) {
return $this->previewManager->getPreview($imageFile, 1024, 1024);
}
// fallback: raw image
return $imageFile;
}
return null;
}
/**
* Get media file from file name
*
* @throws NotFoundException
* @throws InvalidPathException
* @throws NotPermittedException
* @throws NoUserException
*/
public function getMediaFile(int $documentId, string $mediaFileName, string $userId): File|null {
$textFile = $this->getTextFile($documentId, $userId);
return $this->getMediaFullFile($mediaFileName, $textFile);
}
/**
* Get image content or preview from file id in public context
*
* @throws NotFoundException
* @throws NotPermittedException
* @throws InvalidPathException
* @throws NoUserException
*/
public function getMediaFilePublic(int $documentId, string $mediaFileName, string $shareToken): File|null {
$textFile = $this->getTextFilePublic($documentId, $shareToken);
return $this->getMediaFullFile($mediaFileName, $textFile);
}
/**
* @throws NotFoundException
* @throws NotPermittedException
* @throws InvalidPathException
* @throws NoUserException
*/
private function getMediaFullFile(string $mediaFileName, File $textFile): ?File {
$attachmentFolder = $this->getAttachmentDirectoryForFile($textFile, true);
$mediaFile = $attachmentFolder->get($mediaFileName);
if ($mediaFile instanceof File && !$this->isDownloadDisabled($mediaFile)) {
return $mediaFile;
}
return null;
}
/**
* @throws NotFoundException
* @throws NotPermittedException
* @throws InvalidPathException
* @throws NoUserException
*/
public function getMediaFilePreview(int $documentId, string $mediaFileName, string $userId): ?array {
$textFile = $this->getTextFile($documentId, $userId);
return $this->getMediaFilePreviewFile($mediaFileName, $textFile);
}
/**
* @throws NotFoundException
* @throws NotPermittedException
* @throws InvalidPathException
* @throws NoUserException
*/
public function getMediaFilePreviewPublic(int $documentId, string $mediaFileName, string $shareToken): ?array {
$textFile = $this->getTextFilePublic($documentId, $shareToken);
return $this->getMediaFilePreviewFile($mediaFileName, $textFile);
}
/**
* Get media preview or mimetype icon address
*
* @throws NotFoundException
* @throws NotPermittedException
* @throws InvalidPathException
* @throws NoUserException
*/
private function getMediaFilePreviewFile(string $mediaFileName, File $textFile): ?array {
$attachmentFolder = $this->getAttachmentDirectoryForFile($textFile, true);
$mediaFile = $attachmentFolder->get($mediaFileName);
if ($mediaFile instanceof File && !$this->isDownloadDisabled($mediaFile)) {
if ($this->previewManager->isMimeSupported($mediaFile->getMimeType())) {
try {
return [
'type' => 'file',
'file' => $this->previewManager->getPreview($mediaFile, 1024, 1024),
];
} catch (NotFoundException $e) {
// the preview might not be found even if the mimetype is supported
}
}
// fallback: mimetype icon URL
return [
'type' => 'icon',
'iconUrl' => $this->mimeTypeDetector->mimeTypeIcon($mediaFile->getMimeType()),
];
}
return null;
}
/**
* @param int $documentId
* @param string|null $userId
* @param Session|null $session
* @param string|null $shareToken
*
* @return array
* @throws InvalidPathException
* @throws NoUserException
* @throws NotFoundException
* @throws NotPermittedException
*/
public function getAttachmentList(int $documentId, ?string $userId = null, ?Session $session = null, ?string $shareToken = null): array {
if ($shareToken) {
$textFile = $this->getTextFilePublic($documentId, $shareToken);
} elseif ($userId) {
$textFile = $this->getTextFile($documentId, $userId);
} else {
throw new NotPermittedException('Unable to read document');
}
try {
$attachmentDir = $this->getAttachmentDirectoryForFile($textFile);
} catch (NotFoundException) {
return [];
}
$shareTokenUrlString = $shareToken
? '&shareToken=' . rawurlencode($shareToken)
: '';
$urlParamsBase = $session
? '?documentId=' . $documentId . '&sessionId=' . $session->getId() . '&sessionToken=' . rawurlencode($session->getToken()) . $shareTokenUrlString
: '?documentId=' . $documentId . $shareTokenUrlString;
$attachments = [];
$userFolder = $userId ? $this->rootFolder->getUserFolder($userId) : null;
foreach ($attachmentDir->getDirectoryListing() as $node) {
if (!($node instanceof File)) {
// Ignore anything but files
continue;
}
$isImage = in_array($node->getMimetype(), AttachmentController::IMAGE_MIME_TYPES, true);
$name = $node->getName();
$attachments[] = [
'fileId' => $node->getId(),
'name' => $name,
'size' => Util::humanFileSize($node->getSize()),
'mimetype' => $node->getMimeType(),
'mtime' => $node->getMTime(),
'isImage' => $isImage,
'davPath' => $userFolder?->getRelativePath($node->getPath()),
'fullUrl' => $isImage
? $this->urlGenerator->linkToRouteAbsolute('text.Attachment.getImageFile') . $urlParamsBase . '&imageFileName=' . rawurlencode($name) . '&preferRawImage=1'
: $this->urlGenerator->linkToRouteAbsolute('text.Attachment.getMediaFile') . $urlParamsBase . '&mediaFileName=' . rawurlencode($name),
'previewUrl' => $isImage
? $this->urlGenerator->linkToRouteAbsolute('text.Attachment.getImageFile') . $urlParamsBase . '&imageFileName=' . rawurlencode($name)
: $this->urlGenerator->linkToRouteAbsolute('text.Attachment.getMediaFilePreview') . $urlParamsBase . '&mediaFileName=' . rawurlencode($name),
];
}
return $attachments;
}
/**
* Save an uploaded file in the attachment folder
*
* @param int $documentId
* @param string $newFileName
* @param resource $newFileResource
* @param string $userId
*
* @return array
* @throws InvalidPathException
* @throws NoUserException
* @throws NotFoundException
* @throws NotPermittedException
*/
public function uploadAttachment(int $documentId, string $newFileName, $newFileResource, string $userId): array {
$textFile = $this->getTextFile($documentId, $userId);
if (!$textFile->isUpdateable()) {
throw new NotPermittedException('No write permissions');
}
$saveDir = $this->getAttachmentDirectoryForFile($textFile, true);
$fileName = self::getUniqueFileName($saveDir, $newFileName);
$savedFile = $saveDir->newFile($fileName, $newFileResource);
return [
'name' => $fileName,
'dirname' => $saveDir->getName(),
'id' => $savedFile->getId(),
'documentId' => $textFile->getId(),
];
}
/**
* Save an uploaded file in the attachment folder in a public context
*
* @param int|null $documentId
* @param string $newFileName
* @param resource $newFileResource
* @param string $shareToken
*
* @return array
* @throws NotFoundException
* @throws NotPermittedException
* @throws InvalidPathException
* @throws NoUserException
*/
public function uploadAttachmentPublic(?int $documentId, string $newFileName, $newFileResource, string $shareToken): array {
try {
$share = $this->shareManager->getShareByToken($shareToken);
} catch (ShareNotFound) {
throw new NotFoundException('Share not found');
}
if (!$this->hasUpdatePermissions($share)) {
throw new NotPermittedException('No write permissions');
}
if ($share->getPassword() !== null) {
$key = PublicAuth::DAV_AUTHENTICATED;
if (!$this->session->exists($key)) {
throw new NotPermittedException('Share not authenticated');
}
$allowedShareIds = $this->session->get($key);
if (!is_array($allowedShareIds)) {
throw new NotPermittedException('Share not authenticated');
}
if (!in_array($share->getId(), $allowedShareIds, true)) {
throw new NotPermittedException('Share not authenticated');
}
}
$textFile = $this->getTextFilePublic($documentId, $shareToken);
$saveDir = $this->getAttachmentDirectoryForFile($textFile, true);
$fileName = self::getUniqueFileName($saveDir, $newFileName);
$savedFile = $saveDir->newFile($fileName, $newFileResource);
return [
'name' => $fileName,
'dirname' => $saveDir->getName(),
'id' => $savedFile->getId(),
'documentId' => $textFile->getId(),
];
}
/**
* Copy a file from a user's storage in the attachment folder
*
* @param int $documentId
* @param string $path
* @param string $userId
*
* @return array
* @throws NotFoundException
* @throws NotPermittedException
* @throws InvalidPathException
* @throws NoUserException
*/
public function insertAttachmentFile(int $documentId, string $path, string $userId): array {
$textFile = $this->getTextFile($documentId, $userId);
if (!$textFile->isUpdateable()) {
throw new NotPermittedException('No write permissions');
}
$originalFile = $this->getFileFromPath($path, $userId);
$saveDir = $this->getAttachmentDirectoryForFile($textFile, true);
return $this->copyFile($originalFile, $saveDir, $textFile);
}
/**
* @param File $originalFile
* @param Folder $saveDir
* @param File $textFile
*
* @return array
* @throws NotFoundException
* @throws InvalidPathException
*/
private function copyFile(File $originalFile, Folder $saveDir, File $textFile): array {
$fileName = self::getUniqueFileName($saveDir, $originalFile->getName());
$targetPath = $saveDir->getPath() . '/' . $fileName;
$targetFile = $originalFile->copy($targetPath);
return [
'name' => $fileName,
'dirname' => $saveDir->getName(),
'id' => $targetFile->getId(),
'documentId' => $textFile->getId(),
'mimetype' => $targetFile->getMimetype(),
];
}
/**
* Get unique file name in a directory. Add '(n)' suffix.
*
* @param Folder $dir
* @param string $fileName
*
* @return string
*/
public static function getUniqueFileName(Folder $dir, string $fileName): string {
$extension = pathinfo($fileName, PATHINFO_EXTENSION);
$counter = 1;
$uniqueFileName = $fileName;
if ($extension !== '') {
while ($dir->nodeExists($uniqueFileName)) {
$counter++;
$uniqueFileName = preg_replace('/\.' . $extension . '$/', ' (' . $counter . ').' . $extension, $fileName);
}
} else {
while ($dir->nodeExists($uniqueFileName)) {
$counter++;
$uniqueFileName = preg_replace('/$/', ' (' . $counter . ')', $fileName);
}
}
return $uniqueFileName;
}
/**
* Check if the shared access has write permissions
*/
private function hasUpdatePermissions(IShare $share): bool {
return (
in_array(
$share->getShareType(),
[IShare::TYPE_LINK, IShare::TYPE_EMAIL, IShare::TYPE_ROOM],
true
)
&& $share->getPermissions() & Constants::PERMISSION_UPDATE
&& $share->getNode()->getPermissions() & Constants::PERMISSION_UPDATE);
}
/**
* Get or create file-specific attachment folder
*
* @param File $textFile
* @param bool $create
*
* @return Folder
* @throws NotFoundException
* @throws NotPermittedException
* @throws InvalidPathException
* @throws NoUserException
*/
private function getAttachmentDirectoryForFile(File $textFile, bool $create = false): Folder {
$owner = $textFile->getOwner();
if ($owner === null) {
throw new NotFoundException('File has no owner');
}
$ownerId = $owner->getUID();
$ownerUserFolder = $this->rootFolder->getUserFolder($ownerId);
$ownerTextFile = $ownerUserFolder->getById($textFile->getId());
if (count($ownerTextFile) > 0) {
$ownerTextFile = $ownerTextFile[0];
$ownerParentFolder = $ownerTextFile->getParent();
$attachmentFolderName = '.attachments.' . $textFile->getId();
if ($ownerParentFolder->nodeExists($attachmentFolderName)) {
$attachmentFolder = $ownerParentFolder->get($attachmentFolderName);
if ($attachmentFolder instanceof Folder) {
return $attachmentFolder;
}
} elseif ($create) {
return $ownerParentFolder->newFolder($attachmentFolderName);
}
}
throw new NotFoundException('Attachment dir for document ' . $textFile->getId() . ' was not found or could not be created.');
}
/**
* Get a user file from file ID
* @throws NotFoundException
* @throws NotPermittedException
* @throws NoUserException
*/
private function getFileFromPath(string $filePath, string $userId): File {
$userFolder = $this->rootFolder->getUserFolder($userId);
if ($userFolder->nodeExists($filePath)) {
$file = $userFolder->get($filePath);
if ($file instanceof File && !$this->isDownloadDisabled($file)) {
return $file;
}
}
throw new NotFoundException();
}
/**
* @param File $file
*
* @return bool
* @throws NotFoundException
*/
private function isDownloadDisabled(File $file): bool {
$storage = $file->getStorage();
if ($storage->instanceOfStorage(SharedStorage::class)) {
/** @var SharedStorage $storage */
$share = $storage->getShare();
$attributes = $share->getAttributes();
if ($attributes !== null && $attributes->getAttribute('permissions', 'download') === false) {
return true;
}
}
return false;
}
/**
* Get a user file from file ID
*
* @param int $documentId
* @param string $userId
*
* @return File
* @throws NoUserException
* @throws NotFoundException
* @throws NotPermittedException
*/
private function getTextFile(int $documentId, string $userId): File {
$userFolder = $this->rootFolder->getUserFolder($userId);
$files = $userFolder->getById($documentId);
$file = array_shift($files);
if ($file instanceof File && !$this->isDownloadDisabled($file)) {
return $file;
}
throw new NotFoundException('Text file with id=' . $documentId . ' was not found in storage of ' . $userId);
}
/**
* Get file from share token
*
* @param int|null $documentId
* @param string $shareToken
*
* @return File
* @throws NotFoundException
*/
private function getTextFilePublic(?int $documentId, string $shareToken): File {
// is the file shared with this token?
try {
$share = $this->shareManager->getShareByToken($shareToken);
if (in_array($share->getShareType(), [IShare::TYPE_LINK, IShare::TYPE_EMAIL])) {
// shared file or folder?
if ($share->getNodeType() === 'file') {
$textFile = $share->getNode();
if ($textFile instanceof File && !$this->isDownloadDisabled($textFile)) {
return $textFile;
}
} elseif ($documentId !== null && $share->getNodeType() === 'folder') {
$folder = $share->getNode();
if ($folder instanceof Folder) {
$textFile = $folder->getById($documentId);
$textFile = array_shift($textFile);
if ($textFile instanceof File && !$this->isDownloadDisabled($textFile)) {
return $textFile;
}
}
}
}
} catch (ShareNotFound $e) {
// same as below
}
throw new NotFoundException('Text file with id=' . $documentId . ' and shareToken ' . $shareToken . ' was not found.');
}
/**
* Actually delete attachment files which are not pointed in the markdown content
*
* @param int $fileId
*
* @return int The number of deleted files
* @throws NotFoundException
* @throws NotPermittedException
* @throws InvalidPathException
* @throws LockedException
* @throws NoUserException
*/
public function cleanupAttachments(int $fileId): int {
$textFile = $this->rootFolder->getById($fileId);
if (count($textFile) > 0 && $textFile[0] instanceof File) {
$textFile = $textFile[0];
if ($textFile->getMimeType() === 'text/markdown') {
// get IDs of the files inside the attachment dir
try {
$attachmentDir = $this->getAttachmentDirectoryForFile($textFile);
} catch (NotFoundException $e) {
// this only happens if the attachment dir was deleted by the user while editing the document
return 0;
}
$attachmentsByName = [];
foreach ($attachmentDir->getDirectoryListing() as $attNode) {
$attachmentsByName[$attNode->getName()] = $attNode;
}
$contentAttachmentNames = self::getAttachmentNamesFromContent($textFile->getContent(), $fileId);
$toDelete = array_diff(array_keys($attachmentsByName), $contentAttachmentNames);
foreach ($toDelete as $name) {
$attachmentsByName[$name]->delete();
}
return count($toDelete);
}
}
return 0;
}
/**
* Get attachment file names listed in the markdown file content
*
* @param string $content
* @param int $fileId
*
* @return array
*/
public static function getAttachmentNamesFromContent(string $content, int $fileId): array {
$matches = [];
// matches  and captures FILE_NAME
preg_match_all(
'/\!\[(?>[^\[\]]+|\[(?>[^\[\]]+|\[(?>[^\[\]]+|\[(?>[^\[\]]+|\[(?>[^\[\]]+|\[(?>[^\[\]]+|\[\])*\])*\])*\])*\])*\])*\]\(\.attachments\.' . $fileId . '\/([^)&]+)\)/',
$content,
$matches,
PREG_SET_ORDER
);
return array_map(static function (array $match) {
return urldecode($match[1]);
}, $matches);
}
/**
* @param File $source
* @param File $target
*
* @throws NotFoundException
* @throws NotPermittedException
* @throws InvalidPathException
* @throws LockedException
*/
public function moveAttachments(File $source, File $target): void {
// if the parent directory has changed
if ($source->getParent()->getPath() !== $target->getParent()->getPath()) {
try {
$sourceAttachmentDir = $this->getAttachmentDirectoryForFile($source);
} catch (NotFoundException $e) {
// silently return if no attachment dir was found for source file
return;
}
// it is in the same directory as the source file in its owner's storage
// in other words, we move the attachment dir only if the .md file is moved by its owner
if ($source->getParent()->getId() === $sourceAttachmentDir->getParent()->getId()
) {
$sourceAttachmentDir->move($target->getParent()->getPath() . '/' . $sourceAttachmentDir->getName());
}
}
}
/**
* @param File $source
*
* @throws NotFoundException
* @throws NotPermittedException
* @throws InvalidPathException
* @throws NoUserException
*/
public function deleteAttachments(File $source): void {
// if there is an attachment dir for this file
try {
$sourceAttachmentDir = $this->getAttachmentDirectoryForFile($source);
} catch (NotFoundException $e) {
// silently return if no attachment dir was found
return;
}
$sourceAttachmentDir->delete();
}
/**
* @param File $source
* @param File $target
*
* @throws InvalidPathException
* @throws NoUserException
* @throws NotFoundException
* @throws NotPermittedException
* @throws LockedException
*/
public function copyAttachments(File $source, File $target): void {
try {
$sourceAttachmentDir = $this->getAttachmentDirectoryForFile($source);
} catch (NotFoundException $e) {
// silently return if no attachment dir was found for source file
return;
}
// create a new attachment dir next to the new file
$targetAttachmentDir = $this->getAttachmentDirectoryForFile($target, true);
// copy the attachment files
foreach ($sourceAttachmentDir->getDirectoryListing() as $sourceAttachment) {
if ($sourceAttachment instanceof File) {
$targetAttachmentDir->newFile($sourceAttachment->getName(), $sourceAttachment->getContent());
}
}
}
}