|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ContentsFile\Model\Behavior\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Cake\Core\Configure; |
|
6
|
|
|
use Cake\Network\Exception\InternalErrorException; |
|
7
|
|
|
use Cake\ORM\TableRegistry; |
|
8
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* NormalContentsFileBehaviorTrait |
|
12
|
|
|
* 通常のファイルアップ系の処理 |
|
13
|
|
|
* メソッド名の先頭に必ずnormalを付けること |
|
14
|
|
|
*/ |
|
15
|
|
|
trait NormalContentsFileBehaviorTrait |
|
16
|
|
|
{ |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* normalParamCheck |
|
20
|
|
|
* 通常の設定値チェック |
|
21
|
|
|
* @author hagiwara |
|
22
|
|
|
* @return void |
|
23
|
|
|
*/ |
|
24
|
|
|
private function normalParamCheck(): void |
|
|
|
|
|
|
25
|
|
|
{ |
|
26
|
|
|
// S3に必要な設定がそろっているかチェックする |
|
27
|
|
|
$normalSetting = Configure::read('ContentsFile.Setting.Normal'); |
|
28
|
|
View Code Duplication |
if ( |
|
|
|
|
|
|
29
|
|
|
!is_array($normalSetting) || |
|
30
|
|
|
!array_key_exists('tmpDir', $normalSetting) || |
|
31
|
|
|
!array_key_exists('fileDir', $normalSetting) |
|
32
|
|
|
) { |
|
33
|
|
|
throw new InternalErrorException('contentsFileNormalConfig paramater shortage'); |
|
34
|
|
|
} |
|
35
|
|
|
// /が最後についていない場合はつける |
|
36
|
|
|
if (!preg_match('#/$#', $normalSetting['tmpDir'])) { |
|
37
|
|
|
Configure::write('ContentsFile.Setting.Normal.tmpDir', $normalSetting['tmpDir'] . '/'); |
|
38
|
|
|
} |
|
39
|
|
|
if (!preg_match('#/$#', $normalSetting['fileDir'])) { |
|
40
|
|
|
Configure::write('ContentsFile.Setting.Normal.fileDir', $normalSetting['fileDir'] . '/'); |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* normalFileSave |
|
46
|
|
|
* ファイルを保存 |
|
47
|
|
|
* @author hagiwara |
|
48
|
|
|
* @param array $fileInfo |
|
49
|
|
|
* @param array $fieldSettings |
|
50
|
|
|
* @param array $attachmentSaveData |
|
51
|
|
|
* @return bool |
|
52
|
|
|
*/ |
|
53
|
|
|
private function normalFileSave(array $fileInfo, array $fieldSettings, array $attachmentSaveData): bool |
|
|
|
|
|
|
54
|
|
|
{ |
|
55
|
|
|
$newFiledir = Configure::read('ContentsFile.Setting.Normal.fileDir') . $attachmentSaveData['model'] . '/' . $attachmentSaveData['model_id'] . '/'; |
|
56
|
|
|
// ランダムパスの場合の分岐 |
|
57
|
|
View Code Duplication |
if (Configure::read('ContentsFile.Setting.randomFile') === true) { |
|
|
|
|
|
|
58
|
|
|
$newFilepath = $newFiledir . $attachmentSaveData['file_random_path']; |
|
59
|
|
|
} else { |
|
60
|
|
|
$newFilepath = $newFiledir . $fileInfo['field_name']; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
if (Configure::read('ContentsFile.Setting.ext') === true) { |
|
64
|
|
|
$ext = (new \SplFileInfo($attachmentSaveData['file_name']))->getExtension(); |
|
65
|
|
|
$newFilepath .= '.' . $ext; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
//元ファイルは削除する |
|
69
|
|
|
$this->normalFileDelete($attachmentSaveData['model'], $attachmentSaveData['model_id'], $fileInfo['field_name']); |
|
70
|
|
|
|
|
71
|
|
|
if ( |
|
72
|
|
|
!$this->mkdir($newFiledir, 0777, true) || |
|
|
|
|
|
|
73
|
|
|
!rename(Configure::read('ContentsFile.Setting.Normal.tmpDir') . $fileInfo['tmp_file_name'], $newFilepath) |
|
74
|
|
|
) { |
|
75
|
|
|
return false; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
//リサイズ画像作成 |
|
79
|
|
View Code Duplication |
if (!empty($fieldSettings['resize'])) { |
|
|
|
|
|
|
80
|
|
|
foreach ($fieldSettings['resize'] as $resizeSettings) { |
|
81
|
|
|
if (!$this->normalImageResize($newFilepath, $resizeSettings)) { |
|
82
|
|
|
return false; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
return true; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* normalFileDelete |
|
91
|
|
|
* 通常のファイル削除 |
|
92
|
|
|
* @author hagiwara |
|
93
|
|
|
* @param string $modelName |
|
94
|
|
|
* @param integer $modelId |
|
95
|
|
|
* @param string $field |
|
96
|
|
|
* @return bool |
|
97
|
|
|
*/ |
|
98
|
|
|
private function normalFileDelete(string $modelName, int $modelId, string $field): bool |
|
99
|
|
|
{ |
|
100
|
|
|
//attachementからデータを取得 |
|
101
|
|
|
$attachmentModel = TableRegistry::getTableLocator()->get('Attachments'); |
|
102
|
|
|
$attachmentData = $attachmentModel->find('all') |
|
103
|
|
|
->where(['model' => $modelName]) |
|
104
|
|
|
->where(['model_id' => $modelId]) |
|
105
|
|
|
->where(['field_name' => $field]) |
|
106
|
|
|
->first() |
|
107
|
|
|
; |
|
108
|
|
|
// 削除するべきファイルがない |
|
109
|
|
|
if (empty($attachmentData)) { |
|
110
|
|
|
return false; |
|
111
|
|
|
} |
|
112
|
|
View Code Duplication |
if (Configure::read('ContentsFile.Setting.randomFile') === true && $attachmentData->file_random_path != '') { |
|
|
|
|
|
|
113
|
|
|
$deleteField = $attachmentData->file_random_path; |
|
114
|
|
|
} else { |
|
115
|
|
|
$deleteField = $attachmentData->field_name; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
// リサイズのディレクトリ |
|
119
|
|
|
$resizeDir = Configure::read('ContentsFile.Setting.Normal.fileDir') . $modelName . '/' . $modelId . '/' . 'contents_file_resize_' . $deleteField . '/'; |
|
120
|
|
|
if (is_dir($resizeDir)) { |
|
121
|
|
|
$filesystem = new Filesystem(); |
|
122
|
|
|
if (!$filesystem->remove($resizeDir)) { |
|
123
|
|
|
return false; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
// 大元のファイル |
|
128
|
|
|
$deleteFile = Configure::read('ContentsFile.Setting.Normal.fileDir') . $modelName . '/' . $modelId . '/' . $deleteField; |
|
129
|
|
|
if (Configure::read('ContentsFile.Setting.ext') === true) { |
|
130
|
|
|
$ext = (new \SplFileInfo($attachmentData->file_name))->getExtension(); |
|
131
|
|
|
$deleteFile .= '.' . $ext; |
|
132
|
|
|
} |
|
133
|
|
|
if (file_exists($deleteFile) && !unlink($deleteFile)) { |
|
134
|
|
|
return false; |
|
135
|
|
|
} |
|
136
|
|
|
return true; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* normalImageResize |
|
141
|
|
|
* 通常のファイルのリサイズ |
|
142
|
|
|
* @author hagiwara |
|
143
|
|
|
* @param string $newFilepath |
|
144
|
|
|
* @param array $resizeSettings |
|
145
|
|
|
* @return bool |
|
146
|
|
|
*/ |
|
147
|
|
|
private function normalImageResize(string $newFilepath, array $resizeSettings): bool |
|
148
|
|
|
{ |
|
149
|
|
|
return $this->imageResize($newFilepath, $resizeSettings); |
|
|
|
|
|
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|