Skip to content

Commit

Permalink
Pre-fill support for file input
Browse files Browse the repository at this point in the history
  • Loading branch information
formsdev committed Oct 16, 2023
1 parent 25506f5 commit 8276763
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 17 deletions.
21 changes: 13 additions & 8 deletions app/Http/Requests/UploadAssetRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)],
];
}
}
4 changes: 3 additions & 1 deletion app/Http/Resources/FormSubmissionResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ private function generateFileLinks()
});
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,
Expand Down
9 changes: 9 additions & 0 deletions app/Jobs/Form/StoreFormSubmissionJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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;
Expand Down Expand Up @@ -162,6 +163,14 @@ private function storeFile(?string $value)
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;
}
Expand Down
5 changes: 5 additions & 0 deletions app/Rules/StorageFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public function __construct(int $maxSize, array $fileTypes = [], public ?Form $f
*/
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);
Expand Down
34 changes: 27 additions & 7 deletions resources/js/components/forms/FileInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@

<script>
import Modal from '../Modal.vue'
import axios from 'axios'
import inputMixin from '~/mixins/forms/input.js'
export default {
Expand All @@ -166,7 +167,8 @@ export default {
props: {
multiple: { type: Boolean, default: true },
mbLimit: { type: Number, default: 5 },
accept: { type: String, default: '' }
accept: { type: String, default: '' },
moveToFormAssets: { type: Boolean, default: false }
},
data: () => ({
Expand Down Expand Up @@ -263,12 +265,30 @@ export default {
if (!this.multiple) {
this.files = []
}
this.files.push({
file: file,
url: file.name.split('.').slice(0, -1).join('.') + '_' + response.uuid + '.' + response.extension
})
this.showUploadModal = false
this.loading = false
if (this.moveToFormAssets) {
// Move file to permanent storage for form assets
axios.post('/api/open/forms/assets/upload', {
type: 'files',
url: file.name.split('.').slice(0, -1).join('.') + '_' + response.uuid + '.' + response.extension
}).then(moveFileResponse => {
this.files.push({
file: file,
url: moveFileResponse.data.url
})
this.showUploadModal = false
this.loading = false
}).catch((error) => {
this.showUploadModal = false
this.loading = false
})
} else {
this.files.push({
file: file,
url: file.name.split('.').slice(0, -1).join('.') + '_' + response.uuid + '.' + response.extension
})
this.showUploadModal = false
this.loading = false
}
}).catch((error) => {
this.clearAll()
this.showUploadModal = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,12 @@
:form="field"
label="Pre-filled value"
/>
<text-input v-else-if="field.type!=='files'" name="prefill" class="mt-3"
<file-input v-else-if="field.type==='files'" name="prefill" class="mt-4"
:form="field"
label="Pre-filled file"
:multiple="field.multiple===true" :moveToFormAssets="true"
/>
<text-input v-else-if="!['files', 'signature'].includes(field.type)" name="prefill" class="mt-3"
:form="field"
label="Pre-filled value"
:disabled="field.type==='date' && field.prefill_today===true"
Expand Down

0 comments on commit 8276763

Please sign in to comment.