Signed-off-by: Julien Veyssier <eneiluj@posteo.net>
This commit is contained in:
Julien Veyssier 2021-12-28 13:40:52 +01:00
Родитель 2f23e2d18d
Коммит cde5784141
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4141FEE162030638
2 изменённых файлов: 17 добавлений и 24 удалений

Просмотреть файл

@ -98,11 +98,9 @@ class ImageController extends Controller {
try {
$insertResult = $this->imageService->insertImageFile($documentId, $imagePath, $userId);
if (isset($insertResult['error'])) {
return new DataResponse($insertResult, Http::STATUS_BAD_REQUEST);
} else {
return new DataResponse($insertResult);
}
return isset($insertResult['error'])
? new DataResponse($insertResult, Http::STATUS_BAD_REQUEST)
: new DataResponse($insertResult);
} catch (Exception $e) {
$this->logger->error('File insertion error', ['exception' => $e]);
return new DataResponse(['error' => 'File insertion error'], Http::STATUS_BAD_REQUEST);
@ -133,11 +131,9 @@ class ImageController extends Controller {
$userId = $session->getUserId();
$downloadResult = $this->imageService->insertImageLink($documentId, $link, $userId);
}
if (isset($downloadResult['error'])) {
return new DataResponse($downloadResult, Http::STATUS_BAD_REQUEST);
} else {
return new DataResponse($downloadResult);
}
return isset($downloadResult['error'])
? new DataResponse($downloadResult, Http::STATUS_BAD_REQUEST)
: new DataResponse($downloadResult);
} catch (Exception $e) {
$this->logger->error('Link insertion error', ['exception' => $e]);
return new DataResponse(['error' => 'Link insertion error'], Http::STATUS_BAD_REQUEST);
@ -162,7 +158,7 @@ class ImageController extends Controller {
try {
$file = $this->request->getUploadedFile('image');
if ($file !== null && isset($file['tmp_name'], $file['name'], $file['type'])) {
if (!in_array($file['type'], self::IMAGE_MIME_TYPES)) {
if (!in_array($file['type'], self::IMAGE_MIME_TYPES, true)) {
return new DataResponse(['error' => 'Image type not supported'], Http::STATUS_BAD_REQUEST);
}
$newFileContent = file_get_contents($file['tmp_name']);
@ -179,9 +175,8 @@ class ImageController extends Controller {
} else {
return new DataResponse($uploadResult);
}
} else {
return new DataResponse(['error' => 'No uploaded file'], Http::STATUS_BAD_REQUEST);
}
return new DataResponse(['error' => 'No uploaded file'], Http::STATUS_BAD_REQUEST);
} catch (Exception $e) {
$this->logger->error('Upload error', ['exception' => $e]);
return new DataResponse(['error' => 'Upload error'], Http::STATUS_BAD_REQUEST);
@ -212,11 +207,9 @@ class ImageController extends Controller {
} else {
$imageFile = $this->imageService->getImage($documentId, $imageFileName, $this->userId);
}
if ($imageFile !== null) {
return new DataDisplayResponse($imageFile->getContent(), Http::STATUS_OK, ['Content-Type' => $imageFile->getMimeType()]);
} else {
return new DataDisplayResponse('', Http::STATUS_NOT_FOUND);
}
return $imageFile !== null
? new DataDisplayResponse($imageFile->getContent(), Http::STATUS_OK, ['Content-Type' => $imageFile->getMimeType()])
: new DataDisplayResponse('', Http::STATUS_NOT_FOUND);
} catch (Exception $e) {
$this->logger->error('getImage error', ['exception' => $e]);
return new DataDisplayResponse('', Http::STATUS_NOT_FOUND);

Просмотреть файл

@ -240,7 +240,7 @@ class ImageService {
*/
private function copyImageFile(File $imageFile, Folder $saveDir, File $textFile): array {
$mimeType = $imageFile->getMimeType();
if (in_array($mimeType, ImageController::IMAGE_MIME_TYPES)) {
if (in_array($mimeType, ImageController::IMAGE_MIME_TYPES, true)) {
$fileName = (string) time() . '-' . $imageFile->getName();
$targetPath = $saveDir->getPath() . '/' . $fileName;
$targetFile = $imageFile->copy($targetPath);
@ -342,16 +342,16 @@ class ImageService {
}
$savedFile->touch();
if (isset($res['Content-Type'])) {
if (in_array($res['Content-Type'], ImageController::IMAGE_MIME_TYPES)) {
if (in_array($res['Content-Type'], ImageController::IMAGE_MIME_TYPES, true)) {
if ($res['Content-Type'] === 'image/jpeg') {
$fileName = $fileName . '.jpg';
$fileName .= '.jpg';
} elseif ($res['Content-Type'] === 'image/x-xbitmap' || $res['Content-Type'] === 'image/x-ms-bmp') {
$fileName = $fileName . '.bmp';
$fileName .= '.bmp';
} elseif ($res['Content-Type'] === 'image/svg+xml') {
$fileName = $fileName . '.svg';
$fileName .= '.svg';
} else {
$ext = preg_replace('/^image\//i', '', $res['Content-Type']);
$fileName = $fileName . '.' . $ext;
$fileName .= '.' . $ext;
}
$targetPath = $saveDir->getPath() . '/' . $fileName;
$savedFile->move($targetPath);