1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace carono\exchange1c\controllers; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use yii\helpers\FileHelper; |
8
|
|
|
use yii\web\NotFoundHttpException; |
9
|
|
|
|
10
|
|
|
class FileController extends Controller |
11
|
|
|
{ |
12
|
|
|
protected static function getAlias($path) |
13
|
|
|
{ |
14
|
|
|
return \Yii::getAlias("@vendor/carono/yii2-1c-exchange/" . ltrim($path, '/')); |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param $file |
19
|
|
|
* @param array $options |
20
|
|
|
* @throws NotFoundHttpException |
21
|
|
|
*/ |
22
|
|
|
protected function out($file, $options = []) |
23
|
|
|
{ |
24
|
|
|
$filename = self::getAlias($file); |
25
|
|
|
if (!file_exists($filename)) { |
26
|
|
|
throw new NotFoundHttpException("File $filename not found"); |
27
|
|
|
} |
28
|
|
|
$content = file_get_contents($filename); |
29
|
|
|
\Yii::$app->response->sendContentAsFile($content, basename($filename), $options); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param $file |
34
|
|
|
*/ |
35
|
|
|
protected function outAsFile($file) |
36
|
|
|
{ |
37
|
|
|
$this->out($file); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param $file |
42
|
|
|
*/ |
43
|
|
|
protected function outAsImage($file) |
44
|
|
|
{ |
45
|
|
|
$options = [ |
46
|
|
|
'inline' => true, |
47
|
|
|
'mimeType' => FileHelper::getMimeType(self::getAlias($file)) |
48
|
|
|
]; |
49
|
|
|
$this->out($file, $options); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param $file |
54
|
|
|
*/ |
55
|
|
|
public function actionArticle($file) |
56
|
|
|
{ |
57
|
|
|
$this->outAsImage("files/articles/$file"); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param $file |
62
|
|
|
*/ |
63
|
|
|
public function actionDoc($file) |
64
|
|
|
{ |
65
|
|
|
$path = "/files/doc/$file"; |
66
|
|
|
if (pathinfo($file, PATHINFO_EXTENSION) == 'xsd') { |
67
|
|
|
$this->outAsFile($path); |
68
|
|
|
} else { |
69
|
|
|
$this->outAsImage($path); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param $file |
75
|
|
|
*/ |
76
|
|
|
public function actionXml($file) |
77
|
|
|
{ |
78
|
|
|
$this->outAsImage("/files/xml/$file"); |
79
|
|
|
} |
80
|
|
|
} |