From 84885837ab23341c0e69dd7039050bb9b11e3d51 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 26 Jul 2023 14:44:39 +1000 Subject: [PATCH] Fixes for string delimiters --- app/Http/Controllers/ImportController.php | 15 +++++++++++---- app/Http/Requests/Import/PreImportRequest.php | 3 ++- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/app/Http/Controllers/ImportController.php b/app/Http/Controllers/ImportController.php index c8aa9c70db..ee953dcf35 100644 --- a/app/Http/Controllers/ImportController.php +++ b/app/Http/Controllers/ImportController.php @@ -170,20 +170,27 @@ class ImportController extends Controller return $data; } - public function detectDelimiter($csvfile) + /** + * Returns the best delimiter + * + * @param string $csvfile + * @return string + */ + public function detectDelimiter($csvfile): string { $delimiters = [',', '.', ';']; $bestDelimiter = ' '; $count = 0; foreach ($delimiters as $delimiter) { - + if (substr_count(strstr($csvfile, "\n", true), $delimiter) >= $count) { - $count = substr_count($csvfile, $delimiter); - $bestDelimiter = $delimiter; + $count = substr_count(strstr($csvfile, "\n", true), $delimiter); + $bestDelimiter = $delimiter; } } + return $bestDelimiter; } } diff --git a/app/Http/Requests/Import/PreImportRequest.php b/app/Http/Requests/Import/PreImportRequest.php index 7f23b64fcf..9e959ac882 100644 --- a/app/Http/Requests/Import/PreImportRequest.php +++ b/app/Http/Requests/Import/PreImportRequest.php @@ -28,9 +28,10 @@ class PreImportRequest extends Request public function rules() { return [ - 'files.*' => 'file|mimes:csv,txt', + 'files.*' => 'file|mimetypes:text/csv,text/plain,application/octet-stream', 'files' => 'required|array|min:1|max:6', 'import_type' => 'required', ]; } + }