|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace floor12\files\actions; |
|
4
|
|
|
|
|
5
|
|
|
use floor12\files\logic\ImagePreviewer; |
|
6
|
|
|
use floor12\files\models\File; |
|
7
|
|
|
use Yii; |
|
8
|
|
|
use yii\web\NotFoundHttpException; |
|
9
|
|
|
use yii\web\Response; |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
class GetPreviewAction extends \yii\base\Action |
|
13
|
|
|
{ |
|
14
|
|
|
const HEADER_CACHE_TIME = 60 * 60 * 24 * 15; |
|
15
|
|
|
|
|
16
|
|
|
/** @var int */ |
|
17
|
|
|
protected $width; |
|
18
|
|
|
/** @var File */ |
|
19
|
|
|
protected $model; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @param $hash |
|
23
|
|
|
* @param null $width |
|
|
|
|
|
|
24
|
|
|
* @param null $webp |
|
|
|
|
|
|
25
|
|
|
* @throws NotFoundHttpException |
|
26
|
|
|
* @throws \yii\base\InvalidConfigException |
|
27
|
|
|
* @throws \yii\web\RangeNotSatisfiableHttpException |
|
28
|
|
|
*/ |
|
29
|
|
|
public function run($hash, $width = null, $webp = null) |
|
|
|
|
|
|
30
|
|
|
{ |
|
31
|
|
|
|
|
32
|
|
|
$this->loadAndCheckModel($hash); |
|
33
|
|
|
$this->width = $width; |
|
34
|
|
|
|
|
35
|
|
|
if ($width) { |
|
|
|
|
|
|
36
|
|
|
$this->sendPreview($width); |
|
37
|
|
|
} else { |
|
38
|
|
|
$this->sendAsIs(); |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param string $hash |
|
44
|
|
|
* @throws NotFoundHttpException |
|
45
|
|
|
*/ |
|
46
|
|
|
private function loadAndCheckModel(string $hash): void |
|
47
|
|
|
{ |
|
48
|
|
|
$this->model = File::findOne(['hash' => $hash]); |
|
49
|
|
|
if (!$this->model) |
|
50
|
|
|
throw new NotFoundHttpException("Запрашиваемый файл не найден"); |
|
51
|
|
|
|
|
52
|
|
|
if (!$this->model->isImage() && !$this->model->isVideo()) |
|
53
|
|
|
throw new NotFoundHttpException("Запрашиваемый файл не является изображением"); |
|
54
|
|
|
|
|
55
|
|
|
if (!file_exists($this->model->rootPath)) |
|
56
|
|
|
throw new NotFoundHttpException('Запрашиваемый файл не найден на диске.'); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param $width |
|
61
|
|
|
* @throws NotFoundHttpException |
|
62
|
|
|
* @throws \yii\base\InvalidConfigException |
|
63
|
|
|
*/ |
|
64
|
|
|
protected function sendPreview($width) |
|
65
|
|
|
{ |
|
66
|
|
|
$filename = Yii::createObject(ImagePreviewer::class, [$this->model, $width, $webp])->getUrl(); |
|
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
if (!file_exists($filename)) |
|
69
|
|
|
throw new NotFoundHttpException('Запрашиваемый файл не найден на диске.'); |
|
70
|
|
|
|
|
71
|
|
|
$response = Yii::$app->response; |
|
|
|
|
|
|
72
|
|
|
$response->format = Response::FORMAT_RAW; |
|
73
|
|
|
$coontentType = mime_content_type($filename); |
|
74
|
|
|
$this->setHeaders($response, $coontentType, md5($this->model->created)); |
|
75
|
|
|
$stream = fopen($filename, 'rb'); |
|
76
|
|
|
Yii::$app->response->sendStreamAsFile($stream, $this->model->title, [ |
|
|
|
|
|
|
77
|
|
|
'inline' => true, |
|
78
|
|
|
'mimeType' => $this->model->content_type, |
|
79
|
|
|
'filesize' => $this->model->size |
|
80
|
|
|
]); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param string $coontentType |
|
85
|
|
|
* @param string $etag |
|
86
|
|
|
*/ |
|
87
|
|
|
private function setHeaders($response, string $coontentType, string $etag): void |
|
88
|
|
|
{ |
|
89
|
|
|
$response->headers->set('Last-Modified', date("c", $this->model->created)); |
|
90
|
|
|
$response->headers->set('Cache-Control', 'public, max-age=' . self::HEADER_CACHE_TIME); |
|
91
|
|
|
$response->headers->set('Content-Type', $coontentType . '; charset=utf-8'); |
|
92
|
|
|
$response->headers->set('ETag', $etag); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @throws \yii\web\RangeNotSatisfiableHttpException |
|
97
|
|
|
*/ |
|
98
|
|
|
protected function sendAsIs() |
|
99
|
|
|
{ |
|
100
|
|
|
$stream = fopen($this->model->rootPath, 'rb'); |
|
101
|
|
|
Yii::$app->response->sendStreamAsFile($stream, $this->model->title, [ |
|
|
|
|
|
|
102
|
|
|
'inline' => true, |
|
103
|
|
|
'mimeType' => $this->model->content_type, |
|
104
|
|
|
'filesize' => $this->model->size |
|
105
|
|
|
]); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|