Support PDFs as attachments
MODIFY
app/Http/Controllers/AttachmentController.php
app/Http/Controllers/AttachmentController.php
namespace App\Http\Controllers;
+use App\Models\Attachment;
use App\Repositories\AttachmentRepository;
use Illuminate\Http\Request;
-use Illuminate\Support\Facades\Storage;
+use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;
class AttachmentController extends Controller
@@ -35,6 +36,21 @@ class AttachmentController extends Controller
return redirect('/' . $transactionType . 's/' . $transactionId);
}
+ public function download(Request $request, Attachment $attachment)
+ {
+ $user = Auth::user();
+
+ if (!$user->spaces->contains($attachment->transaction->space_id)) {
+ abort(403);
+ }
+
+ if ($attachment->file_type !== 'pdf') {
+ return null;
+ }
+
+ return response()->download(storage_path() . '/app/' . $attachment->file_path);
+ }
+
public function delete(Request $request, string $id)
{
$attachment = $this->attachmentRepository->getById($id);

MODIFY
app/Models/Attachment.php
app/Models/Attachment.php
namespace App\Models;
+use Exception;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Storage;
@@ -16,6 +17,20 @@ class Attachment extends Model
'file_path'
];
+ // Relationships
+ public function transaction()
+ {
+ if ($this->transaction_type === 'earning') {
+ return $this->belongsTo(Earning::class, 'transaction_id');
+ }
+
+ if ($this->transaction_type) {
+ return $this->belongsTo(Spending::class, 'transaction_id');
+ }
+
+ throw new Exception('Invalid transaction type for attachment (ID ' . $this->id . ')');
+ }
+
// Accessors
public function getFileB64Attribute()
{
@@ -29,4 +44,11 @@ class Attachment extends Model
return 'data:image/' . $type . ';base64,' . base64_encode($file);
}
+
+ public function getFileTypeAttribute()
+ {
+ $parts = explode('.', $this->file_path);
+
+ return $parts[count($parts) - 1];
+ }
}

MODIFY
resources/views/partials/attachments.blade.php
resources/views/partials/attachments.blade.php
@endif
@foreach ($payload->attachments as $attachment)
<div class="box__section row">
+ <div>
+ @if ($attachment->file_type !== 'pdf')
<img src="{{ $attachment->file_b64 }}" style="max-width: 100%; max-height: 200px; border-radius: 5px; vertical-align: top;" />
+ @else
+ <div class="mb-1" style="display: flex; align-items: center; justify-content: center; width: 200px; height: 200px; border-radius: 5px; background: #EEE;">
+ <i class="fas fa-file-pdf"></i>
+ </div>
+ <a href="/attachments/{{ $attachment->id }}/download">Download</a>
+ @endif
+ </div>
<div class="ml-2">
<form method="POST" action="/attachments/{{ $attachment->id }}/delete">
{{ csrf_field() }}

MODIFY
routes/web.php
routes/web.php
@@ -17,6 +17,7 @@ Route::group(['middleware' => ['auth']], function () {
Route::get('/dashboard', 'DashboardController')->name('dashboard');
Route::post('/attachments', 'AttachmentController@store');
+ Route::get('/attachments/{attachment}/download', 'AttachmentController@download');
Route::post('/attachments/{id}/delete', 'AttachmentController@delete');
Route::get('/transactions/create', 'TransactionController@create')->name('transactions.create');

Support PDFs as attachments