|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Thinktomorrow\AssetLibrary\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
|
6
|
|
|
use Illuminate\Support\Facades\DB; |
|
7
|
|
|
use Thinktomorrow\AssetLibrary\Models\AssetUploader; |
|
8
|
|
|
use Thinktomorrow\AssetLibrary\Models\Application\DeleteAsset; |
|
9
|
|
|
use Spatie\MediaLibrary\Exceptions\FileCannotBeAdded\UnreachableUrl; |
|
10
|
|
|
|
|
11
|
|
|
class ImageToAssetMigrateCommand extends Command |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* The name and signature of the console command. |
|
15
|
|
|
* |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $signature = 'assetlibrary:migrate-image |
|
19
|
|
|
{table} |
|
20
|
|
|
{urlcolumn} |
|
21
|
|
|
{linkedmodel} |
|
22
|
|
|
{idcolumn=id} |
|
23
|
|
|
{ordercolumn?} |
|
24
|
|
|
{localecolumn?} |
|
25
|
|
|
{--force} |
|
26
|
|
|
{--reset} |
|
27
|
|
|
{--dry}'; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* The console command description. |
|
31
|
|
|
* |
|
32
|
|
|
* @var string |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $description = 'Import images from imageurl on a model to assets managed by assetlibrary.'; |
|
35
|
|
|
|
|
36
|
|
|
private $table; |
|
37
|
|
|
private $urlcolumn; |
|
38
|
|
|
private $linkedmodel; |
|
39
|
|
|
private $idcolumn; |
|
40
|
|
|
private $ordercolumn; |
|
41
|
|
|
private $localecolumn; |
|
42
|
|
|
|
|
43
|
|
|
private $nomodel = 0; |
|
44
|
|
|
private $unreachable = 0; |
|
45
|
|
|
private $files = 0; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Execute the console command. |
|
49
|
|
|
* |
|
50
|
|
|
* @return mixed |
|
51
|
|
|
*/ |
|
52
|
13 |
|
public function handle() |
|
53
|
|
|
{ |
|
54
|
13 |
|
\ini_set('memory_limit', '256M'); |
|
55
|
|
|
|
|
56
|
13 |
|
$this->setArguments(); |
|
57
|
|
|
|
|
58
|
13 |
|
$results = $this->getResultsFromDatabase(); |
|
59
|
13 |
|
$bar = $this->output->createProgressBar(count($results)); |
|
60
|
13 |
|
$orderedResults = $this->mapResults($results); |
|
61
|
13 |
|
$isDry = $this->option('dry'); |
|
62
|
|
|
|
|
63
|
13 |
|
$this->handleResetFlag($orderedResults); |
|
64
|
|
|
|
|
65
|
13 |
|
$this->info("\n".'Migrating images.'); |
|
66
|
|
|
|
|
67
|
13 |
|
foreach ($orderedResults as $result) { |
|
68
|
13 |
|
foreach ($result['images'] as $line) { |
|
69
|
13 |
|
$bar->advance(); |
|
70
|
|
|
|
|
71
|
13 |
|
if(! $line) { |
|
72
|
1 |
|
$this->unreachable++; |
|
73
|
1 |
|
continue; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
12 |
|
if($isDry){ |
|
77
|
1 |
|
$this->files++; |
|
78
|
1 |
|
continue; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
try { |
|
82
|
11 |
|
$asset = AssetUploader::uploadFromUrl(public_path($line)); |
|
83
|
1 |
|
} catch (UnreachableUrl $ex) { |
|
84
|
|
|
// increment the amount of unreachable files counter |
|
85
|
1 |
|
$this->unreachable++; |
|
86
|
|
|
|
|
87
|
1 |
|
continue; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
11 |
|
$asset->setOrder($result['order'])->attachToModel($result['model']); |
|
91
|
|
|
|
|
92
|
11 |
|
if ($this->option('force')) { |
|
93
|
1 |
|
unlink(public_path($line)); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
// increment the amount of files migrated counter |
|
97
|
13 |
|
$this->files++; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
13 |
|
$bar->finish(); |
|
102
|
|
|
|
|
103
|
13 |
|
$this->info('Migrating done.'); |
|
104
|
13 |
|
$this->info('Migrated '.$this->files.' files.'); |
|
105
|
13 |
|
$this->info('Couldn\'t reach '.$this->unreachable.' files.'); |
|
106
|
13 |
|
$this->info('Couldn\'t find '.$this->nomodel.' model(s).'); |
|
107
|
13 |
|
} |
|
108
|
|
|
|
|
109
|
13 |
|
private function getResultsFromDatabase() |
|
110
|
|
|
{ |
|
111
|
13 |
|
$columns = [$this->urlcolumn, $this->idcolumn, $this->ordercolumn, $this->localecolumn]; |
|
112
|
|
|
|
|
113
|
13 |
|
$builder = DB::table($this->table)->select($columns); |
|
114
|
|
|
|
|
115
|
13 |
|
if (! $this->ordercolumn) { |
|
116
|
12 |
|
$builder = $builder->orderBy($this->idcolumn); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
13 |
|
$results = $builder->get(); |
|
120
|
|
|
|
|
121
|
13 |
|
return $results; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
13 |
|
private function setArguments() |
|
125
|
|
|
{ |
|
126
|
13 |
|
$this->table = $this->argument('table'); |
|
127
|
13 |
|
$this->urlcolumn = $this->argument('urlcolumn'); |
|
128
|
13 |
|
$this->linkedmodel = $this->argument('linkedmodel'); |
|
129
|
13 |
|
$this->idcolumn = $this->argument('idcolumn'); |
|
130
|
13 |
|
$this->ordercolumn = $this->argument('ordercolumn'); |
|
131
|
13 |
|
$this->localecolumn = $this->argument('localecolumn'); |
|
132
|
13 |
|
} |
|
133
|
|
|
|
|
134
|
13 |
|
private function handleResetFlag($orderedResults) |
|
135
|
|
|
{ |
|
136
|
13 |
|
if ($this->option('reset') && ! $this->option('dry')) { |
|
137
|
1 |
|
$this->info('Resetting the assets on the models'); |
|
138
|
1 |
|
$resetbar = $this->output->createProgressBar(count($orderedResults)); |
|
139
|
|
|
|
|
140
|
|
|
$orderedResults->each(function ($entry) use ($resetbar) { |
|
141
|
1 |
|
app(DeleteAsset::class)->deleteAll($entry['model']); |
|
142
|
1 |
|
$resetbar->advance(); |
|
143
|
1 |
|
}); |
|
144
|
|
|
|
|
145
|
1 |
|
$resetbar->finish(); |
|
146
|
|
|
} |
|
147
|
13 |
|
} |
|
148
|
|
|
|
|
149
|
13 |
|
private function mapResults($results) |
|
150
|
|
|
{ |
|
151
|
|
|
return $results->map(function ($result) { |
|
152
|
13 |
|
$formattedResults = []; |
|
153
|
|
|
|
|
154
|
13 |
|
$formattedResults['images'][] = $result->{$this->urlcolumn}; |
|
155
|
13 |
|
$formattedResults['model'] = $this->linkedmodel::find($result->{$this->idcolumn}); |
|
156
|
|
|
|
|
157
|
13 |
|
if ($this->ordercolumn) { |
|
158
|
1 |
|
$formattedResults['order'] = $result->{$this->ordercolumn}; |
|
159
|
|
|
}else{ |
|
160
|
12 |
|
$formattedResults['order'] = null; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
13 |
|
return $formattedResults; |
|
164
|
|
|
})->reject(function ($value) { |
|
165
|
13 |
|
if ($result = $value['model'] == null) { |
|
166
|
1 |
|
$this->nomodel++; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
13 |
|
return $result; |
|
170
|
13 |
|
}); |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|