Soshot/app/Soshot/GetThumb.php

164 lines
6.1 KiB
PHP
Raw Permalink Normal View History

2023-06-21 11:42:36 +02:00
<?php
namespace App\Soshot;
use App\DataBase\DataBase;
use App\Utils\Hmac;
use App\Utils\Error;
use App\Utils\ResizeToDemande;
class GetThumb {
protected $params;
protected $type;
protected $filePath;
protected $receiveHmac;
protected $requestImg;
protected $queuePath = __DIR__ . '/../../cache/queue/';
protected $complete;
protected $conf;
protected $db;
protected $demande;
2023-06-28 12:29:20 +02:00
protected $fileFormat;
2023-06-21 11:42:36 +02:00
/**
* Constructs a new object.
*
* This method initializes the object properties based on the provided parameters
* and configuration. It first checks if the provided HMAC is valid using the
* Hmac class. If the HMAC is invalid, it returns an error message. It then sets
* the object properties for the request URL, HMAC, type, file format, file path,
* request image path, complete image path, and database connection.
*
* @param object $params The request parameters object containing the URL, HMAC, and type.
* @param object $conf The configuration object containing the file format and key.
*
* @return GetOg The constructed GetOg object.
*/
function __construct(object $params, object $conf) {
2023-06-21 11:42:36 +02:00
$hmac = new Hmac($conf->key);
$this->conf = $conf;
$this->receiveHmac = $params->hmac;
if ($hmac->checkHmac($this->receiveHmac, $params->url) === false) {
$message = (object) ['status' => 404, 'message' => 'Wrong Hmac'];
$error = new Error();
$error->index($message);
}
$this->params = $params;
$this->type = $params->type;
2023-06-28 12:29:20 +02:00
$this->fileFormat = $this->conf->fileFormat;
2023-06-21 11:42:36 +02:00
$this->filePath = $hmac->makeFilePath($this->receiveHmac);
if ($this->type === 'pdf') {
$this->requestImg = $this->filePath . $this->receiveHmac . '_' . $this->type . '.pdf';
} else {
$this->requestImg = $this->filePath . $this->receiveHmac . '_' . $this->type . '.' . $this->fileFormat;
2023-06-21 11:42:36 +02:00
}
$this->complete = __DIR__ . '/../../cache/img/' . substr($this->params->hmac, 0, 4) . '/' . $this->params->hmac . '_complete.' . $this->fileFormat;
2023-06-21 11:42:36 +02:00
$this->demande = (object)[
'url' => $this->params->url,
'hmac' => $this->params->hmac,
'filePath' => $this->filePath,
'requestImg' => $this->requestImg,
'type' => $this->params->type
];
$this->db = new DataBase($this->demande);
return $this;
}
/**
* Display the requested image or PDF.
*
* This method checks if the requested file exists and serves it with the appropriate
* headers. If the requested file does not exist, it checks if a complete image exists
* and resizes it to the requested type. If neither the requested file nor a complete
* image exists, it adds a job to the queue to generate the requested image and serves
* a placeholder image.
*
* @return void
*/
2023-06-21 11:42:36 +02:00
public function show() {
if ($this->type === 'pdf' && file_exists($this->requestImg)) {
header("Content-type:application/pdf");
header('Expires: ', gmdate('D, d M Y H:i:s', time()) . ' GMT');
header("Content-Disposition:attachment;filename=\"archive.pdf\"");
readfile($this->requestImg);
echo file_get_contents($this->requestImg);
exit();
} else if (file_exists($this->requestImg) && in_array($this->type, ['complete', 'full', 'hd', 'nhd', 'thumb'])) {
2023-06-28 12:29:20 +02:00
header("Content-type: image/$this->fileFormat");
2023-06-21 11:42:36 +02:00
header('Expires: ', gmdate('D, d M Y H:i:s', time()) . ' GMT');
echo file_get_contents($this->requestImg);
exit();
}
if (file_exists($this->complete) && in_array($this->type, ['complete', 'full', 'hd', 'nhd', 'thumb'])) {
2023-06-21 11:42:36 +02:00
if (ResizeToDemande::makeDemande((object)[
'complete' => $this->complete,
'filePath' => $this->requestImg,
'type' => $this->type
])) {
$this->db->addUpdate(1, $this->type);
2023-06-28 12:29:20 +02:00
header("Content-type: image/$this->fileFormat");
2023-06-21 11:42:36 +02:00
header('Expires: ', gmdate('D, d M Y H:i:s', time()) . ' GMT');
echo file_get_contents($this->requestImg);
exit();
} else {
// @todo log
}
exit();
} else {
$json = json_encode([
'type' => $this->params->type,
'hmac' => $this->params->hmac,
'url' => $this->params->url,
'filePath' => $this->requestImg,
'complete' => __DIR__ . '/../../cache/img/' . substr($this->params->hmac, 0, 4) . '/' . $this->params->hmac . '_complete.' . $this->fileFormat
2023-06-21 11:42:36 +02:00
]);
$completeDemande = (object)[
'url' => $this->params->url,
'hmac' => $this->params->hmac,
'filePath' => $this->filePath,
'requestImg' => $this->requestImg,
'type' => 'complete'
];
$complete = new DataBase($completeDemande);
$complete->addUpdate(3, $this->type);
2023-06-21 11:42:36 +02:00
if ($this->conf->alwaysMakePdf === true) {
$complete->addUpdate(3, 'pdf');
}
$this->addQueue($json);
header("Content-type: image/png");
header('Expires: ', gmdate('D, d M Y H:i:s', time()) . ' GMT');
if ($this->type === 'fav') {
echo file_get_contents(__DIR__ . '/../../src/images/error_fav.png');
} else {
echo file_get_contents(__DIR__ . '/../../src/images/' . $this->type . '_generation_in_progress.jpg');
2023-06-21 11:42:36 +02:00
}
exit();
}
}
/**
* Add a JSON object to the queue file.
*
* @param string $json The JSON object to add to the queue file.
*
* @return void
*/
2023-06-21 11:42:36 +02:00
private function addQueue($json) {
$file = $this->queuePath . $this->receiveHmac . '.json';
$this->db->addUpdate(3, $this->type);
2023-06-21 11:42:36 +02:00
if (!file_exists($file)) {
file_put_contents($file, $json);
}
}
}