add file download with hashed name

This commit is contained in:
aslan 2024-01-31 08:47:05 +03:00
parent 59689d670e
commit bf232ef382
5 changed files with 16 additions and 10 deletions

View File

@ -44,23 +44,26 @@ class FileController extends Controller
{ {
abort_if(Auth::guest(), 403); abort_if(Auth::guest(), 403);
$content = $request->file('url'); $nameFile = $request->file('url')->getClientOriginalName();
WorkWithFiles::saveFileToUploads($content); $name = Storage::put('public', $request->file('url'));
$newPath = WorkWithFiles::renameFile($content);
$url = $request->file('file');
$return = Storage::put('file.jpg', $content);
dd($return);
$validated = $request->validated(); $validated = $request->validated();
$file = new File(); $file = new File();
$file->name = $validated['name']; $file->name = $validated['name'];
$file->url = $newPath; $file->file_name = $nameFile;
$file->url = Storage::url($name);
$file->position = $validated['position']; $file->position = $validated['position'];
$file->reception_screen_id = $validated['idReceptionScreen']; $file->reception_screen_id = $validated['idReceptionScreen'];
$file->save(); $file->save();
return redirect()->route('files.index'); return redirect()->route('files.index');
} }
public function download($id)
{
$file = (new File())->find($id);
return Storage::url($file->url);
}
public function edit(int $idFile) public function edit(int $idFile)
{ {
abort_if(Auth::guest(), 403); abort_if(Auth::guest(), 403);

View File

@ -13,8 +13,9 @@ class File extends Model
protected $fillable = [ protected $fillable = [
'id', 'id',
'name', 'name',
'file_name',
'url', 'url',
'description' 'description',
]; ];
public function receptionScreen(): BelongsTo public function receptionScreen(): BelongsTo

View File

@ -14,6 +14,7 @@ return new class extends Migration
Schema::create('files', function (Blueprint $table) { Schema::create('files', function (Blueprint $table) {
$table->id(); $table->id();
$table->string('name'); $table->string('name');
$table->string('file_name');
$table->string('description')->nullable(); $table->string('description')->nullable();
$table->string('url'); $table->string('url');
$table->integer('position'); $table->integer('position');

View File

@ -28,7 +28,7 @@
</div> </div>
@foreach($receptionScreen->files as $file) @foreach($receptionScreen->files as $file)
<div class="col-8"> <div class="col-8">
<a href="{{ $file->url }}">{{ $file->name }}</a> <a href="{{ $file->url }}" target="_blank">{{ $file->name }}</a>
</div> </div>
@endforeach @endforeach
@endforeach @endforeach

View File

@ -30,6 +30,7 @@ Route::get('/files/create/{file?}', [FileController::class, 'create'])->name('fi
Route::patch('/files/{file}', [FileController::class, 'update'])->name('files.update'); Route::patch('/files/{file}', [FileController::class, 'update'])->name('files.update');
Route::delete('files/{file}', [FileController::class, 'destroy'])->name('files.destroy'); Route::delete('files/{file}', [FileController::class, 'destroy'])->name('files.destroy');
Route::get('/files/edit/{file}', [FileController::class, 'edit'])->name('files.edit'); Route::get('/files/edit/{file}', [FileController::class, 'edit'])->name('files.edit');
Route::get('/files/download/{file}', [FileController::class, 'download'])->name('files.download');
Route::get('/course', function () { Route::get('/course', function () {
return view('menu.course'); return view('menu.course');