1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Models; |
4
|
|
|
|
5
|
|
|
use App\Facades\Download; |
6
|
|
|
use Exception; |
7
|
|
|
use Illuminate\Support\Collection; |
8
|
|
|
use Illuminate\Support\Facades\Log; |
9
|
|
|
use ZipArchive; |
10
|
|
|
|
11
|
|
|
class SongZipArchive |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var ZipArchive |
15
|
|
|
*/ |
16
|
|
|
protected $archive; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Path to the zip archive |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $path; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Names of the files in the archive |
27
|
|
|
* Format: [file-name.mp3' => currentFileIndex] |
28
|
|
|
* |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
protected $fileNames = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param string $path |
35
|
|
|
* |
36
|
|
|
* @throws Exception |
37
|
|
|
*/ |
38
|
|
|
public function __construct($path = '') |
39
|
|
|
{ |
40
|
|
|
if (!class_exists('ZipArchive')) { |
41
|
|
|
throw new Exception('Downloading multiple files requires ZipArchive module.'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
// We use system's temp dir instead of storage_path() here, so that the generated files |
45
|
|
|
// can be cleaned up automatically after server reboot. |
46
|
|
|
$this->path = $path ?: $path = rtrim(sys_get_temp_dir(), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.'koel-download-'.uniqid().'.zip'; |
47
|
|
|
|
48
|
|
|
$this->archive = new ZipArchive(); |
49
|
|
|
|
50
|
|
|
if ($this->archive->open($this->path, ZipArchive::CREATE) !== true) { |
51
|
|
|
throw new Exception('Cannot create zip file.'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $this; |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Add multiple songs into the archive. |
59
|
|
|
* |
60
|
|
|
* @param Collection $songs |
61
|
|
|
* |
62
|
|
|
* @return $this |
63
|
|
|
*/ |
64
|
|
|
public function addSongs(Collection $songs) |
65
|
|
|
{ |
66
|
|
|
$songs->each(function ($song) { |
67
|
|
|
$this->addSong($song); |
68
|
|
|
}); |
69
|
|
|
|
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Add a single song into the archive. |
75
|
|
|
* |
76
|
|
|
* @param Song $song |
77
|
|
|
* |
78
|
|
|
* @return $this |
79
|
|
|
*/ |
80
|
|
|
public function addSong(Song $song) |
81
|
|
|
{ |
82
|
|
|
try { |
83
|
|
|
$path = Download::fromSong($song); |
84
|
|
|
|
85
|
|
|
// We add all files into the zip archive as a flat structure. |
86
|
|
|
// As a result, there can be duplicate file names. |
87
|
|
|
// The following several lines are to make sure each file name is unique. |
88
|
|
|
$name = basename($path); |
89
|
|
|
if (array_key_exists($name, $this->fileNames)) { |
90
|
|
|
++$this->fileNames[$name]; |
91
|
|
|
$parts = explode('.', $name); |
92
|
|
|
$ext = $parts[count($parts) - 1]; |
93
|
|
|
$parts[count($parts) - 1] = $this->fileNames[$name].".$ext"; |
94
|
|
|
$name = implode('.', $parts); |
95
|
|
|
} else { |
96
|
|
|
$this->fileNames[$name] = 1; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$this->archive->addFile($path, $name); |
100
|
|
|
} catch (Exception $e) { |
101
|
|
|
Log::error($e); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Finish (close) the archive. |
109
|
|
|
* |
110
|
|
|
* @return $this |
111
|
|
|
*/ |
112
|
|
|
public function finish() |
113
|
|
|
{ |
114
|
|
|
$this->archive->close(); |
115
|
|
|
|
116
|
|
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Get the path to the archive. |
121
|
|
|
* |
122
|
|
|
* @return string |
123
|
|
|
*/ |
124
|
|
|
public function getPath() |
125
|
|
|
{ |
126
|
|
|
return $this->path; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|