Completed
Push — master ( 3c7450...96f371 )
by Song
05:12
created

File::isDeleteRequest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 0
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
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()));
0 ignored issues
show
Bug introduced by
The method put cannot be called on $this->storage (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
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")) {
0 ignored issues
show
Bug introduced by
The method exists cannot be called on $this->storage (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
173
            $this->name = md5(uniqid()).'.'.$file->guessExtension();
174
        }
175
    }
176
177
    public function destroy()
178
    {
179
        $this->storage->delete($this->original);
0 ignored issues
show
Bug introduced by
The method delete cannot be called on $this->storage (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
180
    }
181
}
182