Completed
Push — master ( 3415c0...de686d )
by ARCANEDEV
03:35
created

MediasController::renameMedia()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 46
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 46
ccs 0
cts 38
cp 0
rs 8.6315
c 0
b 0
f 0
cc 4
eloc 30
nc 4
nop 1
crap 20
1
<?php namespace Arcanesoft\Media\Http\Controllers;
2
3
use Carbon\Carbon;
4
use Illuminate\Http\Request;
5
use Illuminate\Support\Facades\Storage;
6
use Illuminate\Support\Str;
7
8
/**
9
 * Class     MediasController
10
 *
11
 * @package  Arcanesoft\Media\Http\Controllers
12
 * @author   ARCANEDEV <[email protected]>
13
 */
14
class MediasController extends Controller
15
{
16
    /* ------------------------------------------------------------------------------------------------
17
     |  Constructor
18
     | ------------------------------------------------------------------------------------------------
19
     */
20
    /**
21
     * MediasController constructor.
22
     */
23
    public function __construct()
24
    {
25
        parent::__construct();
26
27
        $this->setCurrentPage('media');
28
    }
29
30
    /* ------------------------------------------------------------------------------------------------
31
     |  Main Functions
32
     | ------------------------------------------------------------------------------------------------
33
     */
34
    public function index()
35
    {
36
        $this->setTitle('Media');
37
38
        return $this->view('manager');
39
    }
40
41
    public function getAll(Request $request)
42
    {
43
        $location = $request->get('location');
44
        $location = trim($location, '/');
45
46
        return response()->json([
47
            'status' => 'success',
48
            'data'   => array_merge(
49
                $this->getDirectoriesFromLocation($location),
50
                $this->getFilesFromLocation($location)
51
            ),
52
        ]);
53
    }
54
55
    public function uploadMedia(Request $request)
56
    {
57
        dd($request->all());
58
    }
59
60
    public function createDirectory(Request $request)
61
    {
62
        $data      = $request->all();
63
        $validator = \Validator::make($data, [
64
            'name'     => 'required', // TODO: check if the folder does not exists
65
            'location' => 'required',
66
        ]);
67
68
        $path = trim($data['location'], '/') . '/' . str_slug($data['name']);
69
70
        if ($validator->fails()) {
71
            return response()->json([
72
                'status' => 'error',
73
            ], 400);
74
        }
75
76
        $this->disk()->makeDirectory($path);
77
78
        return response()->json([
79
            'status' => 'success',
80
            'data'   => compact('path'),
81
        ]);
82
    }
83
84
    public function renameMedia(Request $request)
85
    {
86
        $data      = $request->all();
87
88
        // TODO: check if the folder does not exists
89
        $validator = \Validator::make($data, [
90
            'media'    => 'required',
91
            'newName'  => 'required',
92
            'location' => 'required',
93
        ]);
94
95
        if ($validator->fails()) {
96
            return response()->json([
97
                'status' => 'error',
98
            ], 400);
99
        }
100
101
        $location = trim($data['location'], '/');
102
        $media    = $data['media'];
103
        $src  = $location . '/' . $media['name'];
104
105
        if ($media['type'] == 'file') {
106
            $ext = pathinfo($media['url'], PATHINFO_EXTENSION);
107
            $dest = $location . '/' . Str::slug(str_replace(".{$ext}", '', $data['newName'])).'.'.$ext;
108
            $this->disk()->move($src, $dest);
109
110
            return response()->json([
111
                'status' => 'success',
112
                'data'   => ['path' => $dest],
113
            ]);
114
        }
115
        elseif ($media['type'] == 'directory') {
116
            $dest = $location . '/' . Str::slug($data['newName']);
117
            $this->disk()->move($src, $dest);
118
119
            return response()->json([
120
                'status' => 'success',
121
                'data'   => ['path' => $dest],
122
            ]);
123
        }
124
125
        return response()->json([
126
            'status'  => 'error',
127
            'message' => 'Something wrong was happened while renaming the media.',
128
        ], 400);
129
    }
130
131
    public function deleteMedia(Request $request)
132
    {
133
        // TODO: Add validation
134
        $data = $request->all();
135
        $path = trim($data['media']['path'], '/');
136
137
        $this->disk()->deleteDirectory($path);
138
139
        return response()->json([
140
            'status' => 'success',
141
        ]);
142
    }
143
144
    /* ------------------------------------------------------------------------------------------------
145
     |  Other Functions
146
     | ------------------------------------------------------------------------------------------------
147
     */
148
    /**
149
     * Get the disk adapter.
150
     *
151
     * @return \Illuminate\Filesystem\FilesystemAdapter
152
     */
153
    protected function disk()
154
    {
155
        return Storage::disk(config('arcanesoft.media.filesystem.default'));
156
    }
157
158
    /**
159
     * @param  string  $location
160
     *
161
     * @return array
162
     */
163
    private function getDirectoriesFromLocation($location)
164
    {
165
        return array_map(function ($directory) use ($location) {
166
            return [
167
                'name' => str_replace("$location/", '', $directory),
168
                'path' => $directory,
169
                'type' => 'directory',
170
            ];
171
        }, $this->disk()->directories($location));
172
    }
173
174
    /**
175
     * @param  string  $location
176
     *
177
     * @return array
178
     */
179
    private function getFilesFromLocation($location)
180
    {
181
        $disk   = $this->disk();
182
183
        return array_map(function ($path) use ($disk, $location) {
184
            return [
185
                'name'         => str_replace("$location/", '', $path),
186
                'type'         => 'file',
187
                'path'         => $path,
188
                'url'          => $disk->url($path),
189
                'mimetype'     => $disk->mimeType($path),
190
                'lastModified' => Carbon::createFromTimestamp($disk->lastModified($path))->toDateTimeString(),
191
                'visibility'   => $disk->getVisibility($path),
192
                'size'         => $disk->size($path),
193
            ];
194
        }, $disk->files($location));
195
    }
196
197
    private function isLocalDisk()
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
198
    {
199
        $driver = config('arcanesoft.media.filesystem.default');
200
201
        return config("arcanesoft.media.filesystem.disks.{$driver}.driver", 'local');
202
    }
203
}
204