Skip to content

Commit 9b4fbaf

Browse files
committed
Move to dedicated file attachment service
Signed-off-by: Julius Härtl <jus@bitgrid.net>
1 parent 5a53c31 commit 9b4fbaf

8 files changed

Lines changed: 381 additions & 94 deletions

File tree

lib/Service/AttachmentService.php

Lines changed: 45 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@ class AttachmentService {
6262
private $activityManager;
6363
/** @var ChangeHelper */
6464
private $changeHelper;
65-
/** @var DeckShareProvider */
66-
private $shareProvider;
6765

6866
public function __construct(AttachmentMapper $attachmentMapper, CardMapper $cardMapper, ChangeHelper $changeHelper, PermissionService $permissionService, Application $application, ICacheFactory $cacheFactory, $userId, IL10N $l10n, ActivityManager $activityManager, DeckShareProvider $shareProvider) {
6967
$this->attachmentMapper = $attachmentMapper;
@@ -75,11 +73,11 @@ public function __construct(AttachmentMapper $attachmentMapper, CardMapper $card
7573
$this->l10n = $l10n;
7674
$this->activityManager = $activityManager;
7775
$this->changeHelper = $changeHelper;
78-
$this->shareProvider = $shareProvider;
7976

8077
// Register shipped attachment services
8178
// TODO: move this to a plugin based approach once we have different types of attachments
8279
$this->registerAttachmentService('deck_file', FileService::class);
80+
$this->registerAttachmentService('file', FilesAppService::class);
8381
}
8482

8583
/**
@@ -120,6 +118,15 @@ public function findAll($cardId, $withDeleted = false) {
120118
if ($withDeleted) {
121119
$attachments = array_merge($attachments, $this->attachmentMapper->findToDelete($cardId, false));
122120
}
121+
122+
foreach (array_keys($this->services) as $attachmentType) {
123+
/** @var IAttachmentService $service */
124+
$service = $this->getService($attachmentType);
125+
if ($service instanceof ICustomAttachmentService) {
126+
$attachments = array_merge($attachments, $service->listAttachments($cardId));
127+
}
128+
}
129+
123130
foreach ($attachments as &$attachment) {
124131
try {
125132
$service = $this->getService($attachment->getType());
@@ -129,37 +136,7 @@ public function findAll($cardId, $withDeleted = false) {
129136
}
130137
}
131138

132-
return array_merge($attachments, $this->getFilesAppAttachments($cardId));
133-
}
134-
135-
private function getFilesAppAttachments($cardId) {
136-
/** @var IPreview $previewManager */
137-
$previewManager = \OC::$server->get(IPreview::class);
138-
$userFolder = \OC::$server->getRootFolder()->getUserFolder($this->userId);
139-
$shares = $this->shareProvider->getSharedWithByType($cardId, IShare::TYPE_DECK, -1, 0);
140-
return array_map(function (IShare $share) use ($cardId, $userFolder, $previewManager) {
141-
$file = $share->getNode();
142-
$nodes = $userFolder->getById($file->getId());
143-
$userNode = array_shift($nodes);
144-
return [
145-
// general attachment attributes
146-
'cardId' => $cardId,
147-
'type' => 'file',
148-
'data' => $file->getName(),
149-
'lastModified' => $file->getMTime(),
150-
'createdAt' => $file->getMTime(),
151-
'deletedAt' => 0,
152-
// file type attributes
153-
'fileid' => $file->getId(),
154-
'path' => $userFolder->getRelativePath($userNode->getPath()),
155-
'extendedData' => [
156-
'filesize' => $file->getSize(),
157-
'mimetype' => $file->getMimeType(),
158-
'info' => pathinfo($file->getName()),
159-
'hasPreview' => $previewManager->isAvailable($file),
160-
]
161-
];
162-
}, $shares);
139+
return $attachments;
163140
}
164141

165142
/**
@@ -178,22 +155,7 @@ public function count($cardId) {
178155
$this->cache->set('card-' . $cardId, $count);
179156
}
180157

181-
/** @var IDBConnection $qb */
182-
$db = \OC::$server->getDatabaseConnection();
183-
$qb = $db->getQueryBuilder();
184-
$qb->select($qb->createFunction('count(s.id)'))
185-
->from('share', 's')
186-
->andWhere($qb->expr()->eq('s.share_type', $qb->createNamedParameter(IShare::TYPE_DECK)))
187-
->andWhere($qb->expr()->eq('s.share_with', $qb->createNamedParameter($cardId)))
188-
->andWhere($qb->expr()->isNull('s.parent'))
189-
->andWhere($qb->expr()->orX(
190-
$qb->expr()->eq('s.item_type', $qb->createNamedParameter('file')),
191-
$qb->expr()->eq('s.item_type', $qb->createNamedParameter('folder'))
192-
));
193-
194-
$cursor = $qb->execute();
195-
$count += $cursor->fetchColumn(0);
196-
$cursor->closeCursor();
158+
197159

198160
return $count;
199161
}
@@ -234,21 +196,21 @@ public function create($cardId, $type, $data) {
234196
try {
235197
$service = $this->getService($attachment->getType());
236198
$service->create($attachment);
237-
} catch (InvalidAttachmentType $e) {
238-
// just store the data
239-
}
240-
if ($attachment->getData() === null) {
241-
throw new StatusException($this->l10n->t('No data was provided to create an attachment.'));
242-
}
243-
$attachment = $this->attachmentMapper->insert($attachment);
244199

245-
// extend data so the frontend can use it properly after creating
246-
try {
247-
$service = $this->getService($attachment->getType());
200+
if (!$service instanceof ICustomAttachmentService) {
201+
if ($attachment->getData() === null) {
202+
throw new StatusException($this->l10n->t('No data was provided to create an attachment.'));
203+
}
204+
205+
$attachment = $this->attachmentMapper->insert($attachment);
206+
}
207+
248208
$service->extendData($attachment);
209+
249210
} catch (InvalidAttachmentType $e) {
250211
// just store the data
251212
}
213+
252214
$this->changeHelper->cardChanged($attachment->getCardId());
253215
$this->activityManager->triggerEvent(ActivityManager::DECK_OBJECT_CARD, $attachment, ActivityManager::SUBJECT_ATTACHMENT_CREATE);
254216
return $attachment;
@@ -260,42 +222,48 @@ public function create($cardId, $type, $data) {
260222
*
261223
* @param $attachmentId
262224
* @return Response
263-
* @throws BadRequestException
264225
* @throws NoPermissionException
265226
* @throws NotFoundException
266-
* @throws \OCP\AppFramework\Db\DoesNotExistException
267-
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
268227
*/
269228
public function display($attachmentId) {
270-
if (is_numeric($attachmentId) === false) {
271-
throw new BadRequestException('attachment id must be a number');
272-
}
229+
if (is_numeric($attachmentId)) {
230+
try {
231+
$attachment = $this->attachmentMapper->find($attachmentId);
232+
} catch (\Exception $e) {
233+
throw new NoPermissionException('Permission denied');
234+
}
235+
$this->permissionService->checkPermission($this->cardMapper, $attachment->getCardId(), Acl::PERMISSION_READ);
273236

274-
try {
275-
$attachment = $this->attachmentMapper->find($attachmentId);
276-
} catch (\Exception $e) {
277-
throw new NoPermissionException('Permission denied');
237+
try {
238+
$service = $this->getService($attachment->getType());
239+
return $service->display($attachment);
240+
} catch (InvalidAttachmentType $e) {
241+
throw new NotFoundException();
242+
}
278243
}
279-
$this->permissionService->checkPermission($this->cardMapper, $attachment->getCardId(), Acl::PERMISSION_READ);
244+
245+
[$type, $attachmentId] = explode(':', $attachmentId);
280246

281247
try {
282-
$service = $this->getService($attachment->getType());
248+
$attachment = new Attachment();
249+
$attachment->setId($attachmentId);
250+
$attachment->setType($type);
251+
$service = $this->getService($type);
283252
return $service->display($attachment);
284-
} catch (InvalidAttachmentType $e) {
253+
} catch (\Exception $e) {
285254
throw new NotFoundException();
286255
}
256+
287257
}
288258

289259
/**
290260
* Update an attachment with custom data
291261
*
292262
* @param $attachmentId
293-
* @param $request
263+
* @param $data
294264
* @return mixed
295-
* @throws \OCA\Deck\NoPermissionException
296-
* @throws \OCP\AppFramework\Db\DoesNotExistException
297-
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
298265
* @throws BadRequestException
266+
* @throws NoPermissionException
299267
*/
300268
public function update($attachmentId, $data) {
301269
if (is_numeric($attachmentId) === false) {

lib/Service/ConfigService.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,8 @@ private function getGroupLimit() {
148148
}, $groups);
149149
return array_filter($groups);
150150
}
151+
152+
public function getAttachmentFolder(): string {
153+
return $this->config->getUserValue($this->userId, 'deck', 'attachment_folder', '/Deck');
154+
}
151155
}

lib/Service/FileService.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
use OCP\IL10N;
4141
use OCP\ILogger;
4242
use OCP\IRequest;
43+
use OCP\Share\IManager;
4344

4445
class FileService implements IAttachmentService {
4546
private $l10n;
@@ -57,7 +58,8 @@ public function __construct(
5758
ILogger $logger,
5859
IRootFolder $rootFolder,
5960
IConfig $config,
60-
AttachmentMapper $attachmentMapper
61+
AttachmentMapper $attachmentMapper,
62+
IManager $shareManager
6163
) {
6264
$this->l10n = $l10n;
6365
$this->appData = $appData;
@@ -66,6 +68,7 @@ public function __construct(
6668
$this->rootFolder = $rootFolder;
6769
$this->config = $config;
6870
$this->attachmentMapper = $attachmentMapper;
71+
$this->shareManager = $shareManager;
6972
}
7073

7174
/**

0 commit comments

Comments
 (0)