163 lines
6.1 KiB
PHP
163 lines
6.1 KiB
PHP
<?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;
|
|
protected $fileFormat;
|
|
|
|
/**
|
|
* 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) {
|
|
$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;
|
|
$this->fileFormat = $this->conf->fileFormat;
|
|
$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;
|
|
}
|
|
$this->complete = __DIR__ . '/../../cache/img/' . substr($this->params->hmac, 0, 4) . '/' . $this->params->hmac . '_complete.' . $this->fileFormat;
|
|
|
|
$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
|
|
*/
|
|
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'])) {
|
|
header("Content-type: image/$this->fileFormat");
|
|
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'])) {
|
|
if (ResizeToDemande::makeDemande((object)[
|
|
'complete' => $this->complete,
|
|
'filePath' => $this->requestImg,
|
|
'type' => $this->type
|
|
])) {
|
|
$this->db->addUpdate(1, $this->type);
|
|
header("Content-type: image/$this->fileFormat");
|
|
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
|
|
]);
|
|
|
|
$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);
|
|
|
|
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');
|
|
}
|
|
exit();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Add a JSON object to the queue file.
|
|
*
|
|
* @param string $json The JSON object to add to the queue file.
|
|
*
|
|
* @return void
|
|
*/
|
|
private function addQueue($json) {
|
|
$file = $this->queuePath . $this->receiveHmac . '.json';
|
|
$this->db->addUpdate(3, $this->type);
|
|
if (!file_exists($file)) {
|
|
file_put_contents($file, $json);
|
|
}
|
|
}
|
|
}
|