add temporary file manager
This commit is contained in:
82
app/Http/Controllers/FileLinkController.php
Normal file
82
app/Http/Controllers/FileLinkController.php
Normal file
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Response;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Facades\URL;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class FileLinkController extends Controller
|
||||
{
|
||||
public function index(): View
|
||||
{
|
||||
return view('files.index', [
|
||||
'enabled' => config('file_links.enabled'),
|
||||
]);
|
||||
}
|
||||
|
||||
public function uploadWeb(Request $request)
|
||||
{
|
||||
if (!config('file_links.enabled')) {
|
||||
return redirect()->route('files.index')->withErrors(['file' => 'Функционал отключен']);
|
||||
}
|
||||
|
||||
$data = $this->storeAndBuildTemporaryLink($request);
|
||||
|
||||
return redirect()->route('files.index')->with([
|
||||
'file_link' => $data['url'],
|
||||
'file_expires_at' => $data['expiresAt'],
|
||||
]);
|
||||
}
|
||||
|
||||
public function upload(Request $request): JsonResponse
|
||||
{
|
||||
if (!config('file_links.enabled')) {
|
||||
return response()->json(['message' => 'Функционал отключен'], Response::HTTP_NOT_FOUND);
|
||||
}
|
||||
|
||||
$data = $this->storeAndBuildTemporaryLink($request);
|
||||
|
||||
return response()->json([
|
||||
'path' => $data['path'],
|
||||
'expiresAt' => $data['expiresAt'],
|
||||
'url' => $data['url'],
|
||||
]);
|
||||
}
|
||||
|
||||
public function download(string $path)
|
||||
{
|
||||
if (!config('file_links.enabled')) {
|
||||
abort(Response::HTTP_NOT_FOUND);
|
||||
}
|
||||
|
||||
$disk = config('file_links.disk');
|
||||
|
||||
if (!Storage::disk($disk)->exists($path)) {
|
||||
abort(Response::HTTP_NOT_FOUND);
|
||||
}
|
||||
|
||||
return Storage::disk($disk)->download($path);
|
||||
}
|
||||
|
||||
private function storeAndBuildTemporaryLink(Request $request): array
|
||||
{
|
||||
$request->validate([
|
||||
'file' => 'required|file|max:' . config('file_links.max_size_kb'),
|
||||
]);
|
||||
|
||||
$disk = config('file_links.disk');
|
||||
$directory = config('file_links.directory');
|
||||
$path = $request->file('file')->store($directory, $disk);
|
||||
$expiresAt = now()->addHour();
|
||||
|
||||
return [
|
||||
'path' => $path,
|
||||
'expiresAt' => $expiresAt->toIso8601String(),
|
||||
'url' => URL::temporarySignedRoute('files.download', $expiresAt, ['path' => $path]),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user