|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ContentsFile\Model\Behavior\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use ContentsFile\Aws\S3; |
|
6
|
|
|
use Cake\Core\Configure; |
|
7
|
|
|
use Cake\Filesystem\Folder; |
|
8
|
|
|
use Cake\I18n\Time; |
|
9
|
|
|
use Cake\Network\Exception\InternalErrorException; |
|
10
|
|
|
use Cake\Utility\Security; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* S3ContentsFileBehaviorTrait |
|
14
|
|
|
* 通常のファイルアップ系の処理 |
|
15
|
|
|
* メソッド名の先頭に必ずs4を付けること |
|
16
|
|
|
*/ |
|
17
|
|
|
trait S3ContentsFileBehaviorTrait |
|
18
|
|
|
{ |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* s3ParamCheck |
|
22
|
|
|
* 通常の設定値チェック |
|
23
|
|
|
* @author hagiwara |
|
24
|
|
|
*/ |
|
25
|
|
|
private function s3ParamCheck() |
|
|
|
|
|
|
26
|
|
|
{ |
|
27
|
|
|
// S3に必要な設定がそろっているかチェックする |
|
28
|
|
|
$s3Setting = Configure::read('ContentsFile.Setting.S3'); |
|
29
|
|
View Code Duplication |
if ( |
|
|
|
|
|
|
30
|
|
|
!is_array($s3Setting) || |
|
31
|
|
|
!array_key_exists('key', $s3Setting) || |
|
32
|
|
|
!array_key_exists('secret', $s3Setting) || |
|
33
|
|
|
!array_key_exists('bucket', $s3Setting) || |
|
34
|
|
|
!array_key_exists('tmpDir', $s3Setting) || |
|
35
|
|
|
!array_key_exists('fileDir', $s3Setting) || |
|
36
|
|
|
!array_key_exists('workingDir', $s3Setting) |
|
37
|
|
|
|
|
38
|
|
|
) { |
|
39
|
|
|
throw new InternalErrorException('contentsFileS3Config paramater shortage'); |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* s3FileSave |
|
45
|
|
|
* ファイルをS3に保存 |
|
46
|
|
|
* @author hagiwara |
|
47
|
|
|
* @param array $fileInfo |
|
48
|
|
|
* @param array $fieldSettings |
|
49
|
|
|
* @param array $attachmentSaveData |
|
50
|
|
|
*/ |
|
51
|
|
|
private function s3FileSave($fileInfo, $fieldSettings, $attachmentSaveData) |
|
|
|
|
|
|
52
|
|
|
{ |
|
53
|
|
|
$S3 = new S3(); |
|
54
|
|
|
$newFiledir = Configure::read('ContentsFile.Setting.S3.fileDir') . '/' . $attachmentSaveData['model'] . '/' . $attachmentSaveData['model_id'] . '/'; |
|
55
|
|
|
$newFilepath = $newFiledir . $fileInfo['field_name']; |
|
56
|
|
|
$oldFilepath = Configure::read('ContentsFile.Setting.S3.tmpDir') . '/' . $fileInfo['tmp_file_name']; |
|
57
|
|
|
|
|
58
|
|
|
// tmpに挙がっているファイルを移 |
|
59
|
|
|
if (!$S3->move($oldFilepath, $newFilepath)) { |
|
60
|
|
|
return false; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
// リサイズディレクトリはまず削除する |
|
64
|
|
|
// 失敗=ディレクトリが存在しないため、成功失敗判定は行わない。 |
|
65
|
|
|
$S3->deleteRecursive($newFilepath . '/' . 'contents_file_resize_' . $fileInfo['field_name']); |
|
66
|
|
|
|
|
67
|
|
|
//リサイズ画像作成 |
|
68
|
|
View Code Duplication |
if (!empty($fieldSettings['resize'])) { |
|
|
|
|
|
|
69
|
|
|
foreach ($fieldSettings['resize'] as $resizeSettings) { |
|
70
|
|
|
if (!$this->s3ImageResize($newFilepath, $resizeSettings)) { |
|
71
|
|
|
return false; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
return true; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* s3FileDelete |
|
80
|
|
|
* S3のファイル削除 |
|
81
|
|
|
* @author hagiwara |
|
82
|
|
|
* @param string $modelName |
|
83
|
|
|
* @param integer $modelId |
|
84
|
|
|
* @param string $field |
|
85
|
|
|
*/ |
|
86
|
|
|
private function s3FileDelete($modelName, $modelId, $field) |
|
|
|
|
|
|
87
|
|
|
{ |
|
88
|
|
|
$S3 = new S3(); |
|
89
|
|
|
// リサイズのディレクトリ |
|
90
|
|
|
$resizeDir = Configure::read('ContentsFile.Setting.S3.fileDir') . '/' . $modelName . '/' . $modelId . '/' . 'contents_file_resize_' . $field . '/'; |
|
91
|
|
|
if (!$S3->deleteRecursive($resizeDir)) { |
|
92
|
|
|
return false; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
// 大元のファイル |
|
96
|
|
|
$deleteFile = Configure::read('ContentsFile.Setting.S3.fileDir') . '/' . $modelName . '/' . $modelId . '/' . $field; |
|
97
|
|
|
if (!$S3->delete($deleteFile)) { |
|
98
|
|
|
return false; |
|
99
|
|
|
} |
|
100
|
|
|
return true; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* s3ImageResize |
|
105
|
|
|
* 画像のリサイズ処理(S3用) |
|
106
|
|
|
* @author hagiwara |
|
107
|
|
|
* @param string $filepath |
|
108
|
|
|
* @param array $resize |
|
109
|
|
|
*/ |
|
110
|
|
|
public function s3ImageResize($filepath, $resize) |
|
111
|
|
|
{ |
|
112
|
|
|
$imagepathinfo = $this->getPathinfo($filepath, $resize); |
|
|
|
|
|
|
113
|
|
|
$S3 = new S3(); |
|
114
|
|
|
// Exception = 存在していない場合 |
|
115
|
|
|
$tmpFileName = Security::hash(rand() . Time::now()->i18nFormat('YYYY/MM/dd HH:ii:ss')); |
|
116
|
|
|
$tmpPath = Configure::read('ContentsFile.Setting.S3.workingDir') . $tmpFileName; |
|
117
|
|
|
// ベースのファイルを取得 |
|
118
|
|
|
$baseObject = $S3->download($filepath); |
|
119
|
|
|
$fp = fopen($tmpPath, 'w'); |
|
120
|
|
|
fwrite($fp, $baseObject['Body']); |
|
121
|
|
|
fclose($fp); |
|
122
|
|
|
if (!$this->imageResize($tmpPath, $resize)) { |
|
|
|
|
|
|
123
|
|
|
//失敗時はそのままのパスを返す(画像以外の可能性あり) |
|
124
|
|
|
unlink($tmpPath); |
|
125
|
|
|
return $filepath; |
|
126
|
|
|
} |
|
127
|
|
|
$resizeFileDir = Configure::read('ContentsFile.Setting.S3.workingDir') . 'contents_file_resize_' . $tmpFileName; |
|
128
|
|
|
$resizeFolder = new Folder($resizeFileDir); |
|
129
|
|
|
// 一つのはず |
|
130
|
|
|
$resizeImg = $resizeFolder->findRecursive()[0]; |
|
131
|
|
|
|
|
132
|
|
|
// リサイズ画像をアップロード |
|
133
|
|
|
$S3->upload($resizeImg, $imagepathinfo['resize_filepath']); |
|
134
|
|
|
|
|
135
|
|
|
// tmpディレクトリの不要なディレクトリ/ファイルを削除 |
|
136
|
|
|
$resizeFolder->delete(); |
|
137
|
|
|
unlink($tmpPath); |
|
138
|
|
|
return $imagepathinfo['resize_filepath']; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
} |
|
142
|
|
|
|