mirror of
https://github.com/snipe/snipe-it.git
synced 2025-01-13 06:47:46 -08:00
Merge pull request #12489 from marcusmoore/fix/encoding-errors-in-importer
Improve error messaging around invalid characters in import
This commit is contained in:
commit
35ab4a4cce
|
@ -10,6 +10,7 @@ use App\Models\Asset;
|
|||
use App\Models\Company;
|
||||
use App\Models\Import;
|
||||
use Artisan;
|
||||
use Illuminate\Database\Eloquent\JsonEncodingException;
|
||||
use Illuminate\Support\Facades\Request;
|
||||
use Illuminate\Support\Facades\Session;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
@ -35,7 +36,7 @@ class ImportController extends Controller
|
|||
* Process and store a CSV upload file.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return \Illuminate\Http\Response
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
*/
|
||||
public function store()
|
||||
{
|
||||
|
@ -56,7 +57,7 @@ class ImportController extends Controller
|
|||
'text/tsv', ])) {
|
||||
$results['error'] = 'File type must be CSV. Uploaded file is '.$file->getMimeType();
|
||||
|
||||
return response()->json(Helper::formatStandardApiResponse('error', null, $results['error']), 500);
|
||||
return response()->json(Helper::formatStandardApiResponse('error', null, $results['error']), 422);
|
||||
}
|
||||
|
||||
//TODO: is there a lighter way to do this?
|
||||
|
@ -64,7 +65,19 @@ class ImportController extends Controller
|
|||
ini_set('auto_detect_line_endings', '1');
|
||||
}
|
||||
$reader = Reader::createFromFileObject($file->openFile('r')); //file pointer leak?
|
||||
$import->header_row = $reader->fetchOne(0);
|
||||
|
||||
try {
|
||||
$import->header_row = $reader->fetchOne(0);
|
||||
} catch (JsonEncodingException $e) {
|
||||
return response()->json(
|
||||
Helper::formatStandardApiResponse(
|
||||
'error',
|
||||
null,
|
||||
'One or more attributes in the header row contain malformed UTF-8 characters'
|
||||
),
|
||||
422
|
||||
);
|
||||
}
|
||||
|
||||
//duplicate headers check
|
||||
$duplicate_headers = [];
|
||||
|
@ -82,11 +95,22 @@ class ImportController extends Controller
|
|||
}
|
||||
}
|
||||
if (count($duplicate_headers) > 0) {
|
||||
return response()->json(Helper::formatStandardApiResponse('error', null, implode('; ', $duplicate_headers)), 500); //should this be '4xx'?
|
||||
return response()->json(Helper::formatStandardApiResponse('error', null, implode('; ', $duplicate_headers)),422);
|
||||
}
|
||||
|
||||
// Grab the first row to display via ajax as the user picks fields
|
||||
$import->first_row = $reader->fetchOne(1);
|
||||
try {
|
||||
// Grab the first row to display via ajax as the user picks fields
|
||||
$import->first_row = $reader->fetchOne(1);
|
||||
} catch (JsonEncodingException $e) {
|
||||
return response()->json(
|
||||
Helper::formatStandardApiResponse(
|
||||
'error',
|
||||
null,
|
||||
'One or more attributes in row 2 contain malformed UTF-8 characters'
|
||||
),
|
||||
422
|
||||
);
|
||||
}
|
||||
|
||||
$date = date('Y-m-d-his');
|
||||
$fixed_filename = str_slug($file->getClientOriginalName());
|
||||
|
@ -108,12 +132,12 @@ class ImportController extends Controller
|
|||
}
|
||||
$results = (new ImportsTransformer)->transformImports($results);
|
||||
|
||||
return [
|
||||
return response()->json([
|
||||
'files' => $results,
|
||||
];
|
||||
]);
|
||||
}
|
||||
|
||||
return response()->json(Helper::formatStandardApiResponse('error', null, trans('general.feature_disabled')), 500);
|
||||
return response()->json(Helper::formatStandardApiResponse('error', null, trans('general.feature_disabled')), 422);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in a new issue