|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* HiPanel core package |
|
5
|
|
|
* |
|
6
|
|
|
* @link https://hipanel.com/ |
|
7
|
|
|
* @package hipanel-core |
|
8
|
|
|
* @license BSD-3-Clause |
|
9
|
|
|
* @copyright Copyright (c) 2014-2016, HiQDev (http://hiqdev.com/) |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace hipanel\controllers; |
|
13
|
|
|
|
|
14
|
|
|
use hipanel\helpers\FileHelper; |
|
15
|
|
|
use Yii; |
|
16
|
|
|
use yii\filters\HttpCache; |
|
17
|
|
|
use yii\helpers\ArrayHelper; |
|
18
|
|
|
use yii\web\Controller; |
|
19
|
|
|
use yii\web\Response; |
|
20
|
|
|
|
|
21
|
|
|
class FileController extends Controller |
|
22
|
|
|
{ |
|
23
|
|
|
public function behaviors() |
|
24
|
|
|
{ |
|
25
|
|
|
return ArrayHelper::merge(parent::behaviors(), [ |
|
26
|
|
|
'file-cache' => [ |
|
27
|
|
|
'class' => HttpCache::class, |
|
28
|
|
|
'only' => ['view', 'get'], |
|
29
|
|
|
'lastModified' => function () { |
|
30
|
|
|
$id = Yii::$app->request->get('id'); |
|
31
|
|
|
$nocache = Yii::$app->request->get('nocache', false); |
|
32
|
|
|
if ($nocache || $id === null) { |
|
33
|
|
|
return null; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
$filename = Yii::$app->fileStorage->get($id); |
|
37
|
|
|
return filemtime($filename); |
|
38
|
|
|
} |
|
39
|
|
|
] |
|
40
|
|
|
]); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function actionView($id, $nocache = true) |
|
44
|
|
|
{ |
|
45
|
|
|
$filename = Yii::$app->fileStorage->get($id, $nocache); |
|
46
|
|
|
|
|
47
|
|
|
Yii::$app->response->format = Response::FORMAT_RAW; |
|
48
|
|
|
Yii::$app->response->headers->add('Content-type', FileHelper::getMimeType($filename)); |
|
49
|
|
|
|
|
50
|
|
|
return file_get_contents($filename); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function actionGet($id, $downloadName = null, $nocache = true) |
|
54
|
|
|
{ |
|
55
|
|
|
$filename = Yii::$app->fileStorage->get($id, $nocache); |
|
56
|
|
|
|
|
57
|
|
|
if ($downloadName === null) { |
|
58
|
|
|
$file = Yii::$app->fileStorage->getFileModel($id); |
|
59
|
|
|
$downloadName = $file->filename; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
Yii::$app->response->sendFile($filename, $downloadName)->send(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function actionTempView($filename, $key) |
|
66
|
|
|
{ |
|
67
|
|
|
$content = Yii::$app->fileStorage->getTemporary($filename, $key); |
|
68
|
|
|
|
|
69
|
|
|
Yii::$app->response->sendContentAsFile(file_get_contents($content), $filename)->send(); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|