Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 41 additions & 14 deletions src/fs/CachedFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@
*
**/


namespace EdSDK\FlmngrServer\fs;

use EdSDK\FlmngrServer\model\Message;
use EdSDK\FlmngrServer\lib\file\blurHash\Blurhash;
use EdSDK\FlmngrServer\lib\file\Utils;
use EdSDK\FlmngrServer\lib\MessageException;

class CachedFile {
class CachedFile
{

private $fileRelative;

Expand Down Expand Up @@ -57,7 +59,8 @@ function __construct(
}

// Clears cache for this file
function delete() {
function delete()
{
if ($this->driverCache->exists($this->cacheFileJsonRelative)) {
$this->driverCache->delete($this->cacheFileJsonRelative);
}
Expand All @@ -66,7 +69,8 @@ function delete() {
}
}

function getInfo() {
function getInfo()
{
if (!$this->driverCache->exists($this->cacheFileJsonRelative)) {

try {
Expand All @@ -79,7 +83,6 @@ function getInfo() {
'size' => $this->driverFiles->size($this->fileRelative),
];
$this->writeInfo($info);

} catch (Exception $e) {
error_log("Exception while getting image size of " . $this->fileRelative);
error_log($e);
Expand All @@ -96,15 +99,20 @@ function getInfo() {
return $json;
}

private function writeInfo($info) {
private function writeInfo($info)
{
$dirname = dirname($this->cacheFileJsonRelative);
if (!$this->driverCache->exists($dirname)) {
$this->driverCache->makeDirectory($dirname);
}
$this->driverCache->put($this->cacheFileJsonRelative, json_encode($info));
}

function getPreview($preview_width, $preview_height, $contents) {
/**
* override-core: #86c19vjg1 support du Webp
*/
function getPreview($preview_width, $preview_height, $contents)
{
$cacheFilePreviewRelative = $this->cacheFileRelative . '.png';

if ($this->driverCache->exists($cacheFilePreviewRelative)) {
Expand All @@ -123,19 +131,41 @@ function getPreview($preview_width, $preview_height, $contents) {
if (!$this->driverCache->exists($cacheFilePreviewRelative)) {

if (Utils::getMimeType($this->fileRelative) === 'image/svg+xml') {
return ['image/svg+xml', $this->fileRelative, FALSE]; // FALSE means from files folder
return ['image/svg+xml', $this->fileRelative, FALSE]; // FALSE means from files folder
}

if ($contents === NULL) {
$contents = $this->driverFiles->get($this->fileRelative);
}
$image = imagecreatefromstring($contents);

// Check if the file is a WebP image
$isWebP = Utils::getMimeType($this->fileRelative) === 'image/webp';

if ($isWebP) {
// For WebP files, we need to use GD's dedicated WebP function
if (function_exists('imagecreatefromwebp')) {
// Create a temporary file to handle WebP
$tmpFile = tempnam(sys_get_temp_dir(), 'webp_');
file_put_contents($tmpFile, $contents);
$image = imagecreatefromwebp($tmpFile);
unlink($tmpFile); // Clean up
} else {
throw new MessageException(
Message::createMessage(FALSE, Message::IMAGE_PROCESS_ERROR . ' (WebP not supported in this PHP version)')
);
}
} else {
// For other image types, use imagecreatefromstring as before
$image = imagecreatefromstring($contents);
}

if (!$image) {
throw new MessageException(
Message::createMessage(FALSE,Message::IMAGE_PROCESS_ERROR)
Message::createMessage(FALSE, Message::IMAGE_PROCESS_ERROR)
);
}

// Rest of the image processing code remains the same
$orientation = $this->driverFiles->getExifOrientation($this->fileRelative);
if ($orientation === 3) {
$image = imagerotate($image, 180, 0);
Expand All @@ -157,8 +187,7 @@ function getPreview($preview_width, $preview_height, $contents) {

if ($preview_width == NULL) {
$preview_width = max(1, floor($original_ratio * $preview_height));
}
else {
} else {
if ($preview_height == NULL) {
$preview_height = max(1, floor((1 / $original_ratio) * $preview_width));
}
Expand All @@ -168,8 +197,7 @@ function getPreview($preview_width, $preview_height, $contents) {

if ($original_ratio >= $preview_ratio) {
$preview_height = max(1, floor($original_height * $preview_width / $original_width));
}
else {
} else {
$preview_width = max(1, floor($original_width * $preview_height / $original_height));
}

Expand Down Expand Up @@ -249,5 +277,4 @@ function getPreview($preview_width, $preview_height, $contents) {

return ['image/png', $cacheFilePreviewRelative, TRUE]; // TRUE means from cache folder
}

}