generated from rabiloo/skeleton-php-package
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
173 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
namespace Rabiloo\Laravel\LocalTemporaryUrl; | ||
|
||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Facades\Storage; | ||
use Illuminate\Support\Facades\URL; | ||
use League\Flysystem\PathTraversalDetected; | ||
use Symfony\Component\HttpFoundation\StreamedResponse; | ||
|
||
class ServeFile | ||
{ | ||
/** | ||
* Create a new invokable controller to serve files. | ||
*/ | ||
public function __construct( | ||
protected string $disk, | ||
protected array $config, | ||
protected bool $isProduction, | ||
) { | ||
// | ||
} | ||
|
||
/** | ||
* Handle the incoming request. | ||
*/ | ||
public function __invoke(Request $request, string $path): StreamedResponse | ||
{ | ||
abort_unless( | ||
$this->hasValidSignature($request), | ||
$this->isProduction ? 404 : 403 | ||
); | ||
try { | ||
abort_unless(Storage::disk($this->disk)->exists($path), 404); | ||
|
||
/** @var \Illuminate\Filesystem\FilesystemAdapter $disk */ | ||
$disk = Storage::disk($this->disk); | ||
$headers = [ | ||
'Cache-Control' => 'no-store, no-cache, must-revalidate, max-age=0', | ||
'Content-Security-Policy' => "default-src 'none'; style-src 'unsafe-inline'; sandbox", | ||
]; | ||
|
||
return tap( | ||
$disk->response($path, null, $headers), | ||
function ($response) use ($headers) { | ||
if (! $response->headers->has('Content-Security-Policy')) { | ||
$response->headers->replace(); | ||
} | ||
} | ||
); | ||
} catch (PathTraversalDetected $e) { | ||
abort(404); | ||
} | ||
} | ||
|
||
/** | ||
* Determine if the request has a valid signature if applicable. | ||
*/ | ||
protected function hasValidSignature(Request $request): bool | ||
{ | ||
return ($this->config['visibility'] ?? 'private') === 'public' || URL::hasValidRelativeSignature($request); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters