diff options
| author | Thomas Hackl <hackl@data-quest.de> | 2025-08-06 12:19:07 +0200 |
|---|---|---|
| committer | Thomas Hackl <hackl@data-quest.de> | 2025-08-06 12:19:07 +0200 |
| commit | bd98b97ad60e4a8e899be11127eab5b9bae8a5cc (patch) | |
| tree | 56dc34c0b40e39ee27ce4305ef37cbc6c5919026 /db/migrations | |
| parent | 68b830e10fbd203c2b97e918634586ad07c32a59 (diff) | |
some error handling on unavailable ZIP fileissue-5788
Diffstat (limited to 'db/migrations')
| -rw-r--r-- | db/migrations/6.1.16_import_default_stock_images.php | 128 |
1 files changed, 67 insertions, 61 deletions
diff --git a/db/migrations/6.1.16_import_default_stock_images.php b/db/migrations/6.1.16_import_default_stock_images.php index 86debfc..3bf3633 100644 --- a/db/migrations/6.1.16_import_default_stock_images.php +++ b/db/migrations/6.1.16_import_default_stock_images.php @@ -14,75 +14,81 @@ class ImportDefaultStockImages extends Migration $extractPath = $GLOBALS['TMP_PATH'] . '/pool/'; // Fetch zip with pool images. - file_put_contents( - $zipFile, - file_get_contents('https://gitlab.studip.de/studip/bilderpool/-/raw/main/Stud.IP_5_Bilderpool.zip?ref_type=heads&inline=false') - ); - - // Unzip archive to temp directory. - $zip = new ZipArchive(); - $res = $zip->open($zipFile); - if ($res) { - $zip->extractTo($extractPath); - $zip->close(); - } else { - unlink($zipFile); - die('Could not open zip file.'); - } + $content = @file_get_contents('https://gitlab.studip.de/studip/bilderpool/-/raw/main/Stud.IP_5_Bilderpool.zip?ref_type=heads&inline=false'); - // Get metadata and create stock images if not already present. - $csv_file = file($extractPath . 'meta.csv'); - if (!$csv_file) { - $this->cleanup($extractPath); - unlink($zipFile); - die('No meta.csv file provided.'); - } + if ($content !== false) { - $rows = array_map( - fn($v) => str_getcsv($v, ';'), - $csv_file - ); - $header = array_shift($rows); + file_put_contents($zipFile, $content); - $images = []; - foreach ($rows as $row) { - $images[] = array_combine($header, $row); - } + // Unzip archive to temp directory. + $zip = new ZipArchive(); + $res = $zip->open($zipFile); + if ($res) { + $zip->extractTo($extractPath); + $zip->close(); + } else { + unlink($zipFile); + die('Could not open zip file.'); + } - foreach ($images as $meta) { - $filename = $meta['filename']; - if (!$filename) { - continue; + // Get metadata and create stock images if not already present. + $csv_file = file($extractPath . 'meta.csv'); + if (!$csv_file) { + $this->cleanup($extractPath); + unlink($zipFile); + die('No meta.csv file provided.'); } - // Import file only if it doesn't already exist - if (!StockImage::findOneByDescription($meta['description'] ?? 'STOCKIMAGE')) { - $filepath = $extractPath . $filename; - $filesize = filesize($filepath); - $imagesize = getimagesize($filepath); - - $image = \StockImage::create([ - 'title' => $meta['title'] ?? 'unknown', - 'description' => $meta['description'] ?? '', - 'license' => $meta['license'] ?? '', - 'author' => $meta['author'] ?? '', - 'height' => $imagesize[1], - 'width' => $imagesize[0], - 'mime_type' => $imagesize['mime'], - 'size' => $filesize, - 'tags' => json_encode(explode(',', $meta['tags'])), - ]); - - copy($filepath, $image->getPath()); - $scaler = new \Studip\StockImages\Scaler(); - $scaler($image); - $paletteCreator = new \Studip\StockImages\PaletteCreator(); - $paletteCreator($image); + $rows = array_map( + fn($v) => str_getcsv($v, ';'), + $csv_file + ); + $header = array_shift($rows); + + $images = []; + foreach ($rows as $row) { + $images[] = array_combine($header, $row); } - } - $this->cleanup($extractPath); - unlink($zipFile); + foreach ($images as $meta) { + $filename = $meta['filename']; + if (!$filename) { + continue; + } + + // Import file only if it doesn't already exist + if (!StockImage::findOneByDescription($meta['description'] ?? 'STOCKIMAGE')) { + $filepath = $extractPath . $filename; + $filesize = filesize($filepath); + $imagesize = getimagesize($filepath); + + $image = \StockImage::create([ + 'title' => $meta['title'] ?? 'unknown', + 'description' => $meta['description'] ?? '', + 'license' => $meta['license'] ?? '', + 'author' => $meta['author'] ?? '', + 'height' => $imagesize[1], + 'width' => $imagesize[0], + 'mime_type' => $imagesize['mime'], + 'size' => $filesize, + 'tags' => json_encode(explode(',', $meta['tags'])), + ]); + + copy($filepath, $image->getPath()); + $scaler = new \Studip\StockImages\Scaler(); + $scaler($image); + $paletteCreator = new \Studip\StockImages\PaletteCreator(); + $paletteCreator($image); + } + } + + $this->cleanup($extractPath); + unlink($zipFile); + + // No ZIP file found or file not accessible, just show an error message. + } else { + echo 'Could not open zip file.'; + } } private function cleanup($path) |
