<?php

namespace App\Http\Controllers\Utils;

use App\Utils\Helper;
use App\Models\Attachment;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Storage;

class AttachmentController extends Controller
{
  public function show($id, Request $request)
  {
    try {
      $id = base64_decode($id);
      $attachment = Attachment::find($id);
      if (!$attachment) {
        abort(404, "File tidak ditemukan");
      }

      if (!Storage::disk('local')->exists($attachment->path)) {
        abort(404, "File tidak ditemukan");
      }

      return response()->file(Storage::disk('local')->path($attachment->path));
    } catch (\Throwable $th) {
      abort(500, $th->getMessage());
    }
  }
}
