Skip to content

Commit

Permalink
Show better error messages for failed file uploads
Browse files Browse the repository at this point in the history
  • Loading branch information
eidens committed Jun 14, 2022
1 parent 8b88680 commit 4ff247f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
13 changes: 9 additions & 4 deletions app/Http/Controllers/API/FileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,15 @@ public function store(Panel $panel, Request $request)
{
$user = auth()->user();

$request->validate([
'file' => ['required_without:url', 'max:4096'],
'url' => ['required_without:file', 'url']
]);
$maxFileSizeInMB = 4;
$maxFileSizeInBytes = 4 * 1000;
$rules = [
'file' => ['required', 'mimes:jpeg,png,jpg,gif,pdf,tif', "max:$maxFileSizeInBytes"],
];
$messages = [
'file.max' => "Source files may not be larger than $maxFileSizeInMB MB",
];
$this->validate($request, $rules, $messages);

if (Gate::allows('modify-panel', $panel)) {

Expand Down
13 changes: 10 additions & 3 deletions app/Http/Controllers/API/PanelController.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,16 @@ public function listPublicGroupPanels(Request $request, Group $group)
*/
public function store(Request $request)
{
$request->validate([
'file' => ['required', 'mimes:jpeg,png,jpg,gif,pdf,tif', 'max:4096']
]);
$maxFileSizeInMegaBytes = 4;
$maxFileSizeInKiloBytes = 4 * 1000;
$rules = [
'file' => ['required_without:url', "max:$maxFileSizeInKiloBytes"],
'url' => ['required_without:file', 'url'],
];
$messages = [
'file.max' => "SmartFigure images may not be larger than $maxFileSizeInMegaBytes MB",
];
$this->validate($request, $rules, $messages);

$user = auth()->user();

Expand Down
2 changes: 1 addition & 1 deletion resources/js/components/files/FileUploads.vue
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ export default {
this.$store.commit('setPendingUpload', false);
this.file = null
}).catch(error => {
this.$snotify.error(error.data.message, "Upload failed")
this.$snotify.error(error.data.errors.file[0], "Upload failed")
this.$store.commit('setPendingUpload', false);
})
},
Expand Down

0 comments on commit 4ff247f

Please sign in to comment.