forked from dlds/yii2-attachments
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Module.php
173 lines (139 loc) · 4.8 KB
/
Module.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
namespace dlds\attachments;
use dlds\attachments\models\AppAttachment;
use yii\base\Exception;
use yii\helpers\FileHelper;
use yii\helpers\ArrayHelper;
use yii\i18n\PhpMessageSource;
class Module extends \yii\base\Module
{
public $controllerNamespace = 'dlds\attachments\controllers';
public $storePath = '@app/uploads/store';
public $tempPath = '@app/uploads/temp';
/**
* which patterns for mime are allowed to be attached to the model
*/
public $rules = ['*'];
public function init()
{
parent::init();
// custom initialization code goes here
if (!$this->storePath or !$this->tempPath) {
throw new \Exception('Setup storePath and tempPath in module properties');
}
$this->rules = ArrayHelper::merge(['maxFiles' => 3],$this->rules);
$this->defaultRoute = 'file';
$this->registerTranslations();
}
public function registerTranslations()
{
\Yii::$app->i18n->translations['dlds/*'] = [
'class' => PhpMessageSource::className(),
'sourceLanguage' => 'en-US',
'basePath' => '@vendor/dlds/yii2-attachments/messages',
'fileMap' => [
'dlds/attachments' => 'attachments.php'
],
];
}
public static function t($category, $message, $params = [], $language = null)
{
return \Yii::t('dlds/' . $category, $message, $params, $language);
}
public function getStorePath()
{
return \Yii::getAlias($this->storePath);
}
public function getTempPath()
{
return \Yii::getAlias($this->tempPath);
}
/**
* @param $fileHash
* @return string
*/
public function getFilesDirPath($fileHash)
{
$path = $this->getStorePath() . DIRECTORY_SEPARATOR . $this->getSubDirs($fileHash);
FileHelper::createDirectory($path);
return $path;
}
public function getSubDirs($fileHash, $depth = 3)
{
$depth = min($depth, 9);
$path = '';
for ($i = 0; $i < $depth; $i++) {
$folder = substr($fileHash, $i * 3, 2);
$path .= $folder;
if ($i != $depth - 1) $path .= DIRECTORY_SEPARATOR;
}
return $path;
}
public function getUserDirPath()
{
\Yii::$app->session->open();
$userDirPath = $this->getTempPath() . DIRECTORY_SEPARATOR . \Yii::$app->session->id;
FileHelper::createDirectory($userDirPath);
\Yii::$app->session->close();
return $userDirPath . DIRECTORY_SEPARATOR;
}
public function getShortClass($obj)
{
$className = get_class($obj);
if (preg_match('@\\\\([\w]+)$@', $className, $matches)) {
$className = $matches[1];
}
return $className;
}
/**
* @param $filePath string
* @param $owner
* @return bool|AppAttachment
* @throws \Exception
* @throws \yii\base\InvalidConfigException
*/
public function attachFile($filePath, $owner)
{
if (!$owner->id) {
throw new \Exception('Owner must have id when you attach file');
}
if (!file_exists($filePath)) {
throw new \Exception('File not exist :' . $filePath);
}
$fileHash = md5(microtime(true) . $filePath);
$fileType = pathinfo($filePath, PATHINFO_EXTENSION);
$newFileName = $fileHash . '.' . $fileType;
$fileDirPath = $this->getFilesDirPath($fileHash);
$newFilePath = $fileDirPath . DIRECTORY_SEPARATOR . $newFileName;
if (!copy($filePath, $newFilePath)) {
throw new \Exception('Cannot copy file! ' . $filePath . ' to ' . $newFilePath);
}
$file = new AppAttachment();
$file->name = pathinfo($filePath, PATHINFO_FILENAME);
$file->model = $this->getShortClass($owner);
$file->item_id = $owner->id;
$file->hash = $fileHash;
$file->size = filesize($filePath);
$file->type = $fileType;
$file->mime = FileHelper::getMimeType($filePath);
if ($file->save()) {
unlink($filePath);
return $file;
} else {
if (count($file->getErrors()) > 0) {
$ar = array_shift($file->getErrors());
unlink($newFilePath);
throw new \Exception(array_shift($ar));
}
return false;
}
}
public function detachFile($id)
{
$file = AppAttachment::findOne(['id' => $id]);
if (empty($file)) return false;
$filePath = $this->getFilesDirPath($file->hash) . DIRECTORY_SEPARATOR . $file->hash . '.' . $file->type;
// the original methods doesn't check for file_exists to be
return file_exists($filePath) ? unlink($filePath) && $file->delete() : $file->delete();
}
}