Skip to content

Commit

Permalink
Merge pull request simonhamp#21 from septoctobre/feat/xslx-import
Browse files Browse the repository at this point in the history
Loosen the restriction on file type
  • Loading branch information
simonhamp authored Feb 12, 2020
2 parents ade206f + a1b98e5 commit e3daae6
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
8 changes: 4 additions & 4 deletions src/Http/Controllers/ImportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function __construct()
public function preview(NovaRequest $request, $file)
{
$import = $this->importer
->toCollection($this->getFilePath($file), null, 'Csv')
->toCollection($this->getFilePath($file), null)
->first();

$headings = $import->first()->keys();
Expand All @@ -37,10 +37,10 @@ public function preview(NovaRequest $request, $file)
$sample = $import->take(10)->all();

$resources = collect(Nova::$resources);

$resources = $resources->filter(function ($resource) {
$static_vars = (new \ReflectionClass((string) $resource))->getStaticProperties();

if(!isset($static_vars['canImportResource'])) {
return true;
}
Expand Down Expand Up @@ -87,7 +87,7 @@ public function import(NovaRequest $request, $file)
->setAttributeMap($attribute_map)
->setRules($rules)
->setModelClass($model_class)
->import($this->getFilePath($file), null, 'Csv');
->import($this->getFilePath($file), null);

if (! $this->importer->failures()->isEmpty() || ! $this->importer->errors()->isEmpty()) {
return response()->json(['result' => 'failure', 'errors' => $this->importer->errors(), 'failures' => $this->importer->failures()]);
Expand Down
5 changes: 3 additions & 2 deletions src/Http/Controllers/UploadController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@ public function handle(NovaRequest $request)
])->validate();

$file = $request->file('file');
$extension = $file->getClientOriginalExtension();

try {
(new Importer)->toCollection($file, null, 'Csv');
(new Importer)->toCollection($file, null);
} catch (\Exception $e) {
return response()->json(['result' => 'error', 'message' => 'Sorry, we could not import that file'], 422);
}

// Store the file temporarily
$hash = File::hash($file->getRealPath());
$hash = File::hash($file->getRealPath()).".".$extension;

$file->move(storage_path('nova/laravel-nova-import-csv/tmp'), $hash);

Expand Down

0 comments on commit e3daae6

Please sign in to comment.