Skip to content

Commit 7b89ebd

Browse files
madassdevJhumanJ
andauthored
file upload max size feature (#328)
* file upload max size feature * Change label --------- Co-authored-by: Julien Nahum <julien@nahum.net>
1 parent bf95096 commit 7b89ebd

File tree

4 files changed

+27
-2
lines changed

4 files changed

+27
-2
lines changed

app/Http/Requests/AnswerFormRequest.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ public function __construct(Request $request)
2727
$this->maxFileSize = $this->form->workspace->max_file_size;
2828
}
2929

30+
private function getFieldMaxFileSize($fieldProps)
31+
{
32+
return array_key_exists('max_file_size', $fieldProps) ?
33+
min($fieldProps['max_file_size'] * 1000000, $this->maxFileSize) : $this->maxFileSize;
34+
}
35+
3036
/**
3137
* Validate form before use it
3238
*
@@ -180,7 +186,7 @@ private function getPropertyRules($property): array
180186
if (! empty($property['allowed_file_types'])) {
181187
$allowedFileTypes = explode(',', $property['allowed_file_types']);
182188
}
183-
$this->requestRules[$property['id'].'.*'] = [new StorageFile($this->maxFileSize, $allowedFileTypes, $this->form)];
189+
$this->requestRules[$property['id'] . '.*'] = [new StorageFile($this->getFieldMaxFileSize($property), $allowedFileTypes, $this->form)];
184190

185191
return ['array'];
186192
case 'email':

app/Http/Requests/UserFormRequest.php

+3
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ public function rules()
118118
'properties.*.generates_uuid' => 'boolean|nullable',
119119
'properties.*.generates_auto_increment_id' => 'boolean|nullable',
120120

121+
// For file (min and max)
122+
'properties.*.max_file_size' => 'min:1|numeric',
123+
121124
// Security & Privacy
122125
'can_be_indexed' => 'boolean',
123126
'password' => 'sometimes|nullable',

client/components/open/forms/OpenFormField.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ export default {
303303
}
304304
} else if (field.type === 'files' || (field.type === 'url' && field.file_upload)) {
305305
inputProperties.multiple = (field.multiple !== undefined && field.multiple)
306-
inputProperties.mbLimit = this.form?.max_file_size ?? this.currentWorkspace?.max_file_size
306+
inputProperties.mbLimit = Math.min(Math.max(field.max_file_size, 1), this.form?.max_file_size ?? this.currentWorkspace?.max_file_size)
307307
inputProperties.accept = (this.form.is_pro && field.allowed_file_types) ? field.allowed_file_types : ''
308308
} else if (field.type === 'number' && field.is_rating) {
309309
inputProperties.numberOfStars = parseInt(field.rating_max_value)

client/components/open/forms/fields/components/FieldOptions.vue

+16
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@
6060
label="Allowed file types" placeholder="jpg,jpeg,png,gif"
6161
help="Comma separated values, leave blank to allow all file types"
6262
/>
63+
64+
<text-input name="max_file_size" class="mt-3" :form="field" native-type="number"
65+
:min="1"
66+
:max="mbLimit"
67+
label="Maximum file size (in MB)" :placeholder="`1MB - ${mbLimit}MB`"
68+
help="Set the maximum file size that can be uploaded"
69+
/>
6370
</div>
6471

6572
<!-- Number Options -->
@@ -428,6 +435,9 @@ export default {
428435
required: false
429436
}
430437
},
438+
setup() {
439+
return { currentWorkspace: computed(() => useWorkspacesStore().getCurrent), }
440+
},
431441
data () {
432442
return {
433443
typesWithoutPlaceholder: ['date', 'checkbox', 'files'],
@@ -442,6 +452,9 @@ export default {
442452
hasPlaceholder () {
443453
return !this.typesWithoutPlaceholder.includes(this.field.type)
444454
},
455+
mbLimit() {
456+
return this.form?.max_file_size ?? this.currentWorkspace?.max_file_size
457+
},
445458
prefillSelectsOptions () {
446459
if (!['select', 'multi_select'].includes(this.field.type)) return {}
447460
@@ -504,6 +517,9 @@ export default {
504517
if (['text', 'number', 'url', 'email'].includes(this.field?.type) && !this.field?.max_char_limit) {
505518
this.field.max_char_limit = 2000
506519
}
520+
if (this.field.type == 'files') {
521+
this.field.max_file_size = Math.min((this.field.max_file_size ?? this.mbLimit), this.mbLimit)
522+
}
507523
},
508524
509525
methods: {

0 commit comments

Comments
 (0)