|
|
@@ -0,0 +1,70 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace App\Services;
|
|
|
+
|
|
|
+use App\Models\Media;
|
|
|
+use Illuminate\Database\Eloquent\Collection;
|
|
|
+use Illuminate\Http\UploadedFile;
|
|
|
+use Illuminate\Support\Facades\Auth;
|
|
|
+use Illuminate\Support\Facades\Storage;
|
|
|
+
|
|
|
+class MediaService
|
|
|
+{
|
|
|
+ public function getAllBySource(string $source, int $sourceId): Collection
|
|
|
+ {
|
|
|
+ return Media::where('source', $source)
|
|
|
+ ->where('source_id', $sourceId)
|
|
|
+ ->orderBy('created_at', 'desc')
|
|
|
+ ->get();
|
|
|
+ }
|
|
|
+
|
|
|
+ public function findById(int $id): ?Media
|
|
|
+ {
|
|
|
+ return Media::find($id);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function create(array $data): Media
|
|
|
+ {
|
|
|
+ $data['user_id'] = Auth::id();
|
|
|
+ return Media::create($data);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function upload(UploadedFile $file, string $source, int $sourceId, string $mediaType = 'imagem'): Media
|
|
|
+ {
|
|
|
+ $path = $file->store("media/{$source}/{$sourceId}", 'public');
|
|
|
+
|
|
|
+ return $this->create([
|
|
|
+ 'type' => $mediaType,
|
|
|
+ 'source' => $source,
|
|
|
+ 'source_id' => $sourceId,
|
|
|
+ 'path' => $path,
|
|
|
+ 'name' => $file->getClientOriginalName(),
|
|
|
+ 'url' => Storage::url($path),
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function uploadLogo(UploadedFile $file, int $sourceId): Media
|
|
|
+ {
|
|
|
+ Media::where('source', 'partner_agreement_logo')
|
|
|
+ ->where('source_id', $sourceId)
|
|
|
+ ->get()
|
|
|
+ ->each(function (Media $m) {
|
|
|
+ Storage::disk('public')->delete($m->path);
|
|
|
+ $m->forceDelete();
|
|
|
+ });
|
|
|
+
|
|
|
+ return $this->upload($file, 'partner_agreement_logo', $sourceId, 'imagem');
|
|
|
+ }
|
|
|
+
|
|
|
+ public function delete(int $id): bool
|
|
|
+ {
|
|
|
+ $model = $this->findById($id);
|
|
|
+
|
|
|
+ if (!$model) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ Storage::disk('public')->delete($model->path);
|
|
|
+ return $model->delete();
|
|
|
+ }
|
|
|
+}
|