Fa005 submissions file link in notifications (#298)

* fix: unsigned url in form email

* fix: signed file link in email

* fix: use limit function for file  label name

* feat: use signed url  for files in email and csv export

---------

Co-authored-by: Julien Nahum <julien@nahum.net>
This commit is contained in:
Favour Olayinka 2024-01-31 12:21:28 +01:00 committed by GitHub
parent 428ab4f6cf
commit 38cb133e52
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 47 additions and 11 deletions

View File

@ -37,7 +37,8 @@ class FormSubmissionController extends Controller
$formatter = (new FormSubmissionFormatter($form, $row['data'])) $formatter = (new FormSubmissionFormatter($form, $row['data']))
->outputStringsOnly() ->outputStringsOnly()
->setEmptyForNoValue() ->setEmptyForNoValue()
->showRemovedFields(); ->showRemovedFields()
->useSignedUrlForFiles();
$tmp = $formatter->getCleanKeyValue(); $tmp = $formatter->getCleanKeyValue();
$tmp['Create Date'] = date("Y-m-d H:i", strtotime($row['created_at'])); $tmp['Create Date'] = date("Y-m-d H:i", strtotime($row['created_at']));
$allRows[] = $tmp; $allRows[] = $tmp;

View File

@ -35,7 +35,8 @@ class SubmissionConfirmationMail extends OpenFormMail implements ShouldQueue
$formatter = (new FormSubmissionFormatter($form, $this->event->data)) $formatter = (new FormSubmissionFormatter($form, $this->event->data))
->createLinks() ->createLinks()
->outputStringsOnly(); ->outputStringsOnly()
->useSignedUrlForFiles();
return $this return $this
->replyTo($this->getReplyToEmail($form->creator->email)) ->replyTo($this->getReplyToEmail($form->creator->email))

View File

@ -49,7 +49,8 @@ class FormSubmissionNotification extends Notification implements ShouldQueue
$formatter = (new FormSubmissionFormatter($this->event->form, $this->event->data)) $formatter = (new FormSubmissionFormatter($this->event->form, $this->event->data))
->showHiddenFields() ->showHiddenFields()
->createLinks() ->createLinks()
->outputStringsOnly(); ->outputStringsOnly()
->useSignedUrlForFiles();
return (new MailMessage) return (new MailMessage)
->replyTo($this->getReplyToEmail($notifiable->routes['mail'])) ->replyTo($this->getReplyToEmail($notifiable->routes['mail']))

View File

@ -29,6 +29,8 @@ class FormSubmissionFormatter
private $showRemovedFields = false; private $showRemovedFields = false;
private $useSignedUrlForFiles = false;
/** /**
* Logic resolver needs an array id => value, so we create it here * Logic resolver needs an array id => value, so we create it here
*/ */
@ -69,6 +71,12 @@ class FormSubmissionFormatter
return $this; return $this;
} }
public function useSignedUrlForFiles()
{
$this->useSignedUrlForFiles = true;
return $this;
}
/** /**
* Return a nice "FieldName": "Field Response" array * Return a nice "FieldName": "Field Response" array
* - If createLink enabled, returns html link for emails and links * - If createLink enabled, returns html link for emails and links
@ -117,14 +125,14 @@ class FormSubmissionFormatter
$formId = $this->form->id; $formId = $this->form->id;
$returnArray[$field['name']] = implode(', ', $returnArray[$field['name']] = implode(', ',
collect($data[$field['id']])->map(function ($file) use ($formId) { collect($data[$field['id']])->map(function ($file) use ($formId) {
return route('open.forms.submissions.file', [$formId, $file]); return $this->getFileUrl($formId, $file);
})->toArray() })->toArray()
); );
} else { } else {
$formId = $this->form->id; $formId = $this->form->id;
$returnArray[$field['name']] = collect($data[$field['id']])->map(function ($file) use ($formId) { $returnArray[$field['name']] = collect($data[$field['id']])->map(function ($file) use ($formId) {
return [ return [
'file_url' => route('open.forms.submissions.file', [$formId, $file]), 'file_url' => $this->getFileUrl($formId, $file),
'file_name' => $file, 'file_name' => $file,
]; ];
}); });
@ -185,14 +193,22 @@ class FormSubmissionFormatter
$formId = $this->form->id; $formId = $this->form->id;
$field['value'] = implode(', ', $field['value'] = implode(', ',
collect($data[$field['id']])->map(function ($file) use ($formId) { collect($data[$field['id']])->map(function ($file) use ($formId) {
return route('open.forms.submissions.file', [$formId, $file]); return $this->getFileUrl($formId, $file);
})->toArray() })->toArray()
); );
$field['email_data'] = collect($data[$field['id']])->map(function ($file) use ($formId) {
$splitText = explode('.', $file);
return [
"unsigned_url" => route('open.forms.submissions.file', [$formId, $file]),
"signed_url" => $this->getFileUrl($formId, $file),
"label" => \Str::limit($file, 20, '[...].'.end($splitText))
];
})->toArray();
} else { } else {
$formId = $this->form->id; $formId = $this->form->id;
$field['value'] = collect($data[$field['id']])->map(function ($file) use ($formId) { $field['value'] = collect($data[$field['id']])->map(function ($file) use ($formId) {
return [ return [
'file_url' => route('open.forms.submissions.file', [$formId, $file]), 'file_url' => $this->getFileUrl($formId, $file),
'file_name' => $file, 'file_name' => $file,
]; ];
}); });
@ -222,4 +238,11 @@ class FormSubmissionFormatter
} }
} }
private function getFileUrl($formId, $file)
{
return $this->useSignedUrlForFiles ? \URL::signedRoute(
'open.forms.submissions.file',
[$formId, $file]) : route('open.forms.submissions.file', [$formId, $file]);
}
} }

View File

@ -17,9 +17,14 @@ As a reminder, here are your answers:
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
**{{$field['name']}}** **{{$field['name']}}**
@if($field['type'] == 'files')
<br/>
@foreach($field['email_data'] as $link)
<a href="{{$link['signed_url']}}">{{$link['label']}}</a> <br/>
@endforeach
@else
{!! is_array($field['value'])?implode(',',$field['value']):$field['value']!!} {!! is_array($field['value'])?implode(',',$field['value']):$field['value']!!}
@endif
@endif @endif
@endforeach @endforeach
@endif @endif

View File

@ -10,9 +10,14 @@ Your form "{{$form->title}}" has a new submission.
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
**{{$field['name']}}** **{{$field['name']}}**
@if($field['type'] == 'files')
<br/>
@foreach($field['email_data'] as $link)
<a href="{{$link['signed_url']}}">{{$link['label']}}</a> <br/>
@endforeach
@else
{!! is_array($field['value'])?implode(',',$field['value']):$field['value']!!} {!! is_array($field['value'])?implode(',',$field['value']):$field['value']!!}
@endif
@endif @endif
@endforeach @endforeach