Skip to content

Commit ba49d26

Browse files
committed
remove redundant code
1 parent d4b30a1 commit ba49d26

File tree

7 files changed

+33
-143
lines changed

7 files changed

+33
-143
lines changed

public/js/script.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -247,22 +247,21 @@ function goTo(new_dir) {
247247
}
248248

249249
function getPreviousDir() {
250-
var ds = '/';
251250
var working_dir = $('#working_dir').val();
252-
return working_dir.substring(0, working_dir.lastIndexOf(ds));
251+
return working_dir.substring(0, working_dir.lastIndexOf('/'));
253252
}
254253

255254
function setOpenFolders() {
256-
$('[data-type=0]').each(function (index, folder) {
255+
$('#tree [data-path]').each(function (index, folder) {
257256
// close folders that are not parent
258257
var should_open = ($('#working_dir').val() + '/').startsWith($(folder).data('path') + '/');
259258
$(folder).children('i')
260259
.toggleClass('fa-folder-open', should_open)
261260
.toggleClass('fa-folder', !should_open);
262261
});
263262

264-
$('.nav-pills').find('li').removeClass('active');
265-
$('[data-path="' + $('#working_dir').val() + '"]').parent('.nav-item').addClass('active');
263+
$('#tree .nav-item').removeClass('active');
264+
$('#tree [data-path="' + $('#working_dir').val() + '"]').parent('.nav-item').addClass('active');
266265
}
267266

268267
// ====================
@@ -334,7 +333,7 @@ function loadItems() {
334333
selected = [];
335334
var response = JSON.parse(data);
336335
items = response.items;
337-
var hasItems = response.items.length !== 0;
336+
var hasItems = items.length !== 0;
338337
$('#empty').toggleClass('d-none', hasItems);
339338
$('#content').html('').removeAttr('class');
340339

src/Lfm.php

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,6 @@ public function input($key)
3333
return $this->translateFromUtf8($this->request->input($key));
3434
}
3535

36-
/**
37-
* Check current lfm type is image or not.
38-
*
39-
* @return bool
40-
*/
41-
public function isProcessingImages()
42-
{
43-
return lcfirst(str_singular($this->input('type'))) === 'image';
44-
}
45-
4636
/**
4737
* Get only the file name.
4838
*
@@ -77,12 +67,16 @@ public function getCategoryName()
7767
*/
7868
public function currentLfmType()
7969
{
80-
$file_type = 'file';
81-
if ($this->isProcessingImages()) {
82-
$file_type = 'image';
70+
$lfm_type = 'file';
71+
72+
$request_type = lcfirst(str_singular($this->input('type')));
73+
$available_types = array_keys($this->config->get('lfm.folder_categories'));
74+
75+
if (in_array($request_type, $available_types)) {
76+
$lfm_type = $request_type;
8377
}
8478

85-
return $file_type;
79+
return $lfm_type;
8680
}
8781

8882
public function getDisplayMode()

src/LfmFileRepository.php

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -25,36 +25,6 @@ public function rootPath()
2525
return public_path() . '/';
2626
}
2727

28-
public function directories()
29-
{
30-
return File::directories($this->path);
31-
}
32-
33-
public function files()
34-
{
35-
return File::files($this->path);
36-
}
37-
38-
public function makeDirectory()
39-
{
40-
return File::makeDirectory($this->path, 0777, true, true);
41-
}
42-
43-
public function exists()
44-
{
45-
return File::exists($this->path);
46-
}
47-
48-
public function getFile()
49-
{
50-
return File::get($this->path);
51-
}
52-
53-
public function mimeType()
54-
{
55-
return File::mimeType($this->path);
56-
}
57-
5828
public function isDirectory()
5929
{
6030
$parent_path = substr($this->path, 0, strrpos($this->path, '/'));
@@ -69,17 +39,6 @@ public function move($new_lfm_path)
6939
return File::move($this->path, $new_lfm_path->path('storage'));
7040
}
7141

72-
/**
73-
* Check a folder and its subfolders is empty or not.
74-
*
75-
* @param string $directory_path Real path of a directory.
76-
* @return bool
77-
*/
78-
public function directoryIsEmpty()
79-
{
80-
return count(File::allFiles($this->path)) == 0;
81-
}
82-
8342
public function save($file, $new_filename)
8443
{
8544
File::move($file->getRealPath(), $this->path . '/' . $new_filename);

src/LfmItem.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class LfmItem
99
private $lfm_path;
1010
private $lfm;
1111

12+
private $columns = ['name', 'path', 'time', 'icon', 'is_file', 'is_image', 'thumb_url'];
1213
public $attributes = [];
1314

1415
public function __construct(LfmPath $lfm_path, Lfm $lfm)
@@ -29,7 +30,7 @@ public function __get($var_name)
2930

3031
public function fill()
3132
{
32-
foreach (['name', 'path', 'time', 'icon', 'is_file', 'is_image', 'thumb_url'] as $column) {
33+
foreach ($this->columns as $column) {
3334
$this->__get($column);
3435
}
3536

@@ -85,9 +86,7 @@ public function mimeType()
8586

8687
public function extension()
8788
{
88-
return pathinfo($this->absolutePath(), PATHINFO_EXTENSION);
89-
// return \File::extension($old_file->path('absolute'));
90-
// return $this->storage->disk->extension($this->absolutePath());
89+
return $this->lfm_path->extension();
9190
}
9291

9392
public function path()
@@ -107,7 +106,6 @@ public function size()
107106
public function time()
108107
{
109108
return $this->lfm_path->lastModified();
110-
// return filemtime($this->absolutePath());
111109
}
112110

113111
public function thumbUrl()

src/LfmPath.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public function appendPathToFile($path, $is_windows = false)
103103
public function folders()
104104
{
105105
$all_folders = array_map(function ($directory_path) {
106-
return $this->get($directory_path);
106+
return $this->pretty($directory_path);
107107
}, $this->storage->directories($this));
108108

109109
$folders = array_filter($all_folders, function ($directory) {
@@ -116,13 +116,13 @@ public function folders()
116116
public function files()
117117
{
118118
$files = array_map(function ($file_path) {
119-
return $this->get($file_path);
119+
return $this->pretty($file_path);
120120
}, $this->storage->files());
121121

122122
return $this->sortByColumn($files);
123123
}
124124

125-
public function get($item_path)
125+
public function pretty($item_path)
126126
{
127127
$lfm_path = clone $this;
128128
$lfm_path = $lfm_path->setName($this->helper->getNameFromPath($item_path));
@@ -151,7 +151,18 @@ public function createFolder()
151151
return false;
152152
}
153153

154-
return $this->storage->makeDirectory($this);
154+
return $this->storage->makeDirectory(0777, true, true);
155+
}
156+
157+
/**
158+
* Check a folder and its subfolders is empty or not.
159+
*
160+
* @param string $directory_path Real path of a directory.
161+
* @return bool
162+
*/
163+
public function directoryIsEmpty()
164+
{
165+
return count($this->disk->allFiles()) == 0;
155166
}
156167

157168
public function normalizeWorkingDir()
@@ -165,7 +176,6 @@ public function normalizeWorkingDir()
165176
* Sort files and directories.
166177
*
167178
* @param mixed $arr_items Array of files or folders or both.
168-
* @param mixed $sort_type Alphabetic or time.
169179
* @return array of object
170180
*/
171181
public function sortByColumn($arr_items)

src/LfmStorageRepository.php

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,16 @@
33
namespace UniSharp\LaravelFilemanager;
44

55
use Illuminate\Support\Facades\Storage;
6-
use Unisharp\FileApi\FileApi;
76

87
class LfmStorageRepository implements RepositoryContract
98
{
10-
private $disk_name;
11-
129
private $disk;
1310

1411
private $path;
1512

1613
public function __construct($storage_path, $disk_name)
1714
{
18-
$this->disk_name = $disk_name;
19-
$this->disk = Storage::disk($this->disk_name);
15+
$this->disk = Storage::disk($disk_name);
2016
$this->path = $storage_path;
2117
}
2218

@@ -32,36 +28,6 @@ public function rootPath()
3228
return $this->disk->getDriver()->getAdapter()->getPathPrefix();
3329
}
3430

35-
public function directories()
36-
{
37-
return $this->disk->directories($this->path);
38-
}
39-
40-
public function files()
41-
{
42-
return $this->disk->files($this->path);
43-
}
44-
45-
public function makeDirectory()
46-
{
47-
return $this->disk->makeDirectory($this->path, 0777, true, true);
48-
}
49-
50-
public function exists()
51-
{
52-
return $this->disk->exists($this->path);
53-
}
54-
55-
public function getFile()
56-
{
57-
return $this->disk->get($this->path);
58-
}
59-
60-
public function mimeType()
61-
{
62-
return $this->disk->mimeType($this->path);
63-
}
64-
6531
public function isDirectory()
6632
{
6733
$parent_path = substr($this->path, 0, strrpos($this->path, '/'));
@@ -76,24 +42,8 @@ public function move($new_lfm_path)
7642
return $this->disk->move($this->path, $new_lfm_path->path('storage'));
7743
}
7844

79-
/**
80-
* Check a folder and its subfolders is empty or not.
81-
*
82-
* @param string $directory_path Real path of a directory.
83-
* @return bool
84-
*/
85-
public function directoryIsEmpty()
86-
{
87-
return count($this->disk->allFiles($this->path)) == 0;
88-
}
89-
9045
public function save($file, $new_filename)
9146
{
9247
$this->disk->putFileAs($this->path, $file, $new_filename);
9348
}
94-
95-
private function insertSuffix($suffix, $file_name)
96-
{
97-
return substr_replace($file_name, $suffix, strpos($file_name, '.'), 0);
98-
}
9949
}

src/RepositoryContract.php

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,7 @@ interface RepositoryContract
66
{
77
public function rootPath();
88

9-
public function directories();
10-
11-
public function files();
12-
13-
public function makeDirectory();
14-
15-
public function exists();
16-
17-
public function getFile();
18-
19-
public function mimeType();
20-
21-
public function isDirectory();
22-
239
public function move($new_lfm_path);
2410

25-
/**
26-
* Check a folder and its subfolders is empty or not.
27-
*
28-
* @param string $directory_path Real path of a directory.
29-
* @return bool
30-
*/
31-
public function directoryIsEmpty();
11+
public function save($file, $new_filename);
3212
}

0 commit comments

Comments
 (0)