Skip to content

Commit b3b3cf2

Browse files
committed
fix path functions
1 parent 7d3d721 commit b3b3cf2

File tree

4 files changed

+44
-72
lines changed

4 files changed

+44
-72
lines changed

src/Lfm.php

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,7 @@ public function getRootFolder($type = null)
124124
$folder = $this->config->get('lfm.shared_folder_name');
125125
}
126126

127-
$ds = static::DS;
128-
if ($this->isRunningOnWindows()) {
129-
$ds = '\\';
130-
}
131-
132-
return $ds . $folder;
127+
return $this->ds() . $folder;
133128
}
134129

135130
public function getThumbFolderName()
@@ -204,6 +199,21 @@ public function translateFromUtf8($input)
204199
return $input;
205200
}
206201

202+
/**
203+
* Get directory seperator of current operating system.
204+
*
205+
* @return string
206+
*/
207+
public function ds()
208+
{
209+
$ds = Lfm::DS;
210+
if ($this->isRunningOnWindows()) {
211+
$ds = '\\';
212+
}
213+
214+
return $ds;
215+
}
216+
207217
/**
208218
* Check current operating system is Windows or not.
209219
*
@@ -214,6 +224,11 @@ public function isRunningOnWindows()
214224
return strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
215225
}
216226

227+
/**
228+
* Check if the package should set up route to the file or not(storage driver).
229+
*
230+
* @return bool
231+
*/
217232
public function shouldSetStorageRoute()
218233
{
219234
$driver = $this->config->get('lfm.driver');

src/LfmPath.php

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -61,32 +61,20 @@ public function getName()
6161

6262
public function path($type = 'storage')
6363
{
64+
$ds = Lfm::DS;
65+
if ($type !== 'working_dir') {
66+
$ds = $this->helper->ds();
67+
}
68+
6469
if ($type == 'working_dir') {
6570
// working directory: /{user_slug}
66-
$result = $this->normalizeWorkingDir();
67-
return $this->appendPathToFile($result, false);
71+
$path = $this->normalizeWorkingDir();
6872
} elseif ($type == 'storage') {
6973
// storage: files/{user_slug}
70-
$result = $this->helper->getCategoryName() . $this->normalizeWorkingDir();
71-
return $this->appendPathToFile($result, $this->helper->isRunningOnWindows());
74+
$path = $this->helper->getCategoryName() . $this->normalizeWorkingDir();
7275
} else {
7376
// absolute: /var/www/html/project/storage/app/files/{user_slug}
74-
$result = $this->storage->rootPath() . $this->helper->getCategoryName() . $this->normalizeWorkingDir();
75-
return $this->appendPathToFile($result, $this->helper->isRunningOnWindows());
76-
}
77-
}
78-
79-
// TODO: work with timestamp
80-
public function url($with_timestamp = false)
81-
{
82-
return Lfm::DS . $this->path('storage');
83-
}
84-
85-
public function appendPathToFile($path, $is_windows = false)
86-
{
87-
$ds = Lfm::DS;
88-
if ($is_windows) {
89-
$ds = '\\';
77+
$path = $this->storage->rootPath() . $this->helper->getCategoryName() . $this->normalizeWorkingDir();
9078
}
9179

9280
if ($this->is_thumb) {
@@ -100,6 +88,12 @@ public function appendPathToFile($path, $is_windows = false)
10088
return $path;
10189
}
10290

91+
// TODO: work with timestamp
92+
public function url($with_timestamp = false)
93+
{
94+
return Lfm::DS . $this->helper->getCategoryName() . $this->path('working_dir');
95+
}
96+
10397
public function folders()
10498
{
10599
$all_folders = array_map(function ($directory_path) {

tests/LfmPathTest.php

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public function test__Get()
2727
$helper->shouldReceive('getCategoryName')->andReturn('files');
2828
$helper->shouldReceive('input')->with('working_dir')->andReturn('/bar');
2929
$helper->shouldReceive('isRunningOnWindows')->andReturn(false);
30+
$helper->shouldReceive('ds')->andReturn('/');
3031

3132
$path = new LfmPath($helper);
3233

@@ -43,6 +44,7 @@ public function test__Call()
4344
$helper->shouldReceive('getCategoryName')->andReturn('files');
4445
$helper->shouldReceive('input')->with('working_dir')->andReturn('/bar');
4546
$helper->shouldReceive('isRunningOnWindows')->andReturn(false);
47+
$helper->shouldReceive('ds')->andReturn('/');
4648

4749
$path = new LfmPath($helper);
4850

@@ -78,6 +80,7 @@ public function testPath()
7880
$helper->shouldReceive('input')->with('working_dir')->andReturnNull();
7981
$helper->shouldReceive('getCategoryName')->andReturn('files');
8082
$helper->shouldReceive('isRunningOnWindows')->andReturn(false);
83+
$helper->shouldReceive('ds')->andReturn('/');
8184

8285
$storage = m::mock(LfmStorage::class);
8386
$storage->shouldReceive('rootPath')->andReturn(realpath(__DIR__ . '/../') . '/storage/app');
@@ -103,24 +106,6 @@ public function testUrl()
103106
$this->assertEquals('/files/foo/foo', $path->setName('foo')->url());
104107
}
105108

106-
public function testThumbAndAppendPathToFile()
107-
{
108-
$helper = m::mock(Lfm::class);
109-
$helper->shouldReceive('getThumbFolderName')->andReturn('thumbs');
110-
$helper->shouldReceive('isRunningOnWindows')->andReturn(false);
111-
112-
$path = new LfmPath($helper);
113-
$path->setName('baz');
114-
115-
$prefix = 'bar';
116-
117-
$this->assertEquals($prefix . '/baz', $path->appendPathToFile($prefix));
118-
119-
$path->thumb();
120-
121-
$this->assertEquals($prefix . '/thumbs/baz', $path->appendPathToFile($prefix));
122-
}
123-
124109
public function testFolders()
125110
{
126111
$storage = m::mock(LfmStorage::class);
@@ -134,6 +119,7 @@ public function testFolders()
134119
$helper->shouldReceive('getNameFromPath')->andReturn('bar');
135120
$helper->shouldReceive('getThumbFolderName')->andReturn('thumbs');
136121
$helper->shouldReceive('isRunningOnWindows')->andReturn(false);
122+
$helper->shouldReceive('ds')->andReturn('/');
137123

138124
$path = new LfmPath($helper);
139125

@@ -152,6 +138,7 @@ public function testFiles()
152138
$helper->shouldReceive('getStorage')->andReturn($storage);
153139
$helper->shouldReceive('getNameFromPath')->andReturn('bar');
154140
$helper->shouldReceive('isRunningOnWindows')->andReturn(false);
141+
$helper->shouldReceive('ds')->andReturn('/');
155142

156143
$path = new LfmPath($helper);
157144

@@ -180,6 +167,7 @@ public function testDelete()
180167
$helper->shouldReceive('getCategoryName')->andReturn('files');
181168
$helper->shouldReceive('input')->with('working_dir')->andReturn('/bar');
182169
$helper->shouldReceive('isRunningOnWindows')->andReturn(false);
170+
$helper->shouldReceive('ds')->andReturn('/');
183171

184172
$path1 = new LfmPath($helper);
185173

@@ -194,6 +182,7 @@ public function testDelete()
194182
$helper->shouldReceive('getCategoryName')->andReturn('files');
195183
$helper->shouldReceive('input')->with('working_dir')->andReturn('/bar');
196184
$helper->shouldReceive('isRunningOnWindows')->andReturn(false);
185+
$helper->shouldReceive('ds')->andReturn('/');
197186

198187
$path2 = new LfmPath($helper);
199188

@@ -212,6 +201,7 @@ public function testCreateFolder()
212201
$helper->shouldReceive('getCategoryName')->andReturn('files');
213202
$helper->shouldReceive('input')->with('working_dir')->andReturn('/bar');
214203
$helper->shouldReceive('isRunningOnWindows')->andReturn(false);
204+
$helper->shouldReceive('ds')->andReturn('/');
215205

216206
$path = new LfmPath($helper);
217207

@@ -229,6 +219,7 @@ public function testCreateFolderButFolderAlreadyExists()
229219
$helper->shouldReceive('getCategoryName')->andReturn('files');
230220
$helper->shouldReceive('input')->with('working_dir')->andReturn('/bar');
231221
$helper->shouldReceive('isRunningOnWindows')->andReturn(false);
222+
$helper->shouldReceive('ds')->andReturn('/');
232223

233224
$path = new LfmPath($helper);
234225

tests/LfmStorageRepositoryTest.php

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,8 @@ public function setUp()
2121
$disk->shouldReceive('getAdapter')->andReturn($disk);
2222
$disk->shouldReceive('getPathPrefix')->andReturn('foo/bar');
2323
$disk->shouldReceive('functionToCall')->with('foo/bar')->andReturn('baz');
24-
$disk->shouldReceive('directories')->with('foo/bar')->andReturn(['foo/bar/baz']);
25-
$disk->shouldReceive('files')->with('foo/bar')->andReturn(['foo/bar/baz']);
26-
$disk->shouldReceive('makeDirectory')->with('foo/bar', 0777, true, true)->andReturn(true);
27-
$disk->shouldReceive('exists')->with('foo/bar')->andReturn(true);
28-
$disk->shouldReceive('get')->with('foo/bar')->andReturn(true);
29-
$disk->shouldReceive('mimeType')->with('foo/bar')->andReturn('text/plain');
3024
$disk->shouldReceive('directories')->with('foo')->andReturn(['foo/bar']);
31-
3225
$disk->shouldReceive('move')->with('foo/bar', 'foo/bar/baz')->andReturn(true);
33-
$disk->shouldReceive('allFiles')->with('foo/bar')->andReturn([]);
3426

3527
Storage::shouldReceive('disk')->andReturn($disk);
3628

@@ -52,26 +44,6 @@ public function testRootPath()
5244
$this->assertEquals('foo/bar', $this->storage->rootPath());
5345
}
5446

55-
public function testDirectories()
56-
{
57-
$this->assertEquals('foo/bar/baz', $this->storage->directories()[0]);
58-
}
59-
60-
public function testFiles()
61-
{
62-
$this->assertEquals('foo/bar/baz', $this->storage->files()[0]);
63-
}
64-
65-
public function testExists()
66-
{
67-
$this->assertTrue($this->storage->exists());
68-
}
69-
70-
public function testMimeType()
71-
{
72-
$this->assertEquals('text/plain', $this->storage->mimeType());
73-
}
74-
7547
public function testIsDirectory()
7648
{
7749
$this->assertTrue($this->storage->isDirectory());

0 commit comments

Comments
 (0)