Skip to content

Commit

Permalink
OpenSID#9 - Sesuaikan impor penduduk kirim foto
Browse files Browse the repository at this point in the history
  • Loading branch information
agungsugiarto committed Dec 16, 2020
1 parent 7407f2b commit e46a1be
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions app/Http/Controllers/Data/PendudukController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Yajra\DataTables\DataTables;
use ZipArchive;

use function back;
use function compact;
Expand Down Expand Up @@ -241,16 +244,36 @@ public function import()
public function importExcel(Request $request)
{
$this->validate($request, [
'file' => 'file|mimes:xls,xlsx,csv|max:5120',
'file' => 'file|mimes:zip|max:5120',
]);

try {
// Upload file zip temporary.
$file = $request->file('file');
$file->storeAs('temp', $name = $file->getClientOriginalName());

// Temporary path file
$path = storage_path("app/temp/{$name}");
$extract = storage_path('app/public/penduduk/foto/');

// Ekstrak file
$zip = new ZipArchive;
$zip->open($path);
$zip->extractTo($extract);
$zip->close();

// Proses impor excell
(new ImporPenduduk($request))
->import($request->file('file'));
->import($extract . $excellName = Str::replaceLast('zip', 'xlsx', $name));
} catch (Exception $e) {
return back()->with('error', 'Import data gagal. ' . $e->getMessage());
}

// Hapus folder temp ketika sudah selesai
Storage::deleteDirectory('temp');
// Hapus file excell temp ketika sudah selesai
Storage::disk('public')->delete('penduduk/foto/' . $excellName);

return back()->with('success', 'Import data sukses.');
}
}

0 comments on commit e46a1be

Please sign in to comment.