1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Encore\Admin\Form\Field; |
4
|
|
|
|
5
|
|
|
use Encore\Admin\Form\Field; |
6
|
|
|
use Illuminate\Support\Facades\Input; |
7
|
|
|
use Illuminate\Support\Facades\Storage; |
8
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
9
|
|
|
|
10
|
|
|
class File extends Field |
11
|
|
|
{ |
12
|
|
|
const ACTION_KEEP = 0; |
13
|
|
|
const ACTION_REMOVE = 1; |
14
|
|
|
|
15
|
|
|
protected $directory = ''; |
16
|
|
|
|
17
|
|
|
protected $name = null; |
18
|
|
|
|
19
|
|
|
protected $options = []; |
20
|
|
|
|
21
|
|
|
protected $storage = ''; |
22
|
|
|
|
23
|
|
|
public function __construct($column, $arguments = []) |
24
|
|
|
{ |
25
|
|
|
$this->initOptions(); |
26
|
|
|
$this->initStorage(); |
27
|
|
|
|
28
|
|
|
parent::__construct($column, $arguments); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
protected function initOptions() |
32
|
|
|
{ |
33
|
|
|
$this->options = [ |
34
|
|
|
'overwriteInitial' => true, |
35
|
|
|
'showUpload' => false, |
36
|
|
|
'language' => config('app.locale'), |
37
|
|
|
]; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
protected function initStorage() |
41
|
|
|
{ |
42
|
|
|
$this->storage = Storage::disk(config('admin.upload.disk')); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function defaultStorePath() |
46
|
|
|
{ |
47
|
|
|
return config('admin.upload.directory.file'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function move($directory, $name = null) |
51
|
|
|
{ |
52
|
|
|
$this->directory = $directory; |
53
|
|
|
|
54
|
|
|
$this->name = $name; |
55
|
|
|
|
56
|
|
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function prepare(UploadedFile $file = null) |
60
|
|
|
{ |
61
|
|
|
if (is_null($file)) { |
62
|
|
|
if ($this->isDeleteRequest()) { |
63
|
|
|
return ''; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $this->original; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$this->directory = $this->directory ?: $this->defaultStorePath(); |
70
|
|
|
|
71
|
|
|
$this->name = $this->name ?: $file->getClientOriginalName(); |
72
|
|
|
|
73
|
|
|
return $this->uploadAndDeleteOriginal($file); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param $file |
78
|
|
|
* |
79
|
|
|
* @return mixed |
80
|
|
|
*/ |
81
|
|
|
protected function uploadAndDeleteOriginal(UploadedFile $file) |
82
|
|
|
{ |
83
|
|
|
$this->renameIfExists($file); |
84
|
|
|
|
85
|
|
|
$target = $this->directory.'/'.$this->name; |
86
|
|
|
|
87
|
|
|
$this->storage->put($target, file_get_contents($file->getRealPath())); |
|
|
|
|
88
|
|
|
|
89
|
|
|
$this->destroy(); |
90
|
|
|
|
91
|
|
|
return $target; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
protected function preview() |
95
|
|
|
{ |
96
|
|
|
$fileName = basename($this->value); |
97
|
|
|
|
98
|
|
|
return <<<EOT |
99
|
|
|
<div class="file-preview-other-frame"> |
100
|
|
|
<div class="file-preview-other"> |
101
|
|
|
<span class="file-icon-4x"><i class="fa fa-file"></i></span> |
102
|
|
|
</div> |
103
|
|
|
</div> |
104
|
|
|
<div class="file-preview-other-footer"><div class="file-thumbnail-footer"> |
105
|
|
|
<div class="file-footer-caption">{$fileName}</div> |
106
|
|
|
</div></div> |
107
|
|
|
EOT; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function options($options = []) |
111
|
|
|
{ |
112
|
|
|
$this->options = array_merge($this->options, $options); |
113
|
|
|
|
114
|
|
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function objectUrl($path) |
118
|
|
|
{ |
119
|
|
|
return trim(config('admin.upload.host'), '/') .'/'. trim($path, '/'); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function render() |
123
|
|
|
{ |
124
|
|
|
$this->js[] = 'bootstrap-fileinput/js/fileinput_locale_'.config('app.locale').'.js'; |
125
|
|
|
|
126
|
|
|
$this->options['initialCaption'] = basename($this->value); |
127
|
|
|
|
128
|
|
|
if (!empty($this->value)) { |
129
|
|
|
$this->options['initialPreview'] = $this->preview(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
$options = json_encode($this->options); |
133
|
|
|
|
134
|
|
|
$this->script = <<<EOT |
135
|
|
|
|
136
|
|
|
$("#{$this->id}").fileinput({$options}); |
137
|
|
|
|
138
|
|
|
$("#{$this->id}").on('filecleared', function(event) { |
139
|
|
|
$("#{$this->id}_action").val(1); |
140
|
|
|
}); |
141
|
|
|
|
142
|
|
|
EOT; |
143
|
|
|
|
144
|
|
|
return parent::render(); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* If is delete request then delete original image. |
149
|
|
|
* |
150
|
|
|
* @return bool |
151
|
|
|
*/ |
152
|
|
|
public function isDeleteRequest() |
153
|
|
|
{ |
154
|
|
|
$action = Input::get($this->id.'_action'); |
155
|
|
|
|
156
|
|
|
if ($action == static::ACTION_REMOVE) { |
157
|
|
|
$this->destroy(); |
158
|
|
|
|
159
|
|
|
return true; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return false; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param $file |
167
|
|
|
* |
168
|
|
|
* @return void |
169
|
|
|
*/ |
170
|
|
|
public function renameIfExists(UploadedFile $file) |
171
|
|
|
{ |
172
|
|
|
if ($this->storage->exists("$this->directory/$this->name")) { |
|
|
|
|
173
|
|
|
$this->name = md5(uniqid()).'.'.$file->guessExtension(); |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
public function destroy() |
178
|
|
|
{ |
179
|
|
|
$this->storage->delete($this->original); |
|
|
|
|
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.