MediaService.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. namespace App\Services;
  3. use App\Enums\ApprovalStatusEnum;
  4. use App\Models\Client;
  5. use App\Models\Media;
  6. use App\Models\Provider;
  7. use App\Traits\RemoveArchiveS3;
  8. use App\Traits\UploadsFile;
  9. use Illuminate\Database\Eloquent\Collection;
  10. use Illuminate\Database\Eloquent\ModelNotFoundException;
  11. use Illuminate\Http\UploadedFile;
  12. use Illuminate\Pagination\LengthAwarePaginator;
  13. use Illuminate\Support\Facades\DB;
  14. use Illuminate\Support\Facades\Log;
  15. class MediaService
  16. {
  17. use RemoveArchiveS3, UploadsFile;
  18. public function getAll(): Collection
  19. {
  20. return Media::query()
  21. ->with(['user'])
  22. ->orderBy('created_at', 'desc')
  23. ->get();
  24. }
  25. public function findById(int $id): ?Media
  26. {
  27. return Media::with(['user'])->find($id);
  28. }
  29. public function create(array $data): Media
  30. {
  31. return Media::create($data);
  32. }
  33. public function update(int $id, array $data): ?Media
  34. {
  35. $model = $this->findById($id);
  36. if (! $model) {
  37. return null;
  38. }
  39. $model->update($data);
  40. return $model->fresh(['user']);
  41. }
  42. public function delete(int $id): bool
  43. {
  44. $model = $this->findById($id);
  45. if (! $model) {
  46. return false;
  47. }
  48. return $model->delete();
  49. }
  50. //
  51. public function getPendingProfileMedia(int $page = 1, int $perPage = 10): LengthAwarePaginator
  52. {
  53. return Media::query()
  54. ->where(function ($query) {
  55. $query->where(function ($query) {
  56. $query->where('media.source', 'provider')
  57. ->whereExists(function ($query) {
  58. $query->selectRaw('1')
  59. ->from('providers')
  60. ->whereColumn('providers.id', 'media.source_id')
  61. ->whereColumn('providers.profile_media_id', 'media.id')
  62. ->where('providers.approval_status', ApprovalStatusEnum::ACCEPTED->value)
  63. ->where('providers.selfie_verified', false)
  64. ->whereNull('providers.deleted_at');
  65. });
  66. })->orWhere(function ($query) {
  67. $query->where('media.source', 'client')
  68. ->whereExists(function ($query) {
  69. $query->selectRaw('1')
  70. ->from('clients')
  71. ->whereColumn('clients.id', 'media.source_id')
  72. ->whereColumn('clients.profile_media_id', 'media.id')
  73. ->where('clients.selfie_verified', false)
  74. ->whereNull('clients.deleted_at');
  75. });
  76. });
  77. })
  78. ->orderBy('media.created_at')
  79. ->paginate($perPage, ['media.*'], 'page', $page);
  80. }
  81. public function approveProfileMedia(string $source, int $sourceId): Media
  82. {
  83. return DB::transaction(function () use ($source, $sourceId) {
  84. $profile = match ($source) {
  85. 'client' => Client::query()->lockForUpdate()->findOrFail($sourceId),
  86. 'provider' => Provider::query()->lockForUpdate()->findOrFail($sourceId),
  87. };
  88. $profile->load('profileMedia');
  89. $media = $profile->profileMedia;
  90. if ($media === null) {
  91. throw (new ModelNotFoundException)->setModel(Media::class);
  92. }
  93. $profile->update(['selfie_verified' => true]);
  94. return $media;
  95. });
  96. }
  97. public function createFromFile(UploadedFile $file, string $folder, string $source, int $sourceId, ?string $filename = null): Media
  98. {
  99. Log::warning('[avatar-upload] MediaService::createFromFile — iniciando upload para S3', [
  100. 'folder' => $folder,
  101. 'source' => $source,
  102. 'source_id' => $sourceId,
  103. 'file_original_name' => $file->getClientOriginalName(),
  104. 'file_size' => $file->getSize(),
  105. 'file_mime' => $file->getMimeType(),
  106. 'file_extension' => $file->getClientOriginalExtension(),
  107. 'file_is_valid' => $file->isValid(),
  108. ]);
  109. $path = $this->uploadFile($file, $folder, $filename);
  110. Log::warning('[avatar-upload] MediaService::createFromFile — upload concluído, criando registro Media', [
  111. 'path_retornado' => $path,
  112. 'source' => $source,
  113. 'source_id' => $sourceId,
  114. ]);
  115. $media = Media::create([
  116. 'source' => $source,
  117. 'source_id' => $sourceId,
  118. 'name' => $file->getClientOriginalName(),
  119. 'path' => $path,
  120. ]);
  121. Log::warning('[avatar-upload] MediaService::createFromFile — Media criado no banco', [
  122. 'media_id' => $media->id,
  123. 'media_path' => $media->path,
  124. 'media_url' => $media->url ?? null,
  125. ]);
  126. return $media;
  127. }
  128. public function replaceFile(UploadedFile $newFile, string $folder, string $source, int $sourceId, ?Media $old = null, ?string $filename = null): Media
  129. {
  130. Log::warning('[avatar-upload] MediaService::replaceFile iniciado', [
  131. 'folder' => $folder,
  132. 'source' => $source,
  133. 'source_id' => $sourceId,
  134. 'has_old_media' => $old !== null,
  135. 'old_media_id' => $old?->id,
  136. 'old_path' => $old?->path,
  137. ]);
  138. if ($old) {
  139. Log::warning('[avatar-upload] Removendo arquivo antigo do S3', ['old_path' => $old->path]);
  140. $this->removeArchiveByPath($old->path);
  141. $old->delete();
  142. Log::warning('[avatar-upload] Arquivo antigo removido do S3 e Media deletado do banco');
  143. }
  144. return $this->createFromFile($newFile, $folder, $source, $sourceId, $filename);
  145. }
  146. }