1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
<?php
namespace JsonApi\Routes\StockImages;
use JsonApi\Errors\AuthorizationFailedException;
use JsonApi\Errors\BadRequestException;
use JsonApi\NonJsonApiController;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Nyholm\Psr7\UploadedFile;
use Studip\StockImages\Scaler;
use Studip\StockImages\PaletteCreator;
class StockImagesZipUpload extends NonJsonApiController
{
use UploadHelpers;
public function __invoke(Request $request, Response $response, $args): Response
{
if (!Authority::canUploadStockImage($this->getUser($request))) {
throw new AuthorizationFailedException();
}
$image_count = $this->handleUpload($request);
$response = $response->withHeader('Content-Type', 'application/json');
$response->getBody()->write(json_encode(['image-count' => $image_count]));
return $response;
}
private function handleUpload(Request $request): int
{
$uploadedFile = self::getUploadedFile($request);
if (UPLOAD_ERR_OK !== $uploadedFile->getError()) {
$error = self::getErrorString($uploadedFile->getError());
throw new BadRequestException($error);
}
$validateError = self::validate($uploadedFile);
if (!empty($validateError)) {
throw new BadRequestException($validateError);
}
$tmp_path = $GLOBALS['TMP_PATH'] . '/stock-images/';
if (!file_exists($tmp_path)) {
mkdir($tmp_path);
}
$zip_path = $tmp_path . 'archiv.zip';
$uploadedFile->moveTo($zip_path);
$zip = new \ZipArchive;
if ($zip->open($zip_path) === TRUE) {
$zip->extractTo($tmp_path);
$zip->close();
} else {
$this->cleanTmp($tmp_path);
throw new BadRequestException('Can not extract Zip file.');
}
$csv_file = file($tmp_path . 'meta.csv');
if (!$csv_file) {
$this->cleanTmp($tmp_path);
throw new BadRequestException('No meta.csv file provided.');
}
$rows = array_map(
fn($v) => str_getcsv($v, ';'),
$csv_file
);
$header = array_shift($rows);
$images = [];
foreach ($rows as $row) {
$images[] = array_combine($header, $row);
}
$image_counter = 0;
foreach ($images as $i => $meta) {
$filename = $meta['filename'];
if (!$filename) {
continue;
}
$filepath = $tmp_path . $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);
$image_counter++;
}
$this->cleanTmp($tmp_path);
return $image_counter;
}
private function cleanTmp(string $tmp_path): void
{
array_map('unlink', glob("$tmp_path/*.*"));
rmdir($tmp_path);
}
/**
* @return string|null null, if the file is valid, otherwise a string containing the error
*/
private function validate(UploadedFile $file)
{
$mimeType = $file->getClientMediaType();
if (!in_array($mimeType, ['application/x-zip-compressed', ' application/x-zip', 'application/zip'])) {
return 'Unsupported archive type.';
}
}
}
|