53 lines
1.5 KiB
PHP
53 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\ClassProgressAttachment;
|
|
use App\Models\ClassProgressReport;
|
|
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
|
|
|
class AdminProgressAttachmentController extends Controller
|
|
{
|
|
use HandlesProgressAttachments;
|
|
|
|
/**
|
|
* Download legacy attachment from class_progress_reports.attachment_path
|
|
*/
|
|
public function downloadLegacyByReport(int $reportId): BinaryFileResponse
|
|
{
|
|
$row = ClassProgressReport::query()->find($reportId);
|
|
|
|
if (! $row || empty($row->attachment_path)) {
|
|
abort(404, 'Attachment not found.');
|
|
}
|
|
|
|
$file = $this->resolveAttachmentFile($row->toArray());
|
|
if (! $file) {
|
|
abort(404, 'Attachment missing.');
|
|
}
|
|
|
|
return response()->download($file, basename($file));
|
|
}
|
|
|
|
/**
|
|
* Download file from normalized attachments table
|
|
*/
|
|
public function downloadAttachment(int $attachmentId): BinaryFileResponse
|
|
{
|
|
$attachment = ClassProgressAttachment::query()->find($attachmentId);
|
|
|
|
if (! $attachment || empty($attachment->file_path)) {
|
|
abort(404, 'Attachment not found.');
|
|
}
|
|
|
|
$file = $this->resolveAttachmentPath((string) $attachment->file_path);
|
|
if (! $file) {
|
|
abort(404, 'Attachment missing.');
|
|
}
|
|
|
|
$downloadName = $attachment->original_name ?: basename($file);
|
|
|
|
return response()->download($file, $downloadName);
|
|
}
|
|
} |