- public sharing refactored part 2

This commit is contained in:
Peter Papp
2021-03-19 09:04:18 +01:00
parent db9900fcfb
commit ed8ab2978f
22 changed files with 1296 additions and 1190 deletions
@@ -88,7 +88,7 @@ class AppFunctionsController extends Controller
return view("vuefilemanager.crawler.og-view")
->with('settings', get_settings_in_json())
->with('metadata', [
'url' => url('/shared', ['token' => $shared->token]),
'url' => url('/share', ['token' => $shared->token]),
'is_protected' => $shared->is_protected,
'user' => $shared->user->settings->name,
'name' => $item->name,
@@ -199,7 +199,7 @@ class BrowseController extends Controller
{
$user_id = Auth::id();
$query = remove_accents($request->input('query'));
$query = remove_accents($request->query);
// Search files id db
$searched_files = File::search($query)
@@ -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),
]);
}
}
@@ -228,6 +228,9 @@ class EditShareItemsController extends Controller
*/
public function zip_folder($id, Share $shared)
{
// Check ability to access protected share record
$this->helper->check_protected_share_record($shared);
// Check access to requested folder
$this->helper->check_item_access($id, $shared);
@@ -261,6 +264,9 @@ class EditShareItemsController extends Controller
*/
public function zip_multiple_files(Request $request, Share $shared)
{
// Check ability to access protected share record
$this->helper->check_protected_share_record($shared);
$file_parent_folders = File::whereUserId($shared->user_id)
->whereIn('id', $request->items)
->get()
@@ -1,135 +0,0 @@
<?php
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\Share;
use App\Models\Setting;
use App\Services\HelperService;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Collection;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use App\Models\Folder;
use App\Models\File;
use App\Models\User;
use Illuminate\Support\Facades\Storage;
class ServeSharedController extends Controller
{
private $helper;
public function __construct()
{
$this->helper = resolve(HelperService::class);
}
/**
* Show page index and delete access_token & shared_token cookie
*
* @param Share $shared
* @return \Illuminate\Http\Response
*/
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->show_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 array|\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)) {
abort(401, __('vuefilemanager.incorrect_password'));
}
// Return authorize token with shared options
return response(new ShareResource($shared), 200)
->cookie('share_session', json_encode([
'token' => $shared->token,
'authenticated' => true,
]), 43200);
}
/**
* Get image from storage and show it
*
* @param $file
* @param $user_id
* @return \Symfony\Component\HttpFoundation\StreamedResponse
*/
private function show_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),
]);
}
/**
* Get shared public file record
*
* @param Share $shared
* @return mixed
*/
public function file_public(Share $shared)
{
// Check ability to access protected share files
$this->helper->check_protected_share_record($shared);
// Get file
$file = File::where('user_id', $shared->user_id)
->where('id', $shared->item_id)
->firstOrFail();
// Set access urls
$file->setPublicUrl($shared->token);
return response(new FileResource($file), 200);
}
}