aboutsummaryrefslogtreecommitdiff
path: root/db/migrations/6.1.16_import_default_stock_images.php
diff options
context:
space:
mode:
Diffstat (limited to 'db/migrations/6.1.16_import_default_stock_images.php')
-rw-r--r--db/migrations/6.1.16_import_default_stock_images.php128
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)