file upload max size feature (#328)

* file upload max size feature

* Change label

---------

Co-authored-by: Julien Nahum <julien@nahum.net>
This commit is contained in:
Favour Olayinka 2024-03-12 18:27:09 +01:00 committed by GitHub
parent bf95096bfe
commit 7b89ebdcb9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 27 additions and 2 deletions

View File

@ -27,6 +27,12 @@ class AnswerFormRequest extends FormRequest
$this->maxFileSize = $this->form->workspace->max_file_size; $this->maxFileSize = $this->form->workspace->max_file_size;
} }
private function getFieldMaxFileSize($fieldProps)
{
return array_key_exists('max_file_size', $fieldProps) ?
min($fieldProps['max_file_size'] * 1000000, $this->maxFileSize) : $this->maxFileSize;
}
/** /**
* Validate form before use it * Validate form before use it
* *
@ -180,7 +186,7 @@ class AnswerFormRequest extends FormRequest
if (! empty($property['allowed_file_types'])) { if (! empty($property['allowed_file_types'])) {
$allowedFileTypes = explode(',', $property['allowed_file_types']); $allowedFileTypes = explode(',', $property['allowed_file_types']);
} }
$this->requestRules[$property['id'].'.*'] = [new StorageFile($this->maxFileSize, $allowedFileTypes, $this->form)]; $this->requestRules[$property['id'] . '.*'] = [new StorageFile($this->getFieldMaxFileSize($property), $allowedFileTypes, $this->form)];
return ['array']; return ['array'];
case 'email': case 'email':

View File

@ -118,6 +118,9 @@ abstract class UserFormRequest extends \Illuminate\Foundation\Http\FormRequest
'properties.*.generates_uuid' => 'boolean|nullable', 'properties.*.generates_uuid' => 'boolean|nullable',
'properties.*.generates_auto_increment_id' => 'boolean|nullable', 'properties.*.generates_auto_increment_id' => 'boolean|nullable',
// For file (min and max)
'properties.*.max_file_size' => 'min:1|numeric',
// Security & Privacy // Security & Privacy
'can_be_indexed' => 'boolean', 'can_be_indexed' => 'boolean',
'password' => 'sometimes|nullable', 'password' => 'sometimes|nullable',

View File

@ -303,7 +303,7 @@ export default {
} }
} else if (field.type === 'files' || (field.type === 'url' && field.file_upload)) { } else if (field.type === 'files' || (field.type === 'url' && field.file_upload)) {
inputProperties.multiple = (field.multiple !== undefined && field.multiple) inputProperties.multiple = (field.multiple !== undefined && field.multiple)
inputProperties.mbLimit = this.form?.max_file_size ?? this.currentWorkspace?.max_file_size inputProperties.mbLimit = Math.min(Math.max(field.max_file_size, 1), this.form?.max_file_size ?? this.currentWorkspace?.max_file_size)
inputProperties.accept = (this.form.is_pro && field.allowed_file_types) ? field.allowed_file_types : '' inputProperties.accept = (this.form.is_pro && field.allowed_file_types) ? field.allowed_file_types : ''
} else if (field.type === 'number' && field.is_rating) { } else if (field.type === 'number' && field.is_rating) {
inputProperties.numberOfStars = parseInt(field.rating_max_value) inputProperties.numberOfStars = parseInt(field.rating_max_value)

View File

@ -60,6 +60,13 @@
label="Allowed file types" placeholder="jpg,jpeg,png,gif" label="Allowed file types" placeholder="jpg,jpeg,png,gif"
help="Comma separated values, leave blank to allow all file types" help="Comma separated values, leave blank to allow all file types"
/> />
<text-input name="max_file_size" class="mt-3" :form="field" native-type="number"
:min="1"
:max="mbLimit"
label="Maximum file size (in MB)" :placeholder="`1MB - ${mbLimit}MB`"
help="Set the maximum file size that can be uploaded"
/>
</div> </div>
<!-- Number Options --> <!-- Number Options -->
@ -428,6 +435,9 @@ export default {
required: false required: false
} }
}, },
setup() {
return { currentWorkspace: computed(() => useWorkspacesStore().getCurrent), }
},
data () { data () {
return { return {
typesWithoutPlaceholder: ['date', 'checkbox', 'files'], typesWithoutPlaceholder: ['date', 'checkbox', 'files'],
@ -442,6 +452,9 @@ export default {
hasPlaceholder () { hasPlaceholder () {
return !this.typesWithoutPlaceholder.includes(this.field.type) return !this.typesWithoutPlaceholder.includes(this.field.type)
}, },
mbLimit() {
return this.form?.max_file_size ?? this.currentWorkspace?.max_file_size
},
prefillSelectsOptions () { prefillSelectsOptions () {
if (!['select', 'multi_select'].includes(this.field.type)) return {} if (!['select', 'multi_select'].includes(this.field.type)) return {}
@ -504,6 +517,9 @@ export default {
if (['text', 'number', 'url', 'email'].includes(this.field?.type) && !this.field?.max_char_limit) { if (['text', 'number', 'url', 'email'].includes(this.field?.type) && !this.field?.max_char_limit) {
this.field.max_char_limit = 2000 this.field.max_char_limit = 2000
} }
if (this.field.type == 'files') {
this.field.max_file_size = Math.min((this.field.max_file_size ?? this.mbLimit), this.mbLimit)
}
}, },
methods: { methods: {