|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ContentsFile\Controller\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Cake\Core\Configure; |
|
6
|
|
|
use Cake\ORM\Entity; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* 通常のファイルローダー周り |
|
10
|
|
|
* NormalContentsFileControllerTrait |
|
11
|
|
|
*/ |
|
12
|
|
|
trait NormalContentsFileControllerTrait |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* normalLoader |
|
16
|
|
|
* 通常のローダー |
|
17
|
|
|
* @param string $filename |
|
18
|
|
|
* @param string $filepath |
|
19
|
|
|
* @return void |
|
20
|
|
|
* @author hagiwara |
|
21
|
|
|
*/ |
|
22
|
|
|
private function normalLoader(string $filename, string $filepath): void |
|
|
|
|
|
|
23
|
|
|
{ |
|
24
|
|
|
$fileExt = null; |
|
25
|
|
|
if (preg_match('/\.([^\.]*)$/', $filename, $ext)) { |
|
26
|
|
|
if ($ext[1]) { |
|
27
|
|
|
$fileExt = strtolower($ext[1]); |
|
28
|
|
|
} |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
$this->response = $this->response->withHeader('Content-Length', filesize($filepath)); |
|
|
|
|
|
|
32
|
|
|
if (!empty($fileExt)) { |
|
33
|
|
|
$fileContentType = $this->getFileType($fileExt); |
|
|
|
|
|
|
34
|
|
|
} else { |
|
35
|
|
|
$fileContentType = $this->getMimeType($filepath); |
|
|
|
|
|
|
36
|
|
|
} |
|
37
|
|
|
$this->response = $this->response->withType($fileContentType); |
|
38
|
|
|
@ob_end_clean(); // clean |
|
|
|
|
|
|
39
|
|
|
$fp = fopen($filepath, 'r'); |
|
40
|
|
|
$body = fread($fp, filesize($filepath)); |
|
41
|
|
|
fclose($fp); |
|
42
|
|
|
$this->response->getBody()->write($body); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* normalTmpFilePath |
|
47
|
|
|
* 通常用のtmpのパス作成 |
|
48
|
|
|
* @author hagiwara |
|
49
|
|
|
* @param string $filename |
|
50
|
|
|
* @return string |
|
51
|
|
|
*/ |
|
52
|
|
|
private function normalTmpFilePath(string $filename): string |
|
|
|
|
|
|
53
|
|
|
{ |
|
54
|
|
|
return Configure::read('ContentsFile.Setting.Normal.tmpDir') . $filename; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* normalFilePath |
|
59
|
|
|
* 通常用のファイルのパス作成 |
|
60
|
|
|
* @author hagiwara |
|
61
|
|
|
* @param Entity $attachmentData |
|
62
|
|
|
* @return string |
|
63
|
|
|
*/ |
|
64
|
|
View Code Duplication |
private function normalFilePath(Entity $attachmentData): string |
|
|
|
|
|
|
65
|
|
|
{ |
|
66
|
|
|
$ext = ''; |
|
67
|
|
|
if (Configure::read('ContentsFile.Setting.ext') === true) { |
|
68
|
|
|
$ext = '.' . (new \SplFileInfo($attachmentData->file_name))->getExtension(); |
|
69
|
|
|
} |
|
70
|
|
|
if (Configure::read('ContentsFile.Setting.randomFile') === true && $attachmentData->file_random_path != '') { |
|
71
|
|
|
return Configure::read('ContentsFile.Setting.Normal.fileDir') . $attachmentData->model . '/' . $attachmentData->model_id . '/' . $attachmentData->file_random_path . $ext; |
|
72
|
|
|
} else { |
|
73
|
|
|
return Configure::read('ContentsFile.Setting.Normal.fileDir') . $attachmentData->model . '/' . $attachmentData->model_id . '/' . $attachmentData->field_name . $ext; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* normalResizeSet |
|
79
|
|
|
* 通常のリサイズ処理 |
|
80
|
|
|
* @author hagiwara |
|
81
|
|
|
* @param string $filepath |
|
82
|
|
|
* @param array $resize |
|
83
|
|
|
* @return string |
|
84
|
|
|
*/ |
|
85
|
|
|
private function normalResizeSet(string $filepath, array $resize): string |
|
|
|
|
|
|
86
|
|
|
{ |
|
87
|
|
|
if (empty($resize['width'])) { |
|
88
|
|
|
$resize['width'] = 0; |
|
89
|
|
|
} |
|
90
|
|
|
if (empty($resize['height'])) { |
|
91
|
|
|
$resize['height'] = 0; |
|
92
|
|
|
} |
|
93
|
|
|
//両方ゼロの場合はそのまま返す |
|
94
|
|
|
if ($resize['width'] == 0 && $resize['height'] == 0) { |
|
95
|
|
|
return $filepath; |
|
96
|
|
|
} |
|
97
|
|
|
$imagepathinfo = $this->baseModel->getPathinfo($filepath, $resize); |
|
|
|
|
|
|
98
|
|
|
|
|
99
|
|
|
//ファイルの存在チェック |
|
100
|
|
|
if (file_exists($imagepathinfo['resize_filepath'])) { |
|
101
|
|
|
return $imagepathinfo['resize_filepath']; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
//ない場合はリサイズを実行 |
|
105
|
|
|
if (!$this->baseModel->imageResize($filepath, $resize)) { |
|
106
|
|
|
//失敗時はそのままのパスを返す(画像以外の可能性あり) |
|
107
|
|
|
return $filepath; |
|
108
|
|
|
} |
|
109
|
|
|
return $imagepathinfo['resize_filepath']; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|