new: [setting] MISP.thumbnail_in_redis

pull/8402/head
Jakub Onderka 2022-05-23 21:34:07 +02:00
parent 07f128272f
commit a0778774d7
2 changed files with 42 additions and 19 deletions

View File

@ -878,9 +878,9 @@ class Attribute extends AppModel
return in_array($type, self::ZIPPED_DEFINITION, true) || in_array($type, self::UPLOAD_DEFINITIONS, true);
}
public function getAttachment($attribute, $path_suffix='')
public function getAttachment($attribute)
{
return $this->loadAttachmentTool()->getContent($attribute['event_id'], $attribute['id'], $path_suffix);
return $this->loadAttachmentTool()->getContent($attribute['event_id'], $attribute['id']);
}
/**
@ -889,27 +889,30 @@ class Attribute extends AppModel
* @return File
* @throws Exception
*/
public function getAttachmentFile(array $attribute, $path_suffix='')
public function getAttachmentFile(array $attribute)
{
return $this->loadAttachmentTool()->getFile($attribute['event_id'], $attribute['id'], $path_suffix);
return $this->loadAttachmentTool()->getFile($attribute['event_id'], $attribute['id']);
}
/**
* @param array $attribute
* @param string $path_suffix
* @param bool $skipAvScan
* @return bool
* @throws Exception
*/
private function saveAttachment(array $attribute, $path_suffix='', $skipAvScan = false)
private function saveAttachment(array $attribute)
{
if ($attribute['data'] === false) {
$this->log("Invalid attachment data provided for attribute with ID {$attribute['id']}.");
return false;
}
$result = $this->loadAttachmentTool()->save($attribute['event_id'], $attribute['id'], $attribute['data'], $path_suffix);
if ($result && !$skipAvScan) {
$result = $this->loadAttachmentTool()->save($attribute['event_id'], $attribute['id'], $attribute['data']);
if ($result) {
$this->loadAttachmentScan()->backgroundScan(AttachmentScan::TYPE_ATTRIBUTE, $attribute);
// Clean thumbnail cache
if ($this->isImage($attribute) && Configure::read('MISP.thumbnail_in_redis')) {
$redis = $this->setupRedisWithException();
$redis->del($redis->keys("misp:thumbnail:attribute:{$attribute['id']}:*"));
}
}
return $result;
}
@ -953,9 +956,9 @@ class Attribute extends AppModel
/**
* @param array $attribute
* @param string $outputFormat Can be 'png' or 'webp'
* @param int|null $maxWidth
* @param int|null $maxHeight
* @param string $outputFormat Can be 'png' or 'webp'
* @return string|File
* @throws Exception
*/
@ -965,18 +968,26 @@ class Attribute extends AppModel
return $this->getPictureData($attribute);
}
// Use two times bigget thumbnail for webp to generate hires preview image
// Use two times bigger thumbnail for webp to generate hires preview image
$defaultMaxSize = $outputFormat === 'webp' ? 400 : 200;
$maxWidth = $maxWidth ?: $defaultMaxSize;
$maxHeight = $maxHeight ?: $defaultMaxSize;
if ($maxWidth == $defaultMaxSize && $maxHeight == $defaultMaxSize) {
$suffix = $outputFormat === 'png' ? '_thumbnail' : '_thumbnail_' . $outputFormat;
// Return thumbnail directly if already exists
try {
return $this->loadAttachmentTool()->getFile($attribute['Attribute']['event_id'], $attribute['Attribute']['id'], $suffix);
} catch (NotFoundException $e) {
// pass
$thumbnailInRedis = Configure::read('MISP.thumbnail_in_redis');
if ($thumbnailInRedis) {
$redis = $this->setupRedisWithException();
if ($data = $redis->get("misp:thumbnail:attribute:{$attribute['Attribute']['id']}:$outputFormat")) {
return $data;
}
} else {
$suffix = $outputFormat === 'png' ? '_thumbnail' : '_thumbnail_' . $outputFormat;
// Return thumbnail directly if already exists
try {
return $this->loadAttachmentTool()->getFile($attribute['Attribute']['event_id'], $attribute['Attribute']['id'], $suffix);
} catch (NotFoundException $e) {
// pass
}
}
}
@ -986,8 +997,11 @@ class Attribute extends AppModel
// Save just when requested default thumbnail size
if ($maxWidth == $defaultMaxSize && $maxHeight == $defaultMaxSize) {
$attribute['Attribute']['data'] = $imageData;
$this->saveAttachment($attribute['Attribute'], $suffix, true);
if ($thumbnailInRedis) {
$redis->set("misp:thumbnail:attribute:{$attribute['Attribute']['id']}:$outputFormat", $imageData, 3600);
} else {
$this->loadAttachmentTool()->save($attribute['Attribute']['event_id'], $attribute['Attribute']['id'], $imageData, $suffix);
}
}
return $imageData;
}
@ -995,6 +1009,7 @@ class Attribute extends AppModel
/**
* @param array $user
* @param array $resultArray
* @throws Exception
*/
public function fetchRelated(array $user, array &$resultArray)
{

View File

@ -5710,6 +5710,14 @@ class Server extends AppModel
'type' => 'boolean',
'null' => true,
],
'thumbnail_in_redis' => [
'level' => self::SETTING_OPTIONAL,
'description' => __('Store image thumbnails in Redis insteadof file system.'),
'value' => false,
'test' => 'testBool',
'type' => 'boolean',
'null' => true,
],
),
'GnuPG' => array(
'branch' => 1,