1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* rmarchiv.tk |
5
|
|
|
* (c) 2016-2017 by Marcel 'ryg' Hering |
6
|
|
|
* |
7
|
|
|
* Get all rar gamefiles and repack with zip |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace App\Console\Commands; |
11
|
|
|
|
12
|
|
|
use App\Models\GamesFile; |
13
|
|
|
use Illuminate\Console\Command; |
14
|
|
|
|
15
|
|
|
class PlayerRar2Zip extends Command |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* The name and signature of the console command. |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $signature = 'player:rar2zip'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The console command description. |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $description = 'convert rar files to zip'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Create a new command instance. |
33
|
|
|
* |
34
|
|
|
* @return void |
|
|
|
|
35
|
|
|
*/ |
36
|
|
|
public function __construct() |
37
|
|
|
{ |
38
|
|
|
parent::__construct(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Execute the console command. |
43
|
|
|
* |
44
|
|
|
* @return mixed |
45
|
|
|
*/ |
46
|
|
|
public function handle() |
47
|
|
|
{ |
48
|
|
|
//get all rar files from database |
49
|
|
|
$files = GamesFile::whereExtension('rar')->orderBy('filesize', 'asc')->get(); |
|
|
|
|
50
|
|
|
|
51
|
|
|
foreach ($files as $f) { |
52
|
|
|
//Check for maker engine 2=rm2k, 3=rm2k3, 9=rm2k3 Steam Edition |
53
|
|
|
if (! array_search($f->game->maker_id, [2, 3, 6, 9, 11]) === false) { |
54
|
|
|
echo "Gamefile: $f->filename"; |
|
|
|
|
55
|
|
|
|
56
|
|
|
//prepare the path variables |
57
|
|
|
$pathrar = storage_path('app/public/'.$f->filename); // Path to original rar file |
|
|
|
|
58
|
|
|
$pathzip = storage_path('app/public/'.str_replace('.rar', '.zip', $f->filename)); //Path to zip destination |
|
|
|
|
59
|
|
|
$pathdest = storage_path('app/public/games/'.$f->id.'/'); //destination for unrared files |
60
|
|
|
|
61
|
|
|
//delete old temp files (just in case.) |
62
|
|
|
$this->Delete($pathdest); |
63
|
|
|
|
64
|
|
|
// unrar the rar archive |
65
|
|
|
$command = 'unrar x \''.$pathrar.'\' '.$pathdest; |
66
|
|
|
exec($command); |
67
|
|
|
|
68
|
|
|
$handle = opendir($pathdest); // erm?? |
|
|
|
|
69
|
|
|
|
70
|
|
|
//zip previous decompressed files |
71
|
|
|
$this->Zip($pathdest, $pathzip); |
72
|
|
|
|
73
|
|
|
// Check for zip file |
74
|
|
|
if(!file_exists($pathzip)){ |
75
|
|
|
// zip file does not exist. debug & die. |
76
|
|
|
dd($pathzip); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
//delete decomressed files |
80
|
|
|
$this->Delete($pathdest); |
81
|
|
|
|
82
|
|
|
//Update the Database |
83
|
|
|
$upd = GamesFile::whereId($f->id)->first(); |
|
|
|
|
84
|
|
|
$upd->extension = 'zip'; |
85
|
|
|
$upd->filename = str_replace('.rar', '.zip', $f->filename); |
|
|
|
|
86
|
|
|
$upd->save(); |
87
|
|
|
|
88
|
|
|
//delete rar file |
89
|
|
|
unlink($pathrar); |
90
|
|
|
|
91
|
|
|
echo " - done\n"; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function Delete($path) |
97
|
|
|
{ |
98
|
|
|
// check for directory is not a file |
99
|
|
|
if (is_dir($path) === true) { |
100
|
|
|
// get all files and directorys recursive from all directorys |
101
|
|
|
$files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path), \RecursiveIteratorIterator::CHILD_FIRST); |
102
|
|
|
|
103
|
|
|
foreach ($files as $file) { |
104
|
|
|
// if file is . or .. skip |
105
|
|
|
if (in_array($file->getBasename(), ['.', '..']) !== true) { |
106
|
|
|
// if $file is a directory |
107
|
|
|
if ($file->isDir() === true) { |
108
|
|
|
// delete directory |
109
|
|
|
rmdir($file->getPathName()); |
110
|
|
|
} elseif (($file->isFile() === true) || ($file->isLink() === true)) { // or a file |
111
|
|
|
// delete file |
112
|
|
|
unlink($file->getPathname()); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return rmdir($path); |
118
|
|
|
} elseif ((is_file($path) === true) || (is_link($path) === true)) { |
119
|
|
|
return unlink($path); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return false; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function Zip($source, $destination) |
|
|
|
|
126
|
|
|
{ |
127
|
|
|
// check for php_zip extension and file existance |
128
|
|
|
if (! extension_loaded('zip') || ! file_exists($source)) { |
129
|
|
|
return false; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
// create ZipArchive Object |
133
|
|
|
$zip = new \ZipArchive(); |
134
|
|
|
|
135
|
|
|
// Create a new ZIP |
136
|
|
|
if (! $zip->open($destination, \ZIPARCHIVE::CREATE)) { |
137
|
|
|
return false; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$source = str_replace('\\', '/', realpath($source)); |
141
|
|
|
|
142
|
|
|
if (is_dir($source) === true) { |
143
|
|
|
$files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($source), \RecursiveIteratorIterator::SELF_FIRST); |
144
|
|
|
|
145
|
|
|
foreach ($files as $file) { |
146
|
|
|
$file = str_replace('\\', '/', $file); |
147
|
|
|
|
148
|
|
|
// Ignore "." and ".." folders |
149
|
|
|
if (in_array(substr($file, strrpos($file, '/') + 1), ['.', '..'])) { |
150
|
|
|
continue; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$file = realpath($file); |
154
|
|
|
|
155
|
|
|
if (is_dir($file) === true) { |
156
|
|
|
$zip->addEmptyDir(str_replace($source.'/', '', $file.'/')); |
157
|
|
|
} elseif (is_file($file) === true) { |
158
|
|
|
$zip->addFromString(str_replace($source.'/', '', $file), file_get_contents($file)); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
} elseif (is_file($source) === true) { |
162
|
|
|
$zip->addFromString(basename($source), file_get_contents($source)); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return $zip->close(); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.