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
|
|
|
|
|
|
|
function __construct($params, $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;
|
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 {
|
2023-06-28 12:29:20 +02:00
|
|
|
$this->requestImg = $this->filePath . $this->receiveHmac . '_' . $this->type . '.'.$this->fileFormat;
|
2023-06-21 11:42:36 +02:00
|
|
|
}
|
2023-06-28 12:29:20 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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', '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', 'thumb'])) {
|
|
|
|
if (ResizeToDemande::makeDemande((object)[
|
|
|
|
'complete' => $this->complete,
|
|
|
|
'filePath' => $this->requestImg,
|
|
|
|
'type' => $this->type
|
|
|
|
])) {
|
|
|
|
$this->db->addUpdate(1);
|
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,
|
2023-06-28 12:29:20 +02:00
|
|
|
'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);
|
|
|
|
|
|
|
|
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 {
|
2023-06-29 11:57:38 +02:00
|
|
|
echo file_get_contents(__DIR__ . '/../../src/images/'.$this->type.'_generation_in_progress.jpg');
|
2023-06-21 11:42:36 +02:00
|
|
|
}
|
|
|
|
exit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function addQueue($json) {
|
|
|
|
$file = $this->queuePath . $this->receiveHmac . '.json';
|
|
|
|
$this->db->addUpdate(3);
|
|
|
|
if (!file_exists($file)) {
|
|
|
|
file_put_contents($file, $json);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|