bugs fix ambil path di list provider

This commit is contained in:
Server D3 Linksehat
2025-08-27 16:30:44 +07:00
parent 9900c00b8d
commit a9423e8b0a
6 changed files with 279 additions and 39 deletions

View File

@@ -133,12 +133,46 @@ class ClaimReportController extends Controller
) )
->get(); ->get();
$results['request_files'] = $request_files; $results['request_files'] = $request_files;
// Jika path kosong, kembalikan null
if (!$row->path) {
return null;
}
// $documents = DB::table('files')
// ->where('fileable_type', 'App\Models\ClaimRequest')
// ->where('fileable_id', $claimRequestId)
// ->select('original_name', \DB::raw("CONCAT('" . env('APP_URL') . "/storage/', path) as path"), 'type')
// ->orderBy('id', 'desc')
// ->get();
$documents = DB::table('files') $documents = DB::table('files')
->where('fileable_type', 'App\Models\ClaimRequest') ->where('fileable_type', 'App\Models\ClaimRequest')
->where('fileable_id', $claimRequestId) ->where('fileable_id', $claimRequestId)
->select('original_name', \DB::raw("CONCAT('" . env('APP_URL') . "/storage/', path) as path"), 'type') ->select('id', 'original_name', 'path', 'source', 'type')
->orderBy('id', 'desc') ->orderBy('id', 'desc')
->get(); ->get()
->map(function ($row) {
// default null kalau tidak ada path
if (!$row->path) {
$row->path = null; // atau bisa $row->url = 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; $results['documents'] = $documents;
return Helper::responseJson($results); return Helper::responseJson($results);

View File

@@ -130,8 +130,27 @@ class DataServiceMonitoring extends JsonResource
->get(); ->get();
if ($document){ if ($document){
foreach($document as $d){ foreach($document as $d){
// Jika path kosong, kembalikan null
if (!$row->path) {
return null;
}
// Cek nilai 'source'. Jika 's3', gunakan disk S3.
// Selain itu (termasuk null atau 'local'), gunakan disk 'public'.
if ($row->source === 's3') {
try {
$path = Storage::disk('s3')->temporaryUrl(
$row->path,
now()->addMinutes(60) // expired 1 jam
);
} catch (\Exception $e) {
$path = Storage::disk('s3')->url($row->path); // fallback kalau public
}
} else {
$path = Storage::disk('public')->url($row->path);
}
$arr_document[]= [ $arr_document[]= [
'path' => env('APP_URL') . '/storage/lab_result/' . $d->name . '.' . $d->extension, 'path' => $path,
'type' => $d->type, 'type' => $d->type,
'original_name' => $d->original_name, 'original_name' => $d->original_name,
'name' => $d->name, 'name' => $d->name,
@@ -194,8 +213,28 @@ class DataServiceMonitoring extends JsonResource
->get(); ->get();
if ($document){ if ($document){
foreach($document as $d){ foreach($document as $d){
// Jika path kosong, kembalikan null
if (!$row->path) {
return null;
}
// Cek nilai 'source'. Jika 's3', gunakan disk S3.
// Selain itu (termasuk null atau 'local'), gunakan disk 'public'.
if ($row->source === 's3') {
try {
$path = Storage::disk('s3')->temporaryUrl(
$row->path,
now()->addMinutes(60) // expired 1 jam
);
} catch (\Exception $e) {
$path = Storage::disk('s3')->url($row->path); // fallback kalau public
}
} else {
$path = Storage::disk('public')->url($row->path);
}
$arr_document[]= [ $arr_document[]= [
'path' => env('APP_URL') . '/storage/lab_result/' . $d->name . '.' . $d->extension, 'path' => $path,
'type' => $d->type, 'type' => $d->type,
'original_name' => $d->original_name, 'original_name' => $d->original_name,
'name' => $d->name, 'name' => $d->name,

View File

@@ -195,7 +195,10 @@ class ClaimController extends Controller
$limit = $request->has('per_page') ? $request->input('per_page') : 50; $limit = $request->has('per_page') ? $request->input('per_page') : 50;
$results = DB::table('request_logs') $results = DB::table('request_logs')
->leftJoin('members', 'request_logs.member_id', '=', 'members.id') ->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') // ->leftJoin('member_plans', 'member_plans.member_id', '=', 'members.id')
->when($request->input('search'), function ($query, $search) { ->when($request->input('search'), function ($query, $search) {
$query->where(function ($query) use ($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 (SELECT organizations.name FROM organizations WHERE organizations.id = request_logs.organization_id LIMIT 1) AS provider
'), '),
'request_logs.status_final_log as status', 'request_logs.status_final_log as status',
DB::raw("CONCAT('" . env('APP_URL') . "/storage/', path) as path") 'files.path',
'files.source'
) )
->paginate($limit); ->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)); return response()->json(Helper::paginateResources($results));
} }
public function downloadZip(Request $request) public function downloadZip(Request $request)
{ {
$selectedRows = $request->selectedRows; // asumsi $selectedRows berisi array ID file yang dipilih $selectedRows = $request->selectedRows; // array ID file
$files = []; $files = [];
// Ambil path file dari database atau sumber lain sesuai dengan $selectedRows // Ambil path dan source
$data = DB::table('files') $data = DB::table('files')
->whereIn('id', $selectedRows) ->whereIn('id', $selectedRows)
->select('path') ->select('path', 'source', 'original_name')
->get(); ->get();
foreach ($data as $value) { $zipFileName = 'downloaded_files_' . time() . '.zip';
$files[] = storage_path('app/public/' . $value->path); $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)) { if ($value->source === 's3') {
foreach ($files as $file) { // download file dari S3 ke temporary file
$zip->addFile($file, basename($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); return response()->json(['message' => 'Gagal membuat file ZIP.'], 500);
} }
}
public function downloadTemplate() public function downloadTemplate()
{ {
@@ -1295,9 +1334,31 @@ class ClaimController extends Controller
$documents = DB::table('files') $documents = DB::table('files')
->where('fileable_type', 'App\Models\ClaimRequest') ->where('fileable_type', 'App\Models\ClaimRequest')
->where('fileable_id', $claim_id) ->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') ->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; $results['documents'] = $documents;
$request_documents = DB::table('claim_request_files') $request_documents = DB::table('claim_request_files')

View File

@@ -241,11 +241,34 @@ class InvoicePaymentController extends Controller
'files.id as file_id', 'files.id as file_id',
'invoice_payments.amount_paid', 'invoice_payments.amount_paid',
'invoice_payments.payment_number', 'invoice_payments.payment_number',
DB::raw("CONCAT('" . env('APP_URL') . "/storage/', files.path) as path"), 'files.path',
'files.source',
'files.original_name' 'files.original_name'
) )
->orderBy('invoice_payments.payment_number', 'asc') ->orderBy('invoice_payments.payment_number', 'asc')
->get(); ->get()
->map(function ($row) {
// default null kalau tidak ada path
if (!$row->path) {
$row->path = null; // atau bisa $row->url = 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;
});
$invoice['files'] = $payments->groupBy('payment_number')->map(function ($group) { $invoice['files'] = $payments->groupBy('payment_number')->map(function ($group) {
return [ return [

View File

@@ -990,9 +990,31 @@ class RequestLogController extends Controller
$documents = DB::table('files') $documents = DB::table('files')
->where('fileable_type', 'App\Models\RequestLog') ->where('fileable_type', 'App\Models\RequestLog')
->where('fileable_id', $claimRequestId) ->where('fileable_id', $claimRequestId)
->select('original_name', \DB::raw("CONCAT('" . env('APP_URL') . "/storage/', path) as path"), 'type') ->select('original_name', 'path', 'source','type')
->orderBy('id', 'desc') ->orderBy('id', 'desc')
->get(); ->get()
->map(function ($row) {
// default null kalau tidak ada path
if (!$row->path) {
$row->path = null; // atau bisa $row->url = 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; $results['documents'] = $documents;
$dialog_submits = DB::table('claim_requests') $dialog_submits = DB::table('claim_requests')
->leftJoin('members', 'claim_requests.member_id','=', 'members.id') ->leftJoin('members', 'claim_requests.member_id','=', 'members.id')

View File

@@ -160,9 +160,29 @@ class RequestDailyMonitoring extends Model
])->get(); ])->get();
foreach ($document as $row) { foreach ($document as $row) {
// Jika path kosong, kembalikan null
if (!$row->path) {
return null;
}
// Cek nilai 'source'. Jika 's3', gunakan disk S3.
// Selain itu (termasuk null atau 'local'), gunakan disk 'public'.
if ($row->source === 's3') {
try {
$path = Storage::disk('s3')->temporaryUrl(
$row->path,
now()->addMinutes(60) // expired 1 jam
);
} catch (\Exception $e) {
$path = Storage::disk('s3')->url($row->path); // fallback kalau public
}
} else {
$path = Storage::disk('public')->url($row->path);
}
$arr_document[] = [ $arr_document[] = [
'name' => $row->original_name, 'name' => $row->original_name,
'path' => env('APP_URL') . '/storage/lab_result/' . $row->name .'.'. $row->extension, // 'path' => env('APP_URL') . '/storage/lab_result/' . $row->name .'.'. $row->extension,
'path' => $path,
'type' => $row->type, 'type' => $row->type,
]; ];
} }
@@ -179,9 +199,29 @@ class RequestDailyMonitoring extends Model
])->get(); ])->get();
foreach ($document as $row) { foreach ($document as $row) {
// Jika path kosong, kembalikan null
if (!$row->path) {
return null;
}
// Cek nilai 'source'. Jika 's3', gunakan disk S3.
// Selain itu (termasuk null atau 'local'), gunakan disk 'public'.
if ($row->source === 's3') {
try {
$path = Storage::disk('s3')->temporaryUrl(
$row->path,
now()->addMinutes(60) // expired 1 jam
);
} catch (\Exception $e) {
$path = Storage::disk('s3')->url($row->path); // fallback kalau public
}
} else {
$path = Storage::disk('public')->url($row->path);
}
$arr_document[] = [ $arr_document[] = [
'name' => $row->original_name, 'name' => $row->original_name,
'path' => env('APP_URL') . '/storage/lab_result/' . $row->name .'.'. $row->extension, // 'path' => env('APP_URL') . '/storage/lab_result/' . $row->name .'.'. $row->extension,
'path' => $path,
'type' => $row->type, 'type' => $row->type,
]; ];
} }
@@ -198,9 +238,30 @@ class RequestDailyMonitoring extends Model
])->get(); ])->get();
foreach ($document as $row) { foreach ($document as $row) {
// Jika path kosong, kembalikan null
if (!$row->path) {
return null;
}
// Cek nilai 'source'. Jika 's3', gunakan disk S3.
// Selain itu (termasuk null atau 'local'), gunakan disk 'public'.
if ($row->source === 's3') {
try {
$path = Storage::disk('s3')->temporaryUrl(
$row->path,
now()->addMinutes(60) // expired 1 jam
);
} catch (\Exception $e) {
$path = Storage::disk('s3')->url($row->path); // fallback kalau public
}
} else {
$path = Storage::disk('public')->url($row->path);
}
$arr_document[] = [ $arr_document[] = [
'name' => $row->original_name, 'name' => $row->original_name,
'path' => env('APP_URL') . '/storage/lab_result/' . $row->name .'.'. $row->extension, // 'path' => env('APP_URL') . '/storage/lab_result/' . $row->name .'.'. $row->extension,
'path' => $path,
'type' => $row->type, 'type' => $row->type,
]; ];
} }