mirror of
https://github.com/VueFileManager/vuefilemanager.git
synced 2026-04-20 17:02:16 +00:00
- public sharing refactored part 2
This commit is contained in:
@@ -3,6 +3,9 @@
|
||||
namespace App\Http\Controllers\Sharing;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Share\AuthenticateShareRequest;
|
||||
use App\Http\Resources\FileResource;
|
||||
use App\Http\Resources\ShareResource;
|
||||
use App\Models\File;
|
||||
use App\Models\Folder;
|
||||
use App\Models\Share;
|
||||
@@ -10,6 +13,8 @@ use App\Services\HelperService;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
class BrowseShareController extends Controller
|
||||
{
|
||||
@@ -20,6 +25,64 @@ class BrowseShareController extends Controller
|
||||
$this->helper = resolve(HelperService::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show page index and delete access_token & shared_token cookie
|
||||
* @param Share $shared
|
||||
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Symfony\Component\HttpFoundation\StreamedResponse
|
||||
*/
|
||||
public function index(Share $shared)
|
||||
{
|
||||
// Delete share_session if exist
|
||||
if ($shared->is_protected) {
|
||||
cookie()->queue('share_session', '', -1);
|
||||
}
|
||||
|
||||
// Check if shared is image file and then show it
|
||||
if ($shared->type === 'file' && !$shared->is_protected) {
|
||||
|
||||
$image = File::whereUserId($shared->user_id)
|
||||
->whereType('image')
|
||||
->whereId($shared->item_id)
|
||||
->firstOrFail();
|
||||
|
||||
// Store user download size
|
||||
$shared
|
||||
->user
|
||||
->record_download(
|
||||
(int)$image->getRawOriginal('filesize')
|
||||
);
|
||||
|
||||
return $this->get_single_image($image, $shared->user_id);
|
||||
}
|
||||
|
||||
return view("index")
|
||||
->with('settings', get_settings_in_json() ?? null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check Password for protected item
|
||||
* @param AuthenticateShareRequest $request
|
||||
* @param Share $shared
|
||||
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
|
||||
*/
|
||||
public function authenticate(AuthenticateShareRequest $request, Share $shared)
|
||||
{
|
||||
// Check password
|
||||
if (Hash::check($request->password, $shared->password)) {
|
||||
|
||||
$cookie = json_encode([
|
||||
'token' => $shared->token,
|
||||
'authenticated' => true,
|
||||
]);
|
||||
|
||||
// Return authorize token with shared options
|
||||
return response(new ShareResource($shared), 200)
|
||||
->cookie('share_session', $cookie, 43200);
|
||||
}
|
||||
|
||||
abort(401, __('vuefilemanager.incorrect_password'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Browse public folders
|
||||
*
|
||||
@@ -60,11 +123,15 @@ class BrowseShareController extends Controller
|
||||
// Check ability to access protected share record
|
||||
$this->helper->check_protected_share_record($shared);
|
||||
|
||||
$query = remove_accents(
|
||||
$request->input('query')
|
||||
);
|
||||
|
||||
// Search files id db
|
||||
$searched_files = File::search($request->input('query'))
|
||||
$searched_files = File::search($query)
|
||||
->where('user_id', $shared->user_id)
|
||||
->get();
|
||||
$searched_folders = Folder::search($request->input('query'))
|
||||
$searched_folders = Folder::search($query)
|
||||
->where('user_id', $shared->user_id)
|
||||
->get();
|
||||
|
||||
@@ -95,7 +162,8 @@ class BrowseShareController extends Controller
|
||||
});
|
||||
|
||||
// Collect folders and files to single array
|
||||
return collect([$folders, $files])->collapse();
|
||||
return collect([$folders, $files])
|
||||
->collapse();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -114,8 +182,8 @@ class BrowseShareController extends Controller
|
||||
|
||||
// Get folders
|
||||
$folders = Folder::with('folders:id,parent_id,name')
|
||||
->where('parent_id', $shared->item_id)
|
||||
->where('user_id', $shared->user_id)
|
||||
->whereParentId($shared->item_id)
|
||||
->whereUserId($shared->user_id)
|
||||
->sortable()
|
||||
->get(['id', 'parent_id', 'id', 'name']);
|
||||
|
||||
@@ -128,4 +196,52 @@ class BrowseShareController extends Controller
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get shared public file record
|
||||
*
|
||||
* @param Share $shared
|
||||
* @return mixed
|
||||
*/
|
||||
public function get_single_file(Share $shared)
|
||||
{
|
||||
// Check ability to access protected share files
|
||||
$this->helper->check_protected_share_record($shared);
|
||||
|
||||
// Get file
|
||||
$file = File::whereUserId($shared->user_id)
|
||||
->whereId($shared->item_id)
|
||||
->firstOrFail();
|
||||
|
||||
// Set access urls
|
||||
$file->setPublicUrl($shared->token);
|
||||
|
||||
return response(new FileResource($file), 200);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get image from storage and show it
|
||||
*
|
||||
* @param $file
|
||||
* @param $user_id
|
||||
* @return \Symfony\Component\HttpFoundation\StreamedResponse
|
||||
*/
|
||||
private function get_single_image($file, $user_id)
|
||||
{
|
||||
// Format pretty filename
|
||||
$file_pretty_name = $file->name . '.' . $file->mimetype;
|
||||
|
||||
// Get file path
|
||||
$path = "/files/$user_id/$file->basename";
|
||||
|
||||
// Check if file exist
|
||||
if (!Storage::exists($path)) abort(404);
|
||||
|
||||
return Storage::response($path, $file_pretty_name, [
|
||||
"Content-Type" => Storage::mimeType($path),
|
||||
"Content-Length" => Storage::size($path),
|
||||
"Accept-Ranges" => "bytes",
|
||||
"Content-Range" => "bytes 0-600/" . Storage::size($path),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user