Skip to content
Open
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
14 changes: 11 additions & 3 deletions apps/files/src/views/ReferenceFileWidget.vue
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@

<script lang="ts">
import { defineComponent, type Component, type PropType } from 'vue'
import { generateRemoteUrl, generateUrl } from '@nextcloud/router'
import { generateRemoteUrl, generateUrl, getBaseUrl } from '@nextcloud/router'
import { getCurrentUser } from '@nextcloud/auth'
import { getFilePickerBuilder } from '@nextcloud/dialogs'
import { Node } from '@nextcloud/files'
Expand All @@ -73,6 +73,7 @@ type Ressource = {
mimetype: string
mtime: number // as unix timestamp
'preview-available': boolean
'is-public-link': boolean
}

type ViewerHandler = {
Expand Down Expand Up @@ -137,8 +138,11 @@ export default defineComponent({
.find(handler => handler.mimes.includes(this.richObject.mimetype))
},
viewerFile(): ViewerFile {
const davSource = generateRemoteUrl(`dav/files/${getCurrentUser()?.uid}/${this.richObject.path}`)
.replace(/\/\/$/, '/')
const davSource = this.richObject['is-public-link']
? (getBaseUrl() + `/public.php/dav/files/${this.richObject.path}`)
: generateRemoteUrl(`dav/files/${getCurrentUser()?.uid}/${this.richObject.path}`)
.replace(/\/\/$/, '/')

return {
filename: this.richObject.path,
basename: this.richObject.name,
Expand Down Expand Up @@ -200,6 +204,10 @@ export default defineComponent({
},
methods: {
navigate(event) {
if (this.richObject['is-public-link']) {
return
}

if (this.isFolder) {
event.stopPropagation()
event.preventDefault()
Expand Down
132 changes: 113 additions & 19 deletions lib/private/Collaboration/Reference/File/FileReferenceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use OC\User\NoUserException;
use OCP\Collaboration\Reference\ADiscoverableReferenceProvider;
use OCP\Collaboration\Reference\IPublicReferenceProvider;
use OCP\Collaboration\Reference\IReference;
use OCP\Collaboration\Reference\Reference;
use OCP\Files\IMimeTypeDetector;
Expand All @@ -22,8 +23,13 @@
use OCP\IURLGenerator;
use OCP\IUserSession;
use OCP\L10N\IFactory;
use OCP\Share\Exceptions\ShareNotFound;
use OCP\Share\IManager as ShareManager;

class FileReferenceProvider extends ADiscoverableReferenceProvider implements IPublicReferenceProvider {
private const PREVIEW_WIDTH = 1600;
private const PREVIEW_HEIGHT = 630;

class FileReferenceProvider extends ADiscoverableReferenceProvider {
private ?string $userId;
private IL10N $l10n;

Expand All @@ -34,13 +40,14 @@ public function __construct(
private IMimeTypeDetector $mimeTypeDetector,
private IPreview $previewManager,
IFactory $l10n,
private ShareManager $shareManager,
) {
$this->userId = $userSession->getUser()?->getUID();
$this->l10n = $l10n->get('files');
}

public function matchReference(string $referenceText): bool {
return $this->getFilesAppLinkId($referenceText) !== null;
return $this->getFilesAppLinkId($referenceText) !== null || $this->getFilesAppPublicLinkToken($referenceText) !== null;
}

private function getFilesAppLinkId(string $referenceText): ?int {
Expand Down Expand Up @@ -74,34 +81,105 @@ private function getFilesAppLinkId(string $referenceText): ?int {
return $fileId !== null ? (int)$fileId : null;
}

private function getFilesAppPublicLinkToken(string $referenceText): ?string {
$prefixes = ['/index.php/s/', '/s/'];

$token = null;

foreach ($prefixes as $prefix) {
$fullPrefix = $this->urlGenerator->getAbsoluteURL($prefix);
if (mb_strpos($referenceText, $fullPrefix) === 0) {
$token = substr($referenceText, mb_strlen($fullPrefix));
if (($slashPos = strpos($token, '/')) !== false) {
$token = substr($token, 0, $slashPos);
}
break;
}
}

return $token;
}

public function resolveReference(string $referenceText): ?IReference {
if ($this->matchReference($referenceText)) {
$reference = new Reference($referenceText);
try {
$this->fetchReference($reference);
} catch (NotFoundException $e) {
$reference->setRichObject('file', null);
$reference->setAccessible(false);
$reference = new Reference($referenceText);
try {
$fileId = $this->getFilesAppLinkId($referenceText);

if ($fileId !== null) {
$this->fetchReference($reference, $fileId);
} else {
$fileToken = $this->getFilesAppPublicLinkToken($referenceText);
if ($fileToken !== null) {
$this->fetchReferenceForPublicFile($reference, $referenceText, $fileToken);
} else {
throw new NotFoundException();
}
}
return $reference;
} catch (NotFoundException $e) {
$reference->setRichObject('file', null);
$reference->setAccessible(false);
}
return $reference;
}

return null;
public function resolveReferencePublic(string $referenceText, string $sharingToken): ?IReference {
$reference = new Reference($referenceText);
try {
$fileToken = $this->getFilesAppPublicLinkToken($referenceText);
if ($fileToken !== null) {
$this->fetchReferenceForPublicFile($reference, $referenceText, $fileToken);
} else {
throw new NotFoundException();
}
} catch (NotFoundException $e) {
$reference->setRichObject('file', null);
$reference->setAccessible(false);
}
return $reference;
}

private function fetchReferenceForPublicFile(Reference $reference, string $referenceText, string $fileToken): void {
try {
$share = $this->shareManager->getShareByToken($fileToken);
$node = $share->getNode();

$reference->setTitle($node->getName());
$reference->setDescription($node->getMimetype());
$reference->setUrl($referenceText);
if ($this->previewManager->isMimeSupported($node->getMimeType())) {
$reference->setImageUrl($this->urlGenerator->linkToRouteAbsolute(
'files_sharing.PublicPreview.getPreview',
['x' => self::PREVIEW_WIDTH, 'y' => self::PREVIEW_HEIGHT, 'token' => $fileToken]));
} else {
$fileTypeIconUrl = $this->mimeTypeDetector->mimeTypeIcon($node->getMimeType());
$reference->setImageUrl($fileTypeIconUrl);
}

$reference->setRichObject('file', [
'id' => $fileToken, // security, public link should not show file id
'name' => $node->getName(),
'size' => $node->getSize(),
'path' => $fileToken,
'link' => $reference->getUrl(),
'mimetype' => $node->getMimetype(),
'mtime' => $node->getMTime(),
'preview-available' => $this->previewManager->isAvailable($node),
'is-public-link' => true,
]);
} catch (ShareNotFound|NotFoundException|InvalidPathException|NotPermittedException $e) {
$reference->setRichObject('file', null);
$reference->setAccessible(false);
}
}

/**
* @throws NotFoundException
*/
private function fetchReference(Reference $reference): void {
private function fetchReference(Reference $reference, int $fileId): void {
if ($this->userId === null) {
throw new NotFoundException();
}

$fileId = $this->getFilesAppLinkId($reference->getId());
if ($fileId === null) {
throw new NotFoundException();
}

try {
$userFolder = $this->rootFolder->getUserFolder($this->userId);
$file = $userFolder->getFirstNodeById($fileId);
Expand All @@ -114,7 +192,10 @@ private function fetchReference(Reference $reference): void {
$reference->setDescription($file->getMimetype());
$reference->setUrl($this->urlGenerator->getAbsoluteURL('/index.php/f/' . $fileId));
if ($this->previewManager->isMimeSupported($file->getMimeType())) {
$reference->setImageUrl($this->urlGenerator->linkToRouteAbsolute('core.Preview.getPreviewByFileId', ['x' => 1600, 'y' => 630, 'fileId' => $fileId]));
$reference->setImageUrl($this->urlGenerator->linkToRouteAbsolute(
'core.Preview.getPreviewByFileId',
['x' => self::PREVIEW_WIDTH, 'y' => self::PREVIEW_HEIGHT, 'fileId' => $fileId])
);
} else {
$fileTypeIconUrl = $this->mimeTypeDetector->mimeTypeIcon($file->getMimeType());
$reference->setImageUrl($fileTypeIconUrl);
Expand All @@ -136,13 +217,26 @@ private function fetchReference(Reference $reference): void {
}

public function getCachePrefix(string $referenceId): string {
return (string)$this->getFilesAppLinkId($referenceId);
$fileId = $this->getFilesAppLinkId($referenceId);
if ($fileId !== null) {
return (string)$fileId;
} else {
$fileToken = $this->getFilesAppPublicLinkToken($referenceId);
if ($fileToken !== null) {
return $fileToken;
}
}
return '';
}

public function getCacheKey(string $referenceId): ?string {
return $this->userId ?? '';
}

public function getCacheKeyPublic(string $referenceId, string $sharingToken): ?string {
return $sharingToken;
}

public function getId(): string {
return 'files';
}
Expand Down
Loading
Loading