Skip to content

Commit e13f6d5

Browse files
committed
create and apply path handling helpers to LfmHelper
1 parent 2855d17 commit e13f6d5

File tree

1 file changed

+86
-61
lines changed

1 file changed

+86
-61
lines changed

src/traits/LfmHelpers.php

Lines changed: 86 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ trait LfmHelpers
1010
*** Path / Url ***
1111
*****************************/
1212

13+
private $ds = '/';
14+
1315
public function getThumbPath($image_name = null)
1416
{
1517
return $this->getCurrentPath($image_name, 'thumb');
@@ -19,9 +21,7 @@ public function getCurrentPath($file_name = null, $is_thumb = null)
1921
{
2022
$path = $this->composeSegments('dir', $is_thumb, $file_name);
2123

22-
if ($this->isRunningOnWindows()) {
23-
$path = str_replace('/', '\\', $path);
24-
}
24+
$path = $this->translateToOsPath($path);
2525

2626
return base_path($path);
2727
}
@@ -33,38 +33,36 @@ public function getThumbUrl($image_name = null)
3333

3434
public function getFileUrl($image_name = null, $is_thumb = null)
3535
{
36-
$url = $this->composeSegments('url', $is_thumb, $image_name);
37-
38-
return $url;
36+
return $this->composeSegments('url', $is_thumb, $image_name);
3937
}
4038

4139
private function composeSegments($type, $is_thumb, $file_name)
4240
{
43-
$full_path = $this->getPathPrefix($type)
44-
. $this->getFormatedWorkingDir()
45-
. '/'
46-
. $this->appendThumbFolderPath($is_thumb)
47-
. $file_name;
48-
49-
$full_path = str_replace('\\', '/', $full_path);
41+
$full_path = implode($this->ds, [
42+
$this->getPathPrefix($type),
43+
$this->getFormatedWorkingDir(),
44+
$this->appendThumbFolderPath($is_thumb),
45+
$file_name
46+
]);
5047

51-
if (ends_with($full_path, '/')) {
52-
$full_path = substr($full_path, 0, -1);
53-
}
48+
$full_path = $this->removeDuplicateSlash($full_path);
49+
$full_path = $this->translateToLfmPath($full_path);
5450

55-
return $full_path;
51+
return $this->removeLastSlash($full_path);
5652
}
5753

58-
private function getFormatedWorkingDir()
54+
public function getPathPrefix($type)
5955
{
60-
$working_dir = request('working_dir');
61-
62-
// remove first slash
63-
if (starts_with($working_dir, '/')) {
64-
$working_dir = substr($working_dir, 1);
56+
if (in_array($type, ['url', 'dir'])) {
57+
return config('lfm.' . $this->currentLfmType() . 's_' . $type);
58+
} else {
59+
return null;
6560
}
61+
}
6662

67-
return $working_dir;
63+
private function getFormatedWorkingDir()
64+
{
65+
return $this->removeFirstSlash(request('working_dir'));
6866
}
6967

7068
private function appendThumbFolderPath($is_thumb)
@@ -79,28 +77,86 @@ private function appendThumbFolderPath($is_thumb)
7977
$in_thumb_folder = preg_match('/'.$thumb_folder_name.'$/i', $this->getFormatedWorkingDir());
8078

8179
if (!$in_thumb_folder) {
82-
return $thumb_folder_name . '/';
80+
return $thumb_folder_name . $this->ds;
8381
}
8482
}
8583

8684
public function rootFolder($type)
8785
{
88-
$folder_path = '/';
89-
9086
if ($type === 'user') {
91-
$folder_path .= $this->getUserSlug();
87+
$folder_name = $this->getUserSlug();
9288
} else {
93-
$folder_path .= config('lfm.shared_folder_name');
89+
$folder_name = config('lfm.shared_folder_name');
9490
}
9591

96-
return $folder_path;
92+
return $this->ds . $folder_name;
9793
}
9894

9995
public function getRootFolderPath($type)
10096
{
10197
return base_path($this->getPathPrefix('dir') . $this->rootFolder($type));
10298
}
10399

100+
public function getName($file)
101+
{
102+
$lfm_file_path = $this->getInternalPath($file);
103+
104+
$arr_dir = explode($this->ds, $lfm_file_path);
105+
$file_name = end($arr_dir);
106+
107+
return $file_name;
108+
}
109+
110+
public function getInternalPath($file)
111+
{
112+
$file = $this->translateToLfmPath($file);
113+
$lfm_dir_start = strpos($file, $this->getPathPrefix('dir'));
114+
$working_dir_start = $lfm_dir_start + strlen($this->getPathPrefix('dir'));
115+
$lfm_file_path = $this->ds . substr($file, $working_dir_start);
116+
117+
return $this->removeDuplicateSlash($lfm_file_path);
118+
}
119+
120+
private function translateToOsPath($path)
121+
{
122+
if ($this->isRunningOnWindows()) {
123+
$path = str_replace($this->ds, '\\', $path);
124+
}
125+
return $path;
126+
}
127+
128+
private function translateToLfmPath($path)
129+
{
130+
if ($this->isRunningOnWindows()) {
131+
$path = str_replace('\\', $this->ds, $path);
132+
}
133+
return $path;
134+
}
135+
136+
private function removeDuplicateSlash($path)
137+
{
138+
return str_replace($this->ds . $this->ds, $this->ds, $path);
139+
}
140+
141+
private function removeFirstSlash($path)
142+
{
143+
if (starts_with($path, $this->ds)) {
144+
$path = substr($path, 1);
145+
}
146+
147+
return $path;
148+
}
149+
150+
private function removeLastSlash($path)
151+
{
152+
// remove last slash
153+
if (ends_with($path, $this->ds)) {
154+
$path = substr($path, 0, -1);
155+
}
156+
157+
return $path;
158+
}
159+
104160

105161
/****************************
106162
*** Config / Settings ***
@@ -132,15 +188,6 @@ public function allowMultiUser()
132188
return config('lfm.allow_multi_user') === true;
133189
}
134190

135-
public function getPathPrefix($type)
136-
{
137-
if (in_array($type, ['url', 'dir'])) {
138-
return config('lfm.' . $this->currentLfmType() . 's_' . $type);
139-
} else {
140-
return null;
141-
}
142-
}
143-
144191

145192
/****************************
146193
*** File System ***
@@ -191,28 +238,6 @@ public function getUserSlug()
191238
return empty(auth()->user()) ? '' : auth()->user()->$slug_of_user;
192239
}
193240

194-
public function getName($file)
195-
{
196-
$lfm_file_path = $this->getInternalPath($file);
197-
198-
$arr_dir = explode('/', $lfm_file_path);
199-
$file_name = end($arr_dir);
200-
201-
return $file_name;
202-
}
203-
204-
public function getInternalPath($file)
205-
{
206-
if ($this->isRunningOnWindows()) {
207-
$file = str_replace('\\', '/', $file);
208-
}
209-
$lfm_dir_start = strpos($file, $this->getPathPrefix('dir'));
210-
$working_dir_start = $lfm_dir_start + strlen($this->getPathPrefix('dir'));
211-
$lfm_file_path = substr($file, $working_dir_start);
212-
213-
return str_replace('//', '/', '/' . $lfm_file_path);
214-
}
215-
216241
public function error($error_type, $variables = [])
217242
{
218243
return trans('laravel-filemanager::lfm.error-' . $error_type, $variables);

0 commit comments

Comments
 (0)