|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: floor12 |
|
5
|
|
|
* Date: 07.01.2018 |
|
6
|
|
|
* Time: 8:43 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace floor12\files\logic; |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
use floor12\files\components\SimpleImage; |
|
13
|
|
|
use floor12\files\models\FileType; |
|
14
|
|
|
use yii\base\ErrorException; |
|
15
|
|
|
use yii\db\ActiveRecordInterface; |
|
16
|
|
|
|
|
17
|
|
|
class FileCreateFromPath |
|
18
|
|
|
{ |
|
19
|
|
|
|
|
20
|
|
|
private $className; |
|
21
|
|
|
private $fieldName; |
|
22
|
|
|
private $filePath; |
|
23
|
|
|
private $fileName; |
|
24
|
|
|
private $storagePath; |
|
25
|
|
|
private $model; |
|
26
|
|
|
|
|
27
|
|
|
public function __construct(ActiveRecordInterface $model, string $filePath, string $className, string $fieldName, string $storagePath, string $fileName = null) |
|
28
|
|
|
{ |
|
29
|
|
|
|
|
30
|
|
|
$this->model = $model; |
|
31
|
|
|
|
|
32
|
|
|
if (!$filePath || !$className || !$fieldName || !$storagePath) |
|
33
|
|
|
throw new ErrorException("Empty params not allowed."); |
|
34
|
|
|
|
|
35
|
|
|
if (!file_exists($storagePath)) |
|
36
|
|
|
throw new ErrorException("File storage not found on disk."); |
|
37
|
|
|
|
|
38
|
|
|
if (!file_exists($filePath)) |
|
39
|
|
|
throw new ErrorException("File not found on disk."); |
|
40
|
|
|
|
|
41
|
|
|
if (!is_writable($storagePath)) |
|
42
|
|
|
throw new ErrorException("File storage is not writable."); |
|
43
|
|
|
$this->filePath = $filePath; |
|
44
|
|
|
$this->fileName = $fileName; |
|
45
|
|
|
$this->fieldName = $fieldName; |
|
46
|
|
|
$this->className = $className; |
|
47
|
|
|
$this->storagePath = $storagePath; |
|
48
|
|
|
|
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** Основная работка |
|
52
|
|
|
* @return bool |
|
53
|
|
|
* @throws ErrorException |
|
54
|
|
|
*/ |
|
55
|
|
|
public function execute() |
|
56
|
|
|
{ |
|
57
|
|
|
// копируем файл в хранилище |
|
58
|
|
|
$tmp_extansion = explode('?', pathinfo($this->filePath, PATHINFO_EXTENSION)); |
|
|
|
|
|
|
59
|
|
|
$extansion = $tmp_extansion[0]; |
|
60
|
|
|
$filename = new PathGenerator($this->storagePath) . "." . $extansion; |
|
61
|
|
|
$new_path = $this->storagePath . $filename; |
|
62
|
|
|
copy($this->filePath, $new_path); |
|
63
|
|
|
|
|
64
|
|
|
// создаем запись в базе |
|
65
|
|
|
$this->model->field = $this->fieldName; |
|
|
|
|
|
|
66
|
|
|
$this->model->class = $this->className; |
|
|
|
|
|
|
67
|
|
|
$this->model->filename = $filename; |
|
|
|
|
|
|
68
|
|
|
if ($this->model->filename) |
|
69
|
|
|
$this->model->title = $this->model->filename; |
|
|
|
|
|
|
70
|
|
|
else |
|
71
|
|
|
$this->model->title = rand(0, 99999); #такой прикол ) |
|
72
|
|
|
$this->model->content_type = $this->model->mime_content_type($new_path); |
|
|
|
|
|
|
73
|
|
|
$this->model->type = $this->detectType(); |
|
|
|
|
|
|
74
|
|
|
$this->model->size = filesize($new_path); |
|
|
|
|
|
|
75
|
|
|
$this->model->created = time(); |
|
|
|
|
|
|
76
|
|
|
if ($this->model->type == FileType::VIDEO) |
|
77
|
|
|
$this->model->video_status = 0; |
|
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
if ($this->model->save()) { |
|
80
|
|
|
|
|
81
|
|
|
if ($this->model->type == FileType::IMAGE) { |
|
82
|
|
|
$exif = ''; |
|
|
|
|
|
|
83
|
|
|
@$exif = exif_read_data($new_path); |
|
84
|
|
|
if (isset($exif['Orientation'])) { |
|
85
|
|
|
$ort = $exif['Orientation']; |
|
86
|
|
|
$rotatingImage = new SimpleImage(); |
|
87
|
|
|
$rotatingImage->load($new_path); |
|
88
|
|
|
switch ($ort) { |
|
89
|
|
|
|
|
90
|
|
|
case 3: // 180 rotate left |
|
91
|
|
|
$rotatingImage->rotateDegrees(180); |
|
92
|
|
|
break; |
|
93
|
|
|
case 6: // 90 rotate right |
|
94
|
|
|
$rotatingImage->rotateDegrees(270); |
|
95
|
|
|
break; |
|
96
|
|
|
case 8: // 90 rotate left |
|
97
|
|
|
$rotatingImage->rotateDegrees(90); |
|
98
|
|
|
} |
|
99
|
|
|
$rotatingImage->save($new_path); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
return true; |
|
103
|
|
|
} |
|
104
|
|
|
return false; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @return integer |
|
109
|
|
|
*/ |
|
110
|
|
|
private function detectType() |
|
111
|
|
|
{ |
|
112
|
|
|
$contentTypeArray = explode('/', $this->model->content_type); |
|
|
|
|
|
|
113
|
|
|
if ($contentTypeArray[0] == 'image') |
|
114
|
|
|
return FileType::IMAGE; |
|
115
|
|
|
if ($contentTypeArray[0] == 'video') |
|
116
|
|
|
return FileType::VIDEO; |
|
117
|
|
|
return FileType::FILE; |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|