Skip to content

Commit

Permalink
Merge pull request #6279 from nextcloud/backport/6271/stable30
Browse files Browse the repository at this point in the history
[stable30] fix(attachments): Show proper error message at upload error
  • Loading branch information
juliusknorr authored Aug 28, 2024
2 parents 632a163 + 58aba23 commit 5261eb6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
5 changes: 5 additions & 0 deletions 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,6 +134,10 @@ public function uploadAttachment(string $token = ''): DataResponse {
}
}
return new DataResponse(['error' => 'No uploaded file'], Http::STATUS_BAD_REQUEST);
} 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 $e) {
$this->logger->error('Upload error', ['exception' => $e]);
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 5261eb6

Please sign in to comment.