Skip to content

Commit

Permalink
fix(attachments): Show proper error message at upload error
Browse files Browse the repository at this point in the history
Fixes: #6269

Signed-off-by: Jonas <[email protected]>
  • Loading branch information
mejo- committed Aug 28, 2024
1 parent 25e7cff commit 4cc321f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
7 changes: 6 additions & 1 deletion lib/Controller/AttachmentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http\RedirectResponse;
use OCP\Files\IMimeTypeDetector;
use OCP\Files\InvalidPathException;
use OCP\IL10N;
use OCP\IRequest;
use OCP\Util;
Expand Down Expand Up @@ -133,7 +134,11 @@ public function uploadAttachment(string $token = ''): DataResponse {
}
}
return new DataResponse(['error' => 'No uploaded file'], Http::STATUS_BAD_REQUEST);
} catch (Exception $e) {
} catch (InvalidPathException $e) {
$this->logger->error('Upload error', ['exception' => $e]);
$error = $e->getMessage() ?: 'Upload error';
return new DataResponse(['error' => $error], Http::STATUS_BAD_REQUEST);
} catch (Exception) {
$this->logger->error('Upload error', ['exception' => $e]);

Check failure on line 142 in lib/Controller/AttachmentController.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis

UndefinedVariable

lib/Controller/AttachmentController.php:142:57: UndefinedVariable: Cannot find referenced variable $e (see https://psalm.dev/024)
return new DataResponse(['error' => 'Upload error'], Http::STATUS_BAD_REQUEST);
}
Expand Down
6 changes: 5 additions & 1 deletion lib/Service/AttachmentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use OCP\Constants;
use OCP\Files\File;
use OCP\Files\Folder;
use OCP\Files\IFilenameValidator;
use OCP\Files\IMimeTypeDetector;
use OCP\Files\InvalidPathException;
use OCP\Files\IRootFolder;
Expand All @@ -35,7 +36,8 @@ public function __construct(private IRootFolder $rootFolder,
private ShareManager $shareManager,
private IPreview $previewManager,
private IMimeTypeDetector $mimeTypeDetector,
private IURLGenerator $urlGenerator) {
private IURLGenerator $urlGenerator,
private IFilenameValidator $filenameValidator) {
}

/**
Expand Down Expand Up @@ -263,6 +265,7 @@ public function uploadAttachment(int $documentId, string $newFileName, $newFileR
}
$saveDir = $this->getAttachmentDirectoryForFile($textFile, true);
$fileName = self::getUniqueFileName($saveDir, $newFileName);
$this->filenameValidator->validateFilename($fileName);
$savedFile = $saveDir->newFile($fileName, $newFileResource);
return [
'name' => $fileName,
Expand Down Expand Up @@ -293,6 +296,7 @@ public function uploadAttachmentPublic(?int $documentId, string $newFileName, $n
$textFile = $this->getTextFilePublic($documentId, $shareToken);
$saveDir = $this->getAttachmentDirectoryForFile($textFile, true);
$fileName = self::getUniqueFileName($saveDir, $newFileName);
$this->filenameValidator->validateFilename($fileName);
$savedFile = $saveDir->newFile($fileName, $newFileResource);
return [
'name' => $fileName,
Expand Down
6 changes: 5 additions & 1 deletion src/components/Editor/MediaHandler.vue
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,11 @@ export default {
})
.catch((error) => {
logger.error('Uploading attachment failed', { error })
showError(t('text', 'Uploading attachment failed.'))
if (error.response?.data.error) {
showError(t('text', 'Uploading attachment failed: {error}', { error: error.response.data.error }))
} else {
showError(t('text', 'Uploading attachment failed.'))
}
})
.then(() => {
this.state.isUploadingAttachments = false
Expand Down

0 comments on commit 4cc321f

Please sign in to comment.