bugs fix ambil path di list provider
This commit is contained in:
@@ -195,7 +195,10 @@ class ClaimController extends Controller
|
||||
$limit = $request->has('per_page') ? $request->input('per_page') : 50;
|
||||
$results = DB::table('request_logs')
|
||||
->leftJoin('members', 'request_logs.member_id', '=', 'members.id')
|
||||
->join('files', 'request_logs.id', '=', 'files.fileable_id')
|
||||
->join('files', function ($join) {
|
||||
$join->on('request_logs.id', '=', 'files.fileable_id')
|
||||
->whereNull('files.deleted_at');
|
||||
})
|
||||
// ->leftJoin('member_plans', 'member_plans.member_id', '=', 'members.id')
|
||||
->when($request->input('search'), function ($query, $search) {
|
||||
$query->where(function ($query) use ($search) {
|
||||
@@ -239,46 +242,82 @@ class ClaimController extends Controller
|
||||
(SELECT organizations.name FROM organizations WHERE organizations.id = request_logs.organization_id LIMIT 1) AS provider
|
||||
'),
|
||||
'request_logs.status_final_log as status',
|
||||
DB::raw("CONCAT('" . env('APP_URL') . "/storage/', path) as path")
|
||||
'files.path',
|
||||
'files.source'
|
||||
)
|
||||
->paginate($limit);
|
||||
|
||||
$results->getCollection()->transform(function ($row) {
|
||||
if (!$row->path) {
|
||||
$row->path = null;
|
||||
return $row;
|
||||
}
|
||||
|
||||
if ($row->source === 's3') {
|
||||
try {
|
||||
$row->path = Storage::disk('s3')->temporaryUrl(
|
||||
$row->path,
|
||||
now()->addMinutes(60)
|
||||
);
|
||||
} catch (\Exception $e) {
|
||||
$row->path = Storage::disk('s3')->url($row->path);
|
||||
}
|
||||
} else {
|
||||
$row->path = Storage::disk('public')->url($row->path);
|
||||
}
|
||||
|
||||
return $row;
|
||||
});
|
||||
|
||||
return response()->json(Helper::paginateResources($results));
|
||||
}
|
||||
|
||||
public function downloadZip(Request $request)
|
||||
{
|
||||
$selectedRows = $request->selectedRows; // asumsi $selectedRows berisi array ID file yang dipilih
|
||||
$files = [];
|
||||
$selectedRows = $request->selectedRows; // array ID file
|
||||
$files = [];
|
||||
|
||||
// Ambil path file dari database atau sumber lain sesuai dengan $selectedRows
|
||||
$data = DB::table('files')
|
||||
->whereIn('id', $selectedRows)
|
||||
->select('path')
|
||||
->get();
|
||||
// Ambil path dan source
|
||||
$data = DB::table('files')
|
||||
->whereIn('id', $selectedRows)
|
||||
->select('path', 'source', 'original_name')
|
||||
->get();
|
||||
|
||||
foreach ($data as $value) {
|
||||
$files[] = storage_path('app/public/' . $value->path);
|
||||
}
|
||||
$zipFileName = 'downloaded_files_' . time() . '.zip';
|
||||
$zipPath = storage_path('app/public/' . $zipFileName);
|
||||
|
||||
$zipFileName = 'downloaded_files.zip';
|
||||
$zip = new ZipArchive();
|
||||
$zip = new ZipArchive();
|
||||
if ($zip->open($zipPath, ZipArchive::CREATE | ZipArchive::OVERWRITE)) {
|
||||
foreach ($data as $value) {
|
||||
$localPath = null;
|
||||
|
||||
if ($zip->open(storage_path('app/public/' . $zipFileName), ZipArchive::CREATE | ZipArchive::OVERWRITE)) {
|
||||
foreach ($files as $file) {
|
||||
$zip->addFile($file, basename($file));
|
||||
if ($value->source === 's3') {
|
||||
// download file dari S3 ke temporary file
|
||||
$tmpFile = tempnam(sys_get_temp_dir(), 's3file_');
|
||||
$stream = Storage::disk('s3')->get($value->path);
|
||||
file_put_contents($tmpFile, $stream);
|
||||
$localPath = $tmpFile;
|
||||
} else {
|
||||
// ambil file dari local storage
|
||||
$localPath = storage_path('app/public/' . $value->path);
|
||||
}
|
||||
|
||||
if ($localPath && file_exists($localPath)) {
|
||||
$zip->addFile($localPath, $value->original_name ?? basename($value->path));
|
||||
}
|
||||
}
|
||||
|
||||
$zip->close();
|
||||
|
||||
// return URL ke file zip
|
||||
return response()->json([
|
||||
'file_url' => env('APP_URL') . Storage::url($zipFileName)
|
||||
], 200);
|
||||
}
|
||||
|
||||
$zip->close();
|
||||
|
||||
// Mengembalikan response berupa URL file zip
|
||||
return response()->json(['file_url' => env('APP_URL').Storage::url($zipFileName)], 200);
|
||||
} else {
|
||||
return response()->json(['message' => 'Gagal membuat file ZIP.'], 500);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function downloadTemplate()
|
||||
{
|
||||
@@ -1295,9 +1334,31 @@ class ClaimController extends Controller
|
||||
$documents = DB::table('files')
|
||||
->where('fileable_type', 'App\Models\ClaimRequest')
|
||||
->where('fileable_id', $claim_id)
|
||||
->select('original_name', \DB::raw("CONCAT('" . env('APP_URL') . "/storage/', path) as path"), 'type')
|
||||
->select('id', 'original_name', 'path', 'source', 'type')
|
||||
->orderBy('id', 'desc')
|
||||
->get();
|
||||
->get()
|
||||
->map(function ($row) {
|
||||
if (!$row->path) {
|
||||
$row->path = null;
|
||||
return $row;
|
||||
}
|
||||
|
||||
if ($row->source === 's3') {
|
||||
try {
|
||||
$row->path = Storage::disk('s3')->temporaryUrl(
|
||||
$row->path,
|
||||
now()->addMinutes(60)
|
||||
);
|
||||
} catch (\Exception $e) {
|
||||
$row->path = Storage::disk('s3')->url($row->path);
|
||||
}
|
||||
} else {
|
||||
$row->path = Storage::disk('public')->url($row->path);
|
||||
}
|
||||
|
||||
return $row;
|
||||
});
|
||||
|
||||
$results['documents'] = $documents;
|
||||
|
||||
$request_documents = DB::table('claim_request_files')
|
||||
|
||||
Reference in New Issue
Block a user