|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace floor12\files\actions; |
|
4
|
|
|
|
|
5
|
|
|
use floor12\files\components\SimpleImage; |
|
6
|
|
|
use floor12\files\models\File; |
|
7
|
|
|
use floor12\files\models\FileType; |
|
8
|
|
|
use Yii; |
|
9
|
|
|
use yii\web\NotFoundHttpException; |
|
10
|
|
|
|
|
11
|
|
|
class GetFileAction extends \yii\base\Action |
|
12
|
|
|
{ |
|
13
|
|
|
public function run($hash) |
|
14
|
|
|
{ |
|
15
|
|
|
$model = File::findOne(['hash' => $hash]); |
|
16
|
|
|
|
|
17
|
|
|
if (!$model) |
|
|
|
|
|
|
18
|
|
|
throw new NotFoundHttpException("Запрашиваемый файл не найден"); |
|
19
|
|
|
|
|
20
|
|
|
if (!file_exists($model->rootPath)) |
|
21
|
|
|
throw new NotFoundHttpException('Запрашиваемый файл не найден на диске.'); |
|
22
|
|
|
|
|
23
|
|
|
Yii::$app->response->headers->set('Last-Modified', date("c", $model->created)); |
|
24
|
|
|
Yii::$app->response->headers->set('Cache-Control', 'public, max-age=' . (60 * 60 * 24 * 15)); |
|
25
|
|
|
|
|
26
|
|
|
if ($model->type == FileType::IMAGE && $model->watermark) { |
|
27
|
|
|
$image = new SimpleImage(); |
|
28
|
|
|
$image->load($model->rootPath); |
|
29
|
|
|
$image->watermark(Yii::getAlias("@frontend/web/design/logo-big.png")); |
|
30
|
|
|
$tmpName = Yii::getAlias("@runtime/" . md5(time() . $model->id)); |
|
31
|
|
|
$image->save($tmpName, IMAGETYPE_JPEG); |
|
32
|
|
|
$stream = fopen($tmpName, 'rb'); |
|
33
|
|
|
Yii::$app->response->sendStreamAsFile($stream, $model->title, ['inline' => true, 'mimeType' => "image/jpeg", 'filesize' => $model->size]); |
|
|
|
|
|
|
34
|
|
|
@unlink($tmpName); |
|
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
} else { |
|
37
|
|
|
$stream = fopen($model->rootPath, 'rb'); |
|
38
|
|
|
Yii::$app->response->sendStreamAsFile($stream, $model->title, ['inline' => true, 'mimeType' => $model->content_type, 'filesize' => $model->size]); |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|