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
143 changes: 42 additions & 101 deletions lib/Service/AttachmentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
use OCP\Share\IShare;
use OCP\Util;

class AttachmentService {
readonly class AttachmentService {
public function __construct(
private IRootFolder $rootFolder,
private ShareManager $shareManager,
Expand Down Expand Up @@ -271,12 +271,8 @@ public function getAttachmentList(int $documentId, ?string $userId = null, ?Sess
/**
* 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
Expand All @@ -302,12 +298,8 @@ public function uploadAttachment(int $documentId, string $newFileName, $newFileR
/**
* 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
Expand Down Expand Up @@ -357,11 +349,6 @@ public function uploadAttachmentPublic(?int $documentId, string $newFileName, $n
/**
* 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
Expand All @@ -380,10 +367,6 @@ public function insertAttachmentFile(int $documentId, string $path, string $user
/**
* create a new file in the attachment folder
*
* @param int $documentId
* @param string $userId
*
* @return array
* @throws NotFoundException
* @throws NotPermittedException
* @throws InvalidPathException
Expand All @@ -407,11 +390,6 @@ public function createAttachmentFile(int $documentId, string $newFileName, strin
}

/**
* @param File $originalFile
* @param Folder $saveDir
* @param File $textFile
*
* @return array
* @throws NotFoundException
* @throws InvalidPathException
*/
Expand All @@ -430,11 +408,6 @@ private function copyFile(File $originalFile, Folder $saveDir, File $textFile):

/**
* 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);
Expand Down Expand Up @@ -471,10 +444,6 @@ private function hasUpdatePermissions(IShare $share): bool {
/**
* Get or create file-specific attachment folder
*
* @param File $textFile
* @param bool $create
*
* @return Folder
* @throws NotFoundException
* @throws NotPermittedException
* @throws InvalidPathException
Expand Down Expand Up @@ -505,6 +474,7 @@ private function getAttachmentDirectoryForFile(File $textFile, bool $create = fa

/**
* Get a user file from file ID
*
* @throws NotFoundException
* @throws NotPermittedException
* @throws NoUserException
Expand All @@ -521,9 +491,6 @@ private function getFileFromPath(string $filePath, string $userId): File {
}

/**
* @param File $file
*
* @return bool
* @throws NotFoundException
*/
private function isDownloadDisabled(File $file): bool {
Expand All @@ -543,10 +510,6 @@ private function isDownloadDisabled(File $file): bool {
/**
* Get a user file from file ID
*
* @param int $documentId
* @param string $userId
*
* @return File
* @throws NoUserException
* @throws NotFoundException
* @throws NotPermittedException
Expand All @@ -563,10 +526,6 @@ private function getTextFile(int $documentId, string $userId): File {
/**
* Get file from share token
*
* @param int|null $documentId
* @param string $shareToken
*
* @return File
* @throws NotFoundException
*/
private function getTextFilePublic(?int $documentId, string $shareToken): File {
Expand Down Expand Up @@ -599,8 +558,6 @@ private function getTextFilePublic(?int $documentId, string $shareToken): File {
/**
* Get share folder
*
* @param string $shareToken
*
* @throws NotFoundException
*/
private function getShareFolder(string $shareToken): ?Folder {
Expand All @@ -626,11 +583,8 @@ private function getShareFolder(string $shareToken): ?Folder {
}

/**
* Actually delete attachment files which are not pointed in the markdown content
* 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
Expand All @@ -640,6 +594,11 @@ private function getShareFolder(string $shareToken): ?Folder {
public function cleanupAttachments(int $fileId): int {
$textFile = $this->rootFolder->getFirstNodeById($fileId);
if ($textFile instanceof File) {
if ($textFile->getStorage()->instanceOfStorage(\OCA\Collectives\Mount\CollectiveStorage::class)) {
// Don't cleanup attachments for Collectives pages
return 0;
}

if ($textFile->getMimeType() === 'text/markdown') {
// get IDs of the files inside the attachment dir
try {
Expand Down Expand Up @@ -667,15 +626,11 @@ function ($node) use ($contentAttachmentFileIds, $contentAttachmentNames) {
}

/**
* Get attachment file ids listed in the markdown file content
*
* @param string $content
*
* @return array
* Get attachment file ids listed in the Markdown file content
*/
public static function getAttachmentIdsFromContent(string $content): array {
$matches = [];
// matches [ANY_CONSIDERED_CORRECT_BY_PHP-MARKDOWN](ANY_URL/f/FILE_ID and captures FILE_ID
// matches [ANY_CONSIDERED_CORRECT_BY_PHP-MARKDOWN](ANY_URL/f/FILE_ID) and captures FILE_ID
preg_match_all(
'/\[(?>[^\[\]]+|\[(?>[^\[\]]+|\[(?>[^\[\]]+|\[(?>[^\[\]]+|\[(?>[^\[\]]+|\[(?>[^\[\]]+|\[\])*\])*\])*\])*\])*\])*\]\(\S+\/f\/(\d+)/',
$content,
Expand All @@ -688,16 +643,11 @@ public static function getAttachmentIdsFromContent(string $content): array {
}

/**
* Get attachment file names listed in the markdown file content
*
* @param string $content
* @param int $fileId
*
* @return array
* Get attachment file names listed in the Markdown file content
*/
public static function getAttachmentNamesFromContent(string $content, int $fileId): array {
$matches = [];
// matches ![ANY_CONSIDERED_CORRECT_BY_PHP-MARKDOWN](.attachments.DOCUMENT_ID/ANY_FILE_NAME) and captures FILE_NAME
// matches ![ANY_CONSIDERED_CORRECT_BY_PHP-MARKDOWN](.attachments.DOCUMENT_ID/ANY_FILE_NAME) and captures ANY_FILE_NAME
preg_match_all(
'/\!\[(?>[^\[\]]+|\[(?>[^\[\]]+|\[(?>[^\[\]]+|\[(?>[^\[\]]+|\[(?>[^\[\]]+|\[(?>[^\[\]]+|\[\])*\])*\])*\])*\])*\])*\]\(\.attachments\.' . $fileId . '\/([^)&]+)\)/',
$content,
Expand All @@ -710,9 +660,36 @@ public static function getAttachmentNamesFromContent(string $content, int $fileI
}

/**
* @param File $source
* @param File $target
*
* @throws InvalidPathException
* @throws NoUserException
* @throws NotFoundException
* @throws NotPermittedException
* @throws LockedException
*/
public function copyAttachments(File $source, File $target): array {
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
$fileIdMapping = [];
foreach ($sourceAttachmentDir->getDirectoryListing() as $sourceAttachment) {
if ($sourceAttachment instanceof File) {
$newFile = $targetAttachmentDir->newFile($sourceAttachment->getName(), $sourceAttachment->getContent());
$fileIdMapping[] = [
$sourceAttachment->getId(),
$newFile->getId()
];
}
}
return $fileIdMapping;
}

/**
* @throws NotFoundException
* @throws NotPermittedException
* @throws InvalidPathException
Expand All @@ -737,8 +714,6 @@ public function moveAttachments(File $source, File $target): void {
}

/**
* @param File $source
*
* @throws NotFoundException
* @throws NotPermittedException
* @throws InvalidPathException
Expand All @@ -755,40 +730,6 @@ public function deleteAttachments(File $source): void {
$sourceAttachmentDir->delete();
}

/**
* @param File $source
* @param File $target
*
* @return array file id translation map
* @throws InvalidPathException
* @throws NoUserException
* @throws NotFoundException
* @throws NotPermittedException
* @throws LockedException
*/
public function copyAttachments(File $source, File $target): array {
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
$fileIdMapping = [];
foreach ($sourceAttachmentDir->getDirectoryListing() as $sourceAttachment) {
if ($sourceAttachment instanceof File) {
$newFile = $targetAttachmentDir->newFile($sourceAttachment->getName(), $sourceAttachment->getContent());
$fileIdMapping[] = [
$sourceAttachment->getId(),
$newFile->getId()
];
}
}
return $fileIdMapping;
}

public static function replaceAttachmentFolderId(File $source, File $target): void {
$sourceId = $source->getId();
$targetId = $target->getId();
Expand Down
2 changes: 1 addition & 1 deletion src/components/ViewerComponent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ export default defineComponent({
SourceView,
Editor,
},
inheritAttrs: false,
provide() {
return {
isEmbedded: this.isEmbedded,
}
},
inheritAttrs: false,
props: {
filename: {
type: String,
Expand Down
Loading
Loading