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();
$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')
->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();
->where('fileable_type', 'App\Models\ClaimRequest')
->where('fileable_id', $claimRequestId)
->select('id', 'original_name', 'path', 'source', 'type')
->orderBy('id', 'desc')
->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;
return Helper::responseJson($results);

View File

@@ -130,8 +130,27 @@ class DataServiceMonitoring extends JsonResource
->get();
if ($document){
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[]= [
'path' => env('APP_URL') . '/storage/lab_result/' . $d->name . '.' . $d->extension,
'path' => $path,
'type' => $d->type,
'original_name' => $d->original_name,
'name' => $d->name,
@@ -194,8 +213,28 @@ class DataServiceMonitoring extends JsonResource
->get();
if ($document){
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[]= [
'path' => env('APP_URL') . '/storage/lab_result/' . $d->name . '.' . $d->extension,
'path' => $path,
'type' => $d->type,
'original_name' => $d->original_name,
'name' => $d->name,

View File

@@ -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')

View File

@@ -241,11 +241,34 @@ class InvoicePaymentController extends Controller
'files.id as file_id',
'invoice_payments.amount_paid',
'invoice_payments.payment_number',
DB::raw("CONCAT('" . env('APP_URL') . "/storage/', files.path) as path"),
'files.path',
'files.source',
'files.original_name'
)
->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) {
return [

View File

@@ -990,9 +990,31 @@ class RequestLogController extends Controller
$documents = DB::table('files')
->where('fileable_type', 'App\Models\RequestLog')
->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')
->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;
$dialog_submits = DB::table('claim_requests')
->leftJoin('members', 'claim_requests.member_id','=', 'members.id')

View File

@@ -160,9 +160,29 @@ class RequestDailyMonitoring extends Model
])->get();
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[] = [
'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,
];
}
@@ -179,9 +199,29 @@ class RequestDailyMonitoring extends Model
])->get();
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[] = [
'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,
];
}
@@ -198,9 +238,30 @@ class RequestDailyMonitoring extends Model
])->get();
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[] = [
'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,
];
}