Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/Controller/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use OCP\AppFramework\OCS\OCSForbiddenException;
use OCP\AppFramework\OCS\OCSNotFoundException;
use OCP\AppFramework\OCSController;
use OCP\Files\IRootFolder;
use OCP\IRequest;
use Psr\Log\LoggerInterface;

Expand All @@ -37,6 +38,7 @@ public function __construct(
string $appName,
IRequest $request,
private PageService $service,
private IRootFolder $rootFolder,
private AttachmentService $attachmentService,
private SearchService $indexedSearchService,
private CollectiveService $collectiveService,
Expand Down Expand Up @@ -266,7 +268,9 @@ public function trash(int $collectiveId, int $id): DataResponse {
*/
#[NoAdminRequired]
public function getAttachments(int $collectiveId, int $id): DataResponse {
$attachments = $this->handleErrorResponse(fn (): array => $this->attachmentService->getAttachments($collectiveId, $id, $this->userId), $this->logger);
$pageFile = $this->service->getPageFile($collectiveId, $id, $this->userId);
$userFolder = $this->rootFolder->getUserFolder($this->userId);
$attachments = $this->handleErrorResponse(fn (): array => $this->attachmentService->getAttachments($pageFile, $userFolder), $this->logger);
return new DataResponse(['attachments' => $attachments]);
}

Expand Down
4 changes: 3 additions & 1 deletion lib/Controller/PublicPageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,9 @@ public function getAttachments(int $id): DataResponse {
if (0 !== $sharePageId = $this->getCollectiveShare()->getPageId()) {
$this->checkPageShareAccess($collectiveId, $sharePageId, $id, $owner);
}
return $this->attachmentService->getAttachments($collectiveId, $id, $owner);
$pageFile = $this->service->getPageFile($collectiveId, $id, $owner);
$shareFolder = $this->getShare()->getNode();
return $this->attachmentService->getAttachments($pageFile, $shareFolder);
}, $this->logger);
return new DataResponse(['attachments' => $attachments]);
}
Expand Down
5 changes: 4 additions & 1 deletion lib/Controller/PublicStartController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use OCA\Collectives\Db\CollectiveShareMapper;
use OCA\DAV\Connector\Sabre\PublicAuth;
use OCA\Files_Sharing\Event\ShareLinkAccessedEvent;
use OCA\Viewer\Event\LoadViewer;
use OCP\App\IAppManager;
use OCP\AppFramework\AuthPublicShareController;
Expand Down Expand Up @@ -110,10 +111,12 @@ public function showShare(): PublicTemplateResponse {
if ($appsMissing = $this->checkDependencies()) {
return new PublicTemplateResponse('collectives', 'error', ['appsMissing' => $appsMissing]); // templates/error.php
}
$this->eventDispatcher->dispatch(LoadViewer::class, new LoadViewer());
$this->eventDispatcher->dispatchTyped(new LoadViewer());
$this->eventDispatcher->dispatchTyped(new ShareLinkAccessedEvent($this->getShare(), 'show'));
$response = new PublicTemplateResponse('collectives', 'main', [ // templates/main.php
'id-app-content' => '#app-content-vue',
'id-app-navigation' => '#app-navigation-vue',
'token' => $this->getToken(),
]);
$response->setFooterVisible(false);
return $response;
Expand Down
13 changes: 7 additions & 6 deletions lib/Service/AttachmentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,22 @@

class AttachmentService {
public function __construct(
private PageService $pageService,
private IPreview $preview,
) {
}

/**
* @throws NotFoundException
*/
private function fileToInfo(File $file, string $userId): array {
private function fileToInfo(File $file, folder $folder): array {
try {
return [
'id' => $file->getId(),
'name' => $file->getName(),
'filesize' => $file->getSize(),
'mimetype' => $file->getMimeType(),
'timestamp' => $file->getMTime(),
'path' => substr($file->getPath(), strlen('/' . $userId . '/files')),
'path' => $folder->getRelativePath($file->getPath()),
'internalPath' => $file->getInternalPath(),
'hasPreview' => $this->preview->isAvailable($file),
];
Expand Down Expand Up @@ -62,12 +61,14 @@ private function getAttachmentDirectory(File $pageFile): Folder {
}

/**
* @param File $pageFile file of the page with the attachments.
* @param Folder $folder user or share folder for relative paths.
*
* @throws MissingDependencyException
* @throws NotFoundException
* @throws NotPermittedException
*/
public function getAttachments(int $collectiveId, int $pageId, string $userId): array {
$pageFile = $this->pageService->getPageFile($collectiveId, $pageId, $userId);
public function getAttachments(File $pageFile, Folder $folder): array {
try {
$attachmentDir = $this->getAttachmentDirectory($pageFile);
} catch (NotFoundException) {
Expand All @@ -76,6 +77,6 @@ public function getAttachments(int $collectiveId, int $pageId, string $userId):
}

// Only return files, ignore folders
return array_map(fn ($file) => $this->fileToInfo($file, $userId), array_filter($attachmentDir->getDirectoryListing(), static fn ($node) => $node instanceof File));
return array_map(fn ($file) => $this->fileToInfo($file, $folder), array_filter($attachmentDir->getDirectoryListing(), static fn ($node) => $node instanceof File));
}
}
8 changes: 3 additions & 5 deletions src/components/Nav/TemplatesDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<template>
<NcDialog :name="t('collectives', 'Templates')"
data-cy-collectives="templates-dialog"
class="templates-dialog"
size="normal"
@closing="onClose">
<SkeletonLoading v-if="loading(`template-list-${templatesCollectiveId}`)" type="items" />
Expand Down Expand Up @@ -132,11 +133,8 @@ export default {
align-items: center;
padding-inline: 12px;
}
</style>

<style lang="scss">
#viewer {
.templates-dialog {
// Make viewer modal overlay the templates dialog modal
z-index: 9999;
z-index: 9997;
}
</style>
2 changes: 1 addition & 1 deletion src/components/PageSidebar/SidebarTabAttachments.vue
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ export default {

clickAttachment(attachment, ev) {
// Show in viewer if the mimetype is supported
if (!this.isPublic && window.OCA.Viewer.availableHandlers.map(handler => handler.mimes).flat().includes(attachment.mimetype)) {
if (window.OCA.Viewer?.availableHandlers.map(handler => handler.mimes).flat().includes(attachment.mimetype)) {
ev.preventDefault()
window.OCA.Viewer.open({ path: attachment.path })
}
Expand Down
4 changes: 4 additions & 0 deletions templates/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@

<div id="q-app"></div>

<?php if (isset($_['token'])) {
print_unescaped('<input id="isPublic" type="hidden" name="isPublic" value="1">');
print_unescaped('<input id="sharingToken" type="hidden" value="' . $_['token'] . '">');
} ?>
32 changes: 14 additions & 18 deletions tests/Unit/Service/AttachmentServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,32 @@
use OC\Files\Node\File;
use OC\Files\Node\Folder;
use OCA\Collectives\Service\AttachmentService;
use OCA\Collectives\Service\PageService;
use OCP\IPreview;
use PHPUnit\Framework\TestCase;

class AttachmentServiceTest extends TestCase {
private AttachmentService $service;
private string $userId = 'jane';
private int $collectiveId = 1;
private File $pageFile;
private Folder $parentFolder;
private string $attachmentFolderName = '.attachments.' . 1;

protected function setUp(): void {
$pageService = $this->getMockBuilder(PageService::class)
->disableOriginalConstructor()
->getMock();

$preview = $this->getMockBuilder(IPreview::class)
->disableOriginalConstructor()
->getMock();

$file = $this->getMockBuilder(File::class)
$this->pageFile = $this->getMockBuilder(File::class)
->disableOriginalConstructor()
->getMock();
$parentFolder = $this->getMockBuilder(Folder::class)
$this->parentFolder = $this->getMockBuilder(Folder::class)
->disableOriginalConstructor()
->getMock();
$parentFolder->method('nodeExists')
$this->parentFolder->method('nodeExists')
->with($this->attachmentFolderName)
->willReturn(true);
$this->parentFolder->method('getRelativePath')
->willReturn('/Collectives/x/path/to/' . $this->attachmentFolderName . '/attachmentFile1');
$attachmentFolder = $this->getMockBuilder(Folder::class)
->disableOriginalConstructor()
->getMock();
Expand All @@ -54,31 +52,29 @@ protected function setUp(): void {
->willReturn('/path/to/' . $this->attachmentFolderName . '/attachmentFile1');
$attachmentFolder->method('getDirectoryListing')
->willReturn([$attachmentFile]);
$parentFolder->method('get')
$this->parentFolder->method('get')
->with($this->attachmentFolderName)
->willReturn($attachmentFolder);
$file->method('getParent')
->willReturn($parentFolder);
$file->method('getId')
$this->pageFile->method('getParent')
->willReturn($this->parentFolder);
$this->pageFile->method('getId')
->willReturn(1);
$pageService->method('getPageFile')
->willReturn($file);

$this->service = new AttachmentService($pageService, $preview);
$this->service = new AttachmentService($preview);
}

public function testGetAttachments(): void {
$attachmentInfo = [
'id' => 2,
'name' => null,
'filesize' => null,
'filesize' => 0.0,
'mimetype' => null,
'timestamp' => null,
'path' => '/Collectives/x/path/to/' . $this->attachmentFolderName . '/attachmentFile1',
'internalPath' => '/path/to/' . $this->attachmentFolderName . '/attachmentFile1',
'hasPreview' => null,
];

self::assertEquals([$attachmentInfo], $this->service->getAttachments($this->collectiveId, 1, $this->userId));
self::assertEquals([$attachmentInfo], $this->service->getAttachments($this->pageFile, $this->parentFolder));
}
}
6 changes: 6 additions & 0 deletions tests/stub.phpstub
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,12 @@ namespace OC\Files\Storage\Wrapper{
}
}

namespace OCA\Files_Sharing\Event {
class ShareLinkAccessedEvent extends \OCP\EventDispatcher\Event {
public function __construct(\OCP\Share\IShare $share, string $step = '', int $errorCode = 200, string $errorMessage = '') {}
}
}

namespace OCA\Viewer\Event {
class LoadViewer extends \OCP\EventDispatcher\Event {}
}
Expand Down
Loading