Skip to content

Commit 66d6ac1

Browse files
committed
Upload images to /resources folder instead of /public folder (#115)
1 parent b00b3f5 commit 66d6ac1

File tree

3 files changed

+68
-2
lines changed

3 files changed

+68
-2
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php namespace Unisharp\Laravelfilemanager\controllers;
2+
3+
use Illuminate\Support\Facades\File;
4+
use Illuminate\Support\Facades\Response;
5+
use Illuminate\Http\Request;
6+
7+
/**
8+
* Class RedirectController
9+
* @package Unisharp\Laravelfilemanager\controllers
10+
*/
11+
class RedirectController extends LfmController
12+
{
13+
/**
14+
* Get image from custom directory by route
15+
*
16+
* @param string $image_path
17+
* @return string
18+
*/
19+
public function getImage($base_path, $image_name)
20+
{
21+
return $this->responseImageOrFile($image_name);
22+
}
23+
24+
/**
25+
* Get file from custom directory by route
26+
*
27+
* @param string $file_name
28+
* @return string
29+
*/
30+
public function getFile(Request $request, $base_path, $file_name)
31+
{
32+
$request->request->add(['type' => 'Files']);
33+
34+
return $this->responseImageOrFile($file_name);
35+
}
36+
37+
private function responseImageOrFile($file_name)
38+
{
39+
$file_path = $this->getCurrentPath($file_name);
40+
41+
if (!File::exists($file_path)) {
42+
abort(404);
43+
}
44+
45+
$file = File::get($file_path);
46+
$type = File::mimeType($file_path);
47+
48+
$response = Response::make($file);
49+
$response->header("Content-Type", $type);
50+
51+
return $response;
52+
}
53+
}

src/routes.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,12 @@
8787
]);
8888

8989
Route::get('/demo', 'DemoController@index');
90+
91+
// Get file when base_directory isn't public
92+
$images_url = '/' . \Config::get('lfm.images_folder_name') . '/{base_path}/{image_name}';
93+
$files_url = '/' . \Config::get('lfm.files_folder_name') . '/{base_path}/{file_name}';
94+
Route::get($images_url, 'RedirectController@getImage')
95+
->where('image_name', '.*');
96+
Route::get($files_url, 'RedirectController@getFIle')
97+
->where('file_name', '.*');
9098
});

src/traits/LfmHelpers.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,14 @@ public function getPathPrefix($type)
6060
}
6161

6262
$prefix = config('lfm.' . $this->currentLfmType() . 's_folder_name', $default_folder_name);
63+
$base_directory = config('lfm.base_directory', 'public');
6364

6465
if ($type === 'dir') {
65-
$prefix = config('lfm.base_directory', 'public') . '/' . $prefix;
66+
$prefix = $base_directory . '/' . $prefix;
67+
}
68+
69+
if ($type === 'url' && $base_directory !== 'public') {
70+
$prefix = 'laravel-filemanager/' . $prefix;
6671
}
6772

6873
return $prefix;
@@ -283,7 +288,7 @@ public function getFilesWithInfo($path)
283288
$image_path = $this->getCurrentPath($file_name);
284289
if (File::exists($thumb_path)) {
285290
$thumb_url = $this->getThumbUrl($file_name) . '?timestamp=' . filemtime($thumb_path);
286-
} elseif (File::exists($image_path)) {
291+
} elseif (!$this->isImageToThumb($image_path)) {
287292
$thumb_url = $this->getFileUrl($file_name);
288293
} else {
289294
$thumb_url = null;

0 commit comments

Comments
 (0)