Editable submission File input support (#120)
This commit is contained in:
parent
3f2fe352e8
commit
e165242e57
|
@ -168,7 +168,7 @@ class AnswerFormRequest extends FormRequest
|
|||
return ['boolean'];
|
||||
case 'url':
|
||||
if (isset($property['file_upload']) && $property['file_upload']) {
|
||||
$this->requestRules[$property['id'].'.*'] = [new StorageFile($this->maxFileSize)];
|
||||
$this->requestRules[$property['id'].'.*'] = [new StorageFile($this->maxFileSize, [], $this->form)];
|
||||
return ['array'];
|
||||
}
|
||||
return ['url'];
|
||||
|
@ -177,7 +177,7 @@ class AnswerFormRequest extends FormRequest
|
|||
if($this->form->is_pro && !empty($property['allowed_file_types'])){
|
||||
$allowedFileTypes = explode(",", $property['allowed_file_types']);
|
||||
}
|
||||
$this->requestRules[$property['id'].'.*'] = [new StorageFile($this->maxFileSize, $allowedFileTypes)];
|
||||
$this->requestRules[$property['id'].'.*'] = [new StorageFile($this->maxFileSize, $allowedFileTypes, $this->form)];
|
||||
return ['array'];
|
||||
case 'email':
|
||||
return ['email:filter'];
|
||||
|
|
|
@ -136,6 +136,13 @@ class StoreFormSubmissionJob implements ShouldQueue
|
|||
return $finalData;
|
||||
}
|
||||
|
||||
// This is use when updating a record, and file uploads aren't changed.
|
||||
private function isSkipForUpload($value)
|
||||
{
|
||||
$newPath = Str::of(PublicFormController::FILE_UPLOAD_PATH)->replace('?', $this->form->id);
|
||||
return Storage::disk('s3')->exists($newPath.'/'.$value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Custom Back-end Value formatting. Use case:
|
||||
* - File uploads (move file from tmp storage to persistent)
|
||||
|
@ -150,6 +157,10 @@ class StoreFormSubmissionJob implements ShouldQueue
|
|||
return null;
|
||||
}
|
||||
|
||||
if($this->isSkipForUpload($value)) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
$fileNameParser = StorageFileNameParser::parse($value);
|
||||
|
||||
// Make sure we retrieve the file in tmp storage, move it to persistent
|
||||
|
|
|
@ -7,6 +7,7 @@ use App\Service\Storage\StorageFileNameParser;
|
|||
use Illuminate\Contracts\Validation\Rule;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Str;
|
||||
use App\Models\Forms\Form;
|
||||
|
||||
class StorageFile implements Rule
|
||||
{
|
||||
|
@ -21,7 +22,7 @@ class StorageFile implements Rule
|
|||
* @param int $maxSize
|
||||
* @param string[] $fileTypes
|
||||
*/
|
||||
public function __construct(int $maxSize, array $fileTypes = [])
|
||||
public function __construct(int $maxSize, array $fileTypes = [], public ?Form $form = null)
|
||||
{
|
||||
$this->maxSize = $maxSize;
|
||||
|
||||
|
@ -39,6 +40,14 @@ class StorageFile implements Rule
|
|||
*/
|
||||
public function passes($attribute, $value): bool
|
||||
{
|
||||
// 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);
|
||||
if(Storage::disk('s3')->exists($newPath.'/'.$value)){
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
$fileNameParser = StorageFileNameParser::parse($value);
|
||||
if (!$uuid = $fileNameParser->uuid) {
|
||||
return false;
|
||||
|
|
|
@ -208,7 +208,19 @@ export default {
|
|||
}
|
||||
},
|
||||
|
||||
created () {
|
||||
async created () {
|
||||
if(this.compVal && this.compVal.length > 0) {
|
||||
let tmpFiles = []
|
||||
for (let i = 0; i < this.compVal.length; i++) {
|
||||
await this.getFileFromUrl(this.compVal[i]).then((fileObj) => {
|
||||
tmpFiles.push({
|
||||
file: fileObj,
|
||||
url: this.compVal[i]
|
||||
})
|
||||
})
|
||||
}
|
||||
this.files = tmpFiles
|
||||
}
|
||||
},
|
||||
|
||||
methods: {
|
||||
|
@ -262,6 +274,14 @@ export default {
|
|||
this.showUploadModal = false
|
||||
this.loading = false
|
||||
})
|
||||
},
|
||||
async getFileFromUrl(url, defaultType='image/jpeg'){
|
||||
const response = await fetch(url)
|
||||
const data = await response.blob()
|
||||
const name = url.replace(/^.*(\\|\/|\:)/, '')
|
||||
return new File([data], name, {
|
||||
type: data.type || defaultType,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue