|
20 | 20 |
|
21 | 21 | namespace App\Console\Commands; |
22 | 22 |
|
23 | | -use App\Jobs\PusherTranscriptionJob; |
24 | 23 | use Illuminate\Console\Command; |
25 | | -use Illuminate\Support\Facades\DB; |
26 | | -use Illuminate\Support\Facades\Storage; |
27 | 24 |
|
28 | 25 | /** |
29 | 26 | * Class UpdateQueries |
@@ -51,209 +48,5 @@ public function __construct() |
51 | 48 | /** |
52 | 49 | * Fire command |
53 | 50 | */ |
54 | | - public function handle() |
55 | | - { |
56 | | - $operation = $this->argument('operation'); |
57 | | - |
58 | | - switch ($operation) { |
59 | | - case 'create-directories': |
60 | | - $this->createS3Directories(); |
61 | | - break; |
62 | | - |
63 | | - case 'move-files': |
64 | | - $this->moveS3DirectoryFiles(); |
65 | | - break; |
66 | | - |
67 | | - case 'update-paths': |
68 | | - $this->updateDatabasePaths(); |
69 | | - break; |
70 | | - case 'process-classifications': |
71 | | - $this->processClassifications(); |
72 | | - break; |
73 | | - |
74 | | - default: |
75 | | - $this->createS3Directories(); |
76 | | - $this->moveS3DirectoryFiles(); |
77 | | - $this->updateDatabasePaths(); |
78 | | - $this->processClassifications(); |
79 | | - } |
80 | | - } |
81 | | - |
82 | | - /** |
83 | | - * Create required directories in S3 bucket |
84 | | - */ |
85 | | - protected function createS3Directories(): void |
86 | | - { |
87 | | - $directories = [ |
88 | | - config('config.uploads.site-assets'), |
89 | | - config('config.uploads.project-assets'), |
90 | | - ]; |
91 | | - |
92 | | - foreach ($directories as $directory) { |
93 | | - try { |
94 | | - if (! Storage::disk('s3')->exists($directory)) { |
95 | | - Storage::disk('s3')->makeDirectory($directory); |
96 | | - } |
97 | | - } catch (\Exception $e) { |
98 | | - $this->error("Failed to create directory {$directory}: ".$e->getMessage()); |
99 | | - } |
100 | | - } |
101 | | - } |
102 | | - |
103 | | - /** |
104 | | - * Move files between S3 directories using AWS CLI for optimal performance |
105 | | - */ |
106 | | - protected function moveS3DirectoryFiles(): void |
107 | | - { |
108 | | - $bucket = config('filesystems.disks.s3.bucket'); |
109 | | - $region = config('filesystems.disks.s3.region'); |
110 | | - |
111 | | - $migrations = [ |
112 | | - [ |
113 | | - 'from' => config('config.uploads.project_resources_downloads'), |
114 | | - 'to' => config('config.uploads.project-assets'), |
115 | | - 'name' => 'project resources to project assets', |
116 | | - ], |
117 | | - [ |
118 | | - 'from' => config('config.uploads.resources'), |
119 | | - 'to' => config('config.uploads.site-assets'), |
120 | | - 'name' => 'resources to site assets', |
121 | | - ], |
122 | | - ]; |
123 | | - |
124 | | - foreach ($migrations as $migration) { |
125 | | - $this->migrateS3Directory($bucket, $region, $migration['from'], $migration['to'], $migration['name']); |
126 | | - } |
127 | | - } |
128 | | - |
129 | | - /** |
130 | | - * Migrate files from one S3 directory to another using AWS CLI |
131 | | - */ |
132 | | - protected function migrateS3Directory(string $bucket, string $region, string $fromDir, string $toDir, string $description): void |
133 | | - { |
134 | | - // Check if source directory has files |
135 | | - $listCommand = sprintf( |
136 | | - 'aws s3 ls s3://%s/%s/ --region %s --recursive', |
137 | | - escapeshellarg($bucket), |
138 | | - escapeshellarg($fromDir), |
139 | | - escapeshellarg($region) |
140 | | - ); |
141 | | - |
142 | | - $output = []; |
143 | | - $returnCode = 0; |
144 | | - exec($listCommand.' 2>/dev/null', $output, $returnCode); |
145 | | - |
146 | | - if (empty($output)) { |
147 | | - return; |
148 | | - } |
149 | | - |
150 | | - $fileCount = count($output); |
151 | | - |
152 | | - // Get initial count of destination directory |
153 | | - $destListCommand = sprintf( |
154 | | - 'aws s3 ls s3://%s/%s/ --region %s --recursive', |
155 | | - escapeshellarg($bucket), |
156 | | - escapeshellarg($toDir), |
157 | | - escapeshellarg($region) |
158 | | - ); |
159 | | - |
160 | | - $destInitialOutput = []; |
161 | | - $destInitialReturnCode = 0; |
162 | | - exec($destListCommand.' 2>/dev/null', $destInitialOutput, $destInitialReturnCode); |
163 | | - |
164 | | - $initialDestFileCount = count($destInitialOutput); |
165 | | - $expectedFinalCount = $initialDestFileCount + $fileCount; |
166 | | - |
167 | | - // Copy files to new directory |
168 | | - $copyCommand = sprintf( |
169 | | - 'aws s3 cp s3://%s/%s/ s3://%s/%s/ --region %s --recursive', |
170 | | - escapeshellarg($bucket), |
171 | | - escapeshellarg($fromDir), |
172 | | - escapeshellarg($bucket), |
173 | | - escapeshellarg($toDir), |
174 | | - escapeshellarg($region) |
175 | | - ); |
176 | | - |
177 | | - $copyOutput = []; |
178 | | - $copyReturnCode = 0; |
179 | | - exec($copyCommand.' 2>&1', $copyOutput, $copyReturnCode); |
180 | | - |
181 | | - if ($copyReturnCode !== 0) { |
182 | | - $this->error(' Failed to copy files: '.implode("\n", $copyOutput)); |
183 | | - |
184 | | - return; |
185 | | - } |
186 | | - |
187 | | - // Verify copy operation by listing destination directory |
188 | | - $verifyOutput = []; |
189 | | - $verifyReturnCode = 0; |
190 | | - exec($destListCommand.' 2>/dev/null', $verifyOutput, $verifyReturnCode); |
191 | | - |
192 | | - $actualFinalCount = count($verifyOutput); |
193 | | - |
194 | | - if ($actualFinalCount !== $expectedFinalCount) { |
195 | | - $this->error(" Verification failed: Expected {$expectedFinalCount} files, found {$actualFinalCount}"); |
196 | | - |
197 | | - return; |
198 | | - } |
199 | | - } |
200 | | - |
201 | | - /** |
202 | | - * Update database paths for moved files |
203 | | - */ |
204 | | - protected function updateDatabasePaths(): void |
205 | | - { |
206 | | - // Update ProjectAsset paths from project-resources/downloads to project-assets |
207 | | - $oldProjectPath = config('config.uploads.project_resources_downloads'); |
208 | | - $newProjectPath = config('config.uploads.project-assets'); |
209 | | - |
210 | | - $projectAssetUpdates = DB::table('project_assets') |
211 | | - ->where('download_path', 'LIKE', $oldProjectPath.'/%') |
212 | | - ->get(); |
213 | | - |
214 | | - if ($projectAssetUpdates->isNotEmpty()) { |
215 | | - foreach ($projectAssetUpdates as $asset) { |
216 | | - $newPath = str_replace($oldProjectPath, $newProjectPath, $asset->download_path); |
217 | | - |
218 | | - DB::table('project_assets') |
219 | | - ->where('id', $asset->id) |
220 | | - ->update(['download_path' => $newPath]); |
221 | | - } |
222 | | - } |
223 | | - |
224 | | - // Update SiteAsset paths from resources to site-assets |
225 | | - $oldSitePath = config('config.uploads.resources'); |
226 | | - $newSitePath = config('config.uploads.site-assets'); |
227 | | - |
228 | | - $siteAssetUpdates = DB::table('site_assets') |
229 | | - ->where('download_path', 'LIKE', $oldSitePath.'/%') |
230 | | - ->get(); |
231 | | - |
232 | | - if ($siteAssetUpdates->isNotEmpty()) { |
233 | | - foreach ($siteAssetUpdates as $asset) { |
234 | | - $newPath = str_replace($oldSitePath, $newSitePath, $asset->download_path); |
235 | | - |
236 | | - DB::table('site_assets') |
237 | | - ->where('id', $asset->id) |
238 | | - ->update(['download_path' => $newPath]); |
239 | | - } |
240 | | - } |
241 | | - } |
242 | | - |
243 | | - public function processClassifications(): void |
244 | | - { |
245 | | - DB::table('pusher_classifications')->orderBy('id')->chunkById(100, function ($chunk) { |
246 | | - $ids = []; |
247 | | - foreach ($chunk as $row) { |
248 | | - $data = json_decode($row->data, true); |
249 | | - if ($data && is_array($data)) { |
250 | | - PusherTranscriptionJob::dispatch($data); |
251 | | - $ids[] = $row->id; |
252 | | - } |
253 | | - } |
254 | | - if ($ids) { |
255 | | - DB::table('pusher_classifications')->whereIn('id', $ids)->delete(); |
256 | | - } |
257 | | - }, 'id'); |
258 | | - } |
| 51 | + public function handle() {} |
259 | 52 | } |
0 commit comments