1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 05:02:36 +01:00

Fixes for csv file encodings

This commit is contained in:
David Bomba 2023-07-22 16:00:57 +10:00
parent ec1b942145
commit 8a4da65b6b
2 changed files with 17 additions and 5 deletions

View File

@ -81,7 +81,8 @@ class ImportController extends Controller
/** @var UploadedFile $file */
foreach ($request->files->get('files') as $entityType => $file) {
$contents = file_get_contents($file->getPathname());
// $contents = mb_convert_encoding($contents, 'UTF-16LE', 'UTF-8');
$contents = $this->convertEncoding($contents);
// Store the csv in cache with an expiry of 10 minutes
Cache::put($hash.'-'.$entityType, base64_encode($contents), 600);
@ -97,11 +98,21 @@ class ImportController extends Controller
];
}
$data = mb_convert_encoding($data, 'UTF-8', 'UTF-8');
return response()->json($data);
}
private function convertEncoding($data)
{
$enc = mb_detect_encoding($data, mb_list_encodings(), true);
if($enc !== false) {
$data = mb_convert_encoding($data, "UTF-8", $enc);
}
return $data;
}
public function import(ImportRequest $request)
{
$data = $request->all();

View File

@ -102,6 +102,8 @@ class BaseImport
}
$csv = base64_decode($base64_encoded_csv);
$csv = mb_convert_encoding($csv, 'UTF-8', 'UTF-8');
nlog($csv);
$csv = Reader::createFromString($csv);
$csvdelimiter = self::detectDelimiter($csv);
@ -765,8 +767,7 @@ class BaseImport
{
$keys = array_shift($data);
ksort($keys);
// nlog($data);
// nlog($keys);
return array_map(function ($values) use ($keys) {
return array_combine($keys, $values);
}, $data);