Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add private download feature #359

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions dist/js/tool.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions resources/js/components/Modals/PreviewModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ const copy = (file: Entity) => {

<IconButton
:as-anchor="true"
:download="file?.name"
:href="file?.url"
:download="`/nova-vendor/nova-file-manager/files/download?path=${file?.path}`"
:href="`/nova-vendor/nova-file-manager/files/download?path=${file?.path}`"
Comment on lines +131 to +132
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move these to an attribute, e.g file.downloadUrl
should allow for a better centralization of data and easier refactoring/customization

variant="secondary"
:title="__('NovaFileManager.actions.download')"
>
Expand Down
1 change: 1 addition & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
Route::post('rename/{resource?}', [FileController::class, 'rename'])->name('rename');
Route::post('delete/{resource?}', [FileController::class, 'delete'])->name('delete');
Route::post('unzip/{resource?}', [FileController::class, 'unzip'])->name('unzip');
Route::get('download', [FileController::class, 'download'])->name('download');
});

Route::prefix('folders')->as('folders.')->group(function () {
Expand Down
4 changes: 4 additions & 0 deletions src/Contracts/Support/InteractsWithFilesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ public function canUnzipFile(Closure $callback): static;

public function resolveCanUnzipFile(NovaRequest $request): bool;

public function canDownloadFile(Closure $callback): static;

public function resolveCanDownloadFile(NovaRequest $request): bool;

public function hasUploadValidator(): bool;

public function getUploadValidator(): ?Closure;
Expand Down
16 changes: 16 additions & 0 deletions src/Http/Controllers/FileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Oneduo\NovaFileManager\Events\FileUnzipped;
use Oneduo\NovaFileManager\Events\FileUnzipping;
use Oneduo\NovaFileManager\Http\Requests\DeleteFileRequest;
use Oneduo\NovaFileManager\Http\Requests\DownloadFileRequest;
use Oneduo\NovaFileManager\Http\Requests\RenameFileRequest;
use Oneduo\NovaFileManager\Http\Requests\UnzipFileRequest;
use Oneduo\NovaFileManager\Http\Requests\UploadFileRequest;
Expand Down Expand Up @@ -117,4 +118,19 @@ public function unzip(UnzipFileRequest $request): JsonResponse
'message' => __('nova-file-manager::messages.file.unzip'),
]);
}

/**
* Download a file
*
* @param \Oneduo\NovaFileManager\Http\Requests\DownloadFileRequest $request
* @return \Illuminate\Http\JsonResponse
*/
public function download(DownloadFileRequest $request)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a type hit for the return of this function

{
$manager = $request->manager();

$file = $request->path;

return $manager->filesystem->download($file);
}
}
3 changes: 2 additions & 1 deletion src/Http/Middleware/Authorize.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Laravel\Nova\Nova;
use Laravel\Nova\Tool;
use Oneduo\NovaFileManager\NovaFileManager;
Expand All @@ -21,7 +22,7 @@ class Authorize
* @param \Closure(\Illuminate\Http\Request):mixed $next
* @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse
*/
public function handle(Request $request, Closure $next): Response|JsonResponse
public function handle(Request $request, Closure $next): Response|JsonResponse|StreamedResponse
{
$tool = collect(Nova::registeredTools())->first([$this, 'matchesTool']);

Expand Down
11 changes: 9 additions & 2 deletions src/Http/Requests/BaseRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,18 @@ public function canUnzipArchive(): bool
return $this->element()?->resolveCanUnzipFile($this) ?? true;
}

public function canDownloadFile(): bool
{
return $this->element()?->resolveCanDownloadFile($this) ?? true;
}

protected function failedAuthorization(): void
{
throw ValidationException::withMessages([
$this->authorizationAttribute() => __('nova-file-manager::errors.authorization.unauthorized',
['action' => $this->authorizationActionAttribute()]),
$this->authorizationAttribute() => __(
'nova-file-manager::errors.authorization.unauthorized',
['action' => $this->authorizationActionAttribute()]
),
Comment on lines -163 to +171
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unnecessary code style change, please keep PR feature-only

]);
}

Expand Down
26 changes: 26 additions & 0 deletions src/Http/Requests/DownloadFileRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Oneduo\NovaFileManager\Http\Requests;

use Oneduo\NovaFileManager\Rules\DiskExistsRule;
use Oneduo\NovaFileManager\Rules\ExistsInFilesystem;

/**
* @property-read string $path
*/
class DownloadFileRequest extends BaseRequest
{
public function authorize(): bool
{
return $this->canDownloadFile();
}

public function rules(): array
{
return [
'path' => ['required', 'string', new ExistsInFilesystem($this)],
];
}
}
35 changes: 35 additions & 0 deletions src/Traits/Support/InteractsWithFilesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ trait InteractsWithFilesystem

public ?Closure $canUnzipFile = null;

public ?Closure $canDownloadFile = null;

public ?Closure $showCreateFolder = null;

public ?Closure $showRenameFolder = null;
Expand All @@ -46,6 +48,8 @@ trait InteractsWithFilesystem

public ?Closure $showUnzipFile = null;

public ?Closure $showDownloadFile = null;

public ?Closure $showCropImage = null;

public array $uploadRules = [];
Expand Down Expand Up @@ -173,6 +177,20 @@ public function shouldShowUnzipFile(NovaRequest $request): bool
: true;
}

public function showDownloadFile(Closure $callback): static
{
$this->showDownloadFile = $callback;

return $this;
}

public function shouldShowDownloadFile(NovaRequest $request): bool
{
return is_callable($this->showDownloadFile)
? call_user_func($this->showDownloadFile, $request)
: true;
}

public function showCropImage(Closure $callback): static
{
$this->showCropImage = $callback;
Expand Down Expand Up @@ -299,6 +317,20 @@ public function resolveCanUnzipFile(NovaRequest $request): bool
: $this->shouldShowUnzipFile($request);
}

public function canDownloadFile(Closure $callback): static
{
$this->canDownloadFile = $callback;

return $this;
}

public function resolveCanDownloadFile(NovaRequest $request): bool
{
return is_callable($this->canDownloadFile)
? call_user_func($this->canDownloadFile, $request)
: $this->shouldShowDownloadFile($request);
}

public function hasUploadValidator(): bool
{
return $this->uploadValidator !== null && is_callable($this->uploadValidator);
Expand Down Expand Up @@ -369,6 +401,7 @@ public function options(): array
'edit' => $this->shouldShowCropImage($request),
'delete' => $this->shouldShowDeleteFile($request),
'unzip' => $this->shouldShowUnzipFile($request),
'download' => $this->shouldShowDownloadFile($request),
],
],
'usePintura' => config('nova-file-manager.use_pintura'),
Expand All @@ -393,13 +426,15 @@ public function merge(InteractsWithFilesystemContract $other): static
'canRenameFile',
'canDeleteFile',
'canUnzipFile',
'canDownloadFile',
'showCreateFolder',
'showRenameFolder',
'showDeleteFolder',
'showUploadFile',
'showRenameFile',
'showDeleteFile',
'showUnzipFile',
'showDownloadFile',
'showCropImage',
'uploadRules',
'pinturaOptions',
Expand Down
Loading