{{-- resources/views/agent/files/show.blade.php --}} @extends('layouts.contentLayoutMaster') @php use Illuminate\Support\Facades\Route as Routes; use Illuminate\Support\Facades\Storage as Disk; use Illuminate\Support\Facades\URL as UrlGen; use Illuminate\Support\Str; // Title setup $title = $file->title ?? $file->name ?? $file->original_name ?? 'Document'; // File location and URLs $path = $file->stored_path ?? $file->path ?? null; // should be agent-files/1/documents/2025/09/filename.docx $publicOk = $path ? Disk::disk('public')->exists($path) : false; $publicUrl = ($publicOk && $path) ? Disk::disk('public')->url($path) : null; // Download/stream URLs $streamUrl = ($path && Routes::has('agent.files.stream')) ? route('agent.files.stream', $file->id) : null; $downloadUrl = ($path && Routes::has('agent.files.download')) ? route('agent.files.download', $file->id) : ($publicUrl ?: null); // Safe back URL $backDefault = Routes::has('agent.files.index') ? route('agent.files.index') : (Routes::has('agent.dashboard') ? route('agent.dashboard') : (url()->previous() ?: url('/'))); $backUrl = request('return', $backDefault); // File meta $mime = $file->mime ?? $file->mime_type ?? ''; $ext = strtolower(pathinfo($file->original_name ?? $file->name ?? '', PATHINFO_EXTENSION)); $isImage = Str::startsWith($mime, 'image/'); $isPdf = ($mime === 'application/pdf') || ($ext === 'pdf'); $sizeBytes = (int)($file->size ?? $file->filesize ?? 0); $humanSize = function ($bytes) { $units = ['B','KB','MB','GB','TB']; $i=0; while ($bytes >= 1024 && $i < count($units)-1) { $bytes /= 1024; $i++; } return number_format($bytes, $i ? 2 : 0) . ' ' . $units[$i]; }; // Prefer HTTPS for Office Viewer $publicFullUrl = $publicUrl ? (Str::startsWith($publicUrl, ['http://','https://']) ? $publicUrl : url($publicUrl)) : null; if ($publicFullUrl && Str::startsWith($publicFullUrl, 'http://')) { if (request()->isSecure() || Str::startsWith((string) config('app.url'), 'https://')) { $publicFullUrl = preg_replace('#^http://#', 'https://', $publicFullUrl); } } // Signed temporary public URL fallback $shareUrl = null; if (Routes::has('public.files.show') && $path) { $shareUrl = UrlGen::temporarySignedRoute( 'public.files.show', now()->addMinutes(30), [ 'id' => $file->id, 'filename' => $file->original_name ?? $file->name ?? ('document.' . ($ext ?: 'bin')), ] ); } // Use whichever is available $viewableUrl = $publicFullUrl ?: $shareUrl; // Office formats detection $isOfficeDoc = in_array($ext, ['doc','docx','ppt','pptx','xls','xlsx'], true) || Str::contains($mime, ['officedocument','msword','vnd.ms-powerpoint','vnd.ms-excel']); $officeEmbedUrl = ($viewableUrl && $isOfficeDoc) ? 'https://view.officeapps.live.com/op/embed.aspx?src=' . urlencode($viewableUrl) : null; $googleEmbedUrl = ($viewableUrl && $isOfficeDoc) ? 'https://docs.google.com/gview?embedded=1&url=' . urlencode($viewableUrl) : null; @endphp @section('title', $title) @section('content') @if (!Routes::has('agent.files.index')) @endif
{{-- Flash --}} @if(session('success'))
{{ session('success') }}
@endif @if(session('error'))
{{ session('error') }}
@endif

{{ $title }}

Back @if($downloadUrl) Open / Download @endif
{{-- Debug info for troubleshooting Word doc preview --}} @if($isOfficeDoc)
Preview debug info:
Public URL: {{ $publicFullUrl }}
Office embed URL: {{ $officeEmbedUrl }}
Google embed URL: {{ $googleEmbedUrl }}
Storage Path: {{ $path }}
MIME: {{ $mime }} | Extension: {{ $ext }}
@endif @if (!$path)
No file is attached to this record.
@else

MIME: {{ $mime ?: 'n/a' }} @if($sizeBytes > 0)   •   Size: {{ $humanSize($sizeBytes) }} @endif

@if($googleEmbedUrl) Alternate Viewer (Google) @endif @if($viewableUrl) Open Viewable URL @endif
{{-- Preview priority: Office docs (Office Online) → Google viewer → stream → image/pdf --}} @if ($officeEmbedUrl)
Preview
@elseif ($googleEmbedUrl)
Preview (Google Viewer)
@elseif ($streamUrl)
Preview
@elseif ($publicUrl && ($isImage || $isPdf)) @if($isImage)
Preview
{{ $title }}
@else
Preview
@endif @else
Preview not available for this file type or the public URL isn't accessible.
@if(!$publicUrl && !$downloadUrl)
Tip: ensure the file is on the public disk or use the signed link above.
@endif @if($downloadUrl)
Use Open / Download above to access the file.
@endif
@endif @endif
@endsection