-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThumbAction.php
67 lines (56 loc) · 1.8 KB
/
ThumbAction.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
namespace makroxyz\fileupload;
use Yii;
class ThumbAction extends BaseAction
{
/**
* @var string temporary upload destination
*/
public $uploadDest;
/**
* @var string thumbnail destination
*/
public $thumbDest;
/**
* @var boolean whether to use thumbs directory
*/
public $useThumbs = true;
protected function getFileType($file_path)
{
switch (strtolower(pathinfo($file_path, PATHINFO_EXTENSION))) {
case 'jpeg':
case 'jpg':
return 'image/jpeg';
case 'png':
return 'image/png';
case 'gif':
return 'image/gif';
default:
return 'application/octet-stream';
}
}
public function run($filename)
{
if ($this->uploadDest === null) {
$this->uploadDest = \Yii::$app->runtimePath . DIRECTORY_SEPARATOR . 'temp';
} else {
$this->uploadDest = \Yii::getAlias($this->uploadDest);
}
if ($this->thumbDest === null) {
$this->thumbDest = $this->uploadDest . DIRECTORY_SEPARATOR . 'thumb';
} else {
$this->thumbDest = \Yii::getAlias($this->thumbDest);
}
$filename = $this->cleanFilename($filename);
$path = ($this->useThumbs ? $this->thumbDest : $this->uploadDest) . DIRECTORY_SEPARATOR . $filename;
if (!file_exists($path)) {
throw new \yii\web\NotFoundHttpException();
}
// header('X-Content-Type-Options: nosniff');
// header('Content-Type: '.$this->getFileType($path));
Yii::$app->getResponse()->getHeaders()
->set('X-Content-Type-Options', 'nosniff')
->set('Content-Type', $this->getFileType($path));
readfile($path);
}
}