From 4614dc0f1868ddd554d218cbcca78afa5e61fcca Mon Sep 17 00:00:00 2001 From: formsdev <136701234+formsdev@users.noreply.github.com> Date: Fri, 20 Oct 2023 14:30:35 +0530 Subject: [PATCH] Pre-fill support for file input (#222) Co-authored-by: Julien Nahum --- app/Http/Requests/UploadAssetRequest.php | 21 +++++++----- app/Http/Resources/FormSubmissionResource.php | 4 ++- app/Jobs/Form/StoreFormSubmissionJob.php | 9 +++++ app/Rules/StorageFile.php | 5 +++ resources/js/components/forms/FileInput.vue | 34 +++++++++++++++---- .../forms/fields/components/FieldOptions.vue | 7 +++- 6 files changed, 63 insertions(+), 17 deletions(-) diff --git a/app/Http/Requests/UploadAssetRequest.php b/app/Http/Requests/UploadAssetRequest.php index 05aaf72..51413a6 100644 --- a/app/Http/Requests/UploadAssetRequest.php +++ b/app/Http/Requests/UploadAssetRequest.php @@ -16,15 +16,20 @@ class UploadAssetRequest extends FormRequest */ public function rules() { + $fileTypes = [ + 'png', + 'jpeg', + 'jpg', + 'bmp', + 'gif', + 'svg' + ]; + if ($this->offsetExists('type') && $this->get('type') === 'files') { + $fileTypes = []; + } + return [ - 'url' => ['required',new StorageFile(self::FORM_ASSET_MAX_SIZE, [ - 'png', - 'jpeg', - 'jpg', - 'bmp', - 'gif', - 'svg' - ])], + 'url' => ['required', new StorageFile(self::FORM_ASSET_MAX_SIZE, $fileTypes)], ]; } } diff --git a/app/Http/Resources/FormSubmissionResource.php b/app/Http/Resources/FormSubmissionResource.php index e500dd1..6018e9e 100644 --- a/app/Http/Resources/FormSubmissionResource.php +++ b/app/Http/Resources/FormSubmissionResource.php @@ -46,7 +46,9 @@ class FormSubmissionResource extends JsonResource }); foreach ($fileFields as $field) { if (isset($data[$field['id']]) && !empty($data[$field['id']])) { - $data[$field['id']] = collect($data[$field['id']])->map(function ($file) { + $data[$field['id']] = collect($data[$field['id']])->filter(function ($file) { + return $file !== null && $file; + })->map(function ($file) { return [ 'file_url' => route('open.forms.submissions.file', [$this->form_id, $file]), 'file_name' => $file, diff --git a/app/Jobs/Form/StoreFormSubmissionJob.php b/app/Jobs/Form/StoreFormSubmissionJob.php index 54f2933..0c4e526 100644 --- a/app/Jobs/Form/StoreFormSubmissionJob.php +++ b/app/Jobs/Form/StoreFormSubmissionJob.php @@ -4,6 +4,7 @@ namespace App\Jobs\Form; use App\Events\Forms\FormSubmitted; use App\Http\Controllers\Forms\PublicFormController; +use App\Http\Controllers\Forms\FormController; use App\Http\Requests\AnswerFormRequest; use App\Models\Forms\Form; use App\Models\Forms\FormSubmission; @@ -162,6 +163,14 @@ class StoreFormSubmissionJob implements ShouldQueue return null; } + if(filter_var($value, FILTER_VALIDATE_URL) !== FALSE && str_contains($value, parse_url(config('app.url'))['host'])) { // In case of prefill we have full url so convert to s3 + $fileName = basename($value); + $path = FormController::ASSETS_UPLOAD_PATH . '/' . $fileName; + $newPath = Str::of(PublicFormController::FILE_UPLOAD_PATH)->replace('?', $this->form->id); + Storage::move($path, $newPath.'/'.$fileName); + return $fileName; + } + if($this->isSkipForUpload($value)) { return $value; } diff --git a/app/Rules/StorageFile.php b/app/Rules/StorageFile.php index f2f0a30..b7df2eb 100644 --- a/app/Rules/StorageFile.php +++ b/app/Rules/StorageFile.php @@ -40,6 +40,11 @@ class StorageFile implements Rule */ public function passes($attribute, $value): bool { + // If full path then no need to validate + if (filter_var($value, FILTER_VALIDATE_URL) !== FALSE) { + return true; + } + // This is use when updating a record, and file uploads aren't changed. if($this->form){ $newPath = Str::of(PublicFormController::FILE_UPLOAD_PATH)->replace('?', $this->form->id); diff --git a/resources/js/components/forms/FileInput.vue b/resources/js/components/forms/FileInput.vue index b0ac5b7..d7ffbd2 100644 --- a/resources/js/components/forms/FileInput.vue +++ b/resources/js/components/forms/FileInput.vue @@ -156,6 +156,7 @@