1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Services; |
4
|
|
|
|
5
|
|
|
use App\Models\Album; |
6
|
|
|
use App\Models\Artist; |
7
|
|
|
use Exception; |
8
|
|
|
use Psr\Log\LoggerInterface; |
9
|
|
|
|
10
|
|
|
class MediaMetadataService |
11
|
|
|
{ |
12
|
|
|
private $logger; |
13
|
|
|
|
14
|
132 |
|
public function __construct(LoggerInterface $logger) |
15
|
|
|
{ |
16
|
132 |
|
$this->logger = $logger; |
17
|
132 |
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Download a copy of the album cover. |
21
|
|
|
*/ |
22
|
|
|
public function downloadAlbumCover(Album $album, string $imageUrl): void |
23
|
|
|
{ |
24
|
|
|
$extension = explode('.', $imageUrl); |
25
|
|
|
$this->writeAlbumCover($album, file_get_contents($imageUrl), last($extension)); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Copy a cover file from an existing image on the system. |
30
|
|
|
* |
31
|
|
|
* @param string $source The original image's full path. |
32
|
|
|
* @param string $destination The destination path. Automatically generated if empty. |
33
|
|
|
*/ |
34
|
7 |
|
public function copyAlbumCover(Album $album, string $source, string $destination = ''): void |
35
|
|
|
{ |
36
|
7 |
|
$extension = pathinfo($source, PATHINFO_EXTENSION); |
37
|
7 |
|
$destination = $destination ?: $this->generateAlbumCoverPath($album, $extension); |
38
|
7 |
|
copy($source, $destination); |
39
|
|
|
|
40
|
7 |
|
$album->update(['cover' => basename($destination)]); |
41
|
7 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Write an album cover image file with binary data and update the Album with the new cover attribute. |
45
|
|
|
* |
46
|
|
|
* @param string $destination The destination path. Automatically generated if empty. |
47
|
|
|
*/ |
48
|
7 |
|
public function writeAlbumCover(Album $album, string $binaryData, string $extension, string $destination = ''): void |
49
|
|
|
{ |
50
|
|
|
try { |
51
|
7 |
|
$extension = trim(strtolower($extension), '. '); |
52
|
7 |
|
$destination = $destination ?: $this->generateAlbumCoverPath($album, $extension); |
53
|
7 |
|
file_put_contents($destination, $binaryData); |
54
|
|
|
|
55
|
7 |
|
$album->update(['cover' => basename($destination)]); |
56
|
|
|
} catch (Exception $e) { |
57
|
|
|
$this->logger->error($e); |
58
|
|
|
} |
59
|
7 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Download a copy of the artist image. |
63
|
|
|
*/ |
64
|
|
|
public function downloadArtistImage(Artist $artist, string $imageUrl): void |
65
|
|
|
{ |
66
|
|
|
$extension = explode('.', $imageUrl); |
67
|
|
|
$this->writeArtistImage($artist, file_get_contents($imageUrl), last($extension)); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Write an artist image file with binary data and update the Artist with the new image attribute. |
72
|
|
|
* |
73
|
|
|
* @param string $destination The destination path. Automatically generated if empty. |
74
|
|
|
*/ |
75
|
1 |
|
public function writeArtistImage( |
76
|
|
|
Artist $artist, |
77
|
|
|
string $binaryData, |
78
|
|
|
string $extension, |
79
|
|
|
string $destination = '' |
80
|
|
|
): void { |
81
|
|
|
try { |
82
|
1 |
|
$extension = trim(strtolower($extension), '. '); |
83
|
1 |
|
$destination = $destination ?: $this->generateArtistImagePath($artist, $extension); |
84
|
1 |
|
file_put_contents($destination, $binaryData); |
85
|
|
|
|
86
|
1 |
|
$artist->update(['image' => basename($destination)]); |
87
|
|
|
} catch (Exception $e) { |
88
|
|
|
$this->logger->error($e); |
89
|
|
|
} |
90
|
1 |
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Generate the absolute path for an album cover image. |
94
|
|
|
* |
95
|
|
|
* @param string $extension The extension of the cover (without dot) |
96
|
|
|
*/ |
97
|
6 |
|
private function generateAlbumCoverPath(Album $album, string $extension): string |
98
|
|
|
{ |
99
|
6 |
|
return sprintf('%s/public/img/covers/%s.%s', app()->publicPath(), sha1($album->id), $extension); |
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Generate the absolute path for an artist image. |
104
|
|
|
* |
105
|
|
|
* @param string $extension The extension of the cover (without dot) |
106
|
|
|
*/ |
107
|
|
|
private function generateArtistImagePath(Artist $artist, $extension): string |
108
|
|
|
{ |
109
|
|
|
return sprintf('%s/public/img/artists/%s.%s', app()->publicPath(), sha1($artist->id), $extension); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|