Skip to content

Commit 0761838

Browse files
committed
Merge branch 'devs' into 0.13.x
2 parents 4d3563d + 3c27a8d commit 0761838

2 files changed

Lines changed: 86 additions & 11 deletions

File tree

src/Facades/Storage.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace BlitzPHP\Facades;
1313

14+
use BlitzPHP\Contracts\Filesystem\FilesystemInterface;
1415
use BlitzPHP\Filesystem\FilesystemManager;
1516
use Closure;
1617
use DateTimeInterface;
@@ -22,25 +23,25 @@
2223
* @method static array allDirectories(string|null $directory = null)
2324
* @method static array allFiles(string|null $directory = null)
2425
* @method static bool append(string $path, string $data)
25-
* @method static \BlitzPHP\Filesystem\FilesystemInterface build(array|string $config)
26+
* @method static FilesystemInterface build(array|string $config)
2627
* @method static void buildTemporaryUrlsUsing(Closure $callback)
2728
* @method static string|false checksum(string $path, array $options = [])
28-
* @method static \BlitzPHP\Filesystem\FilesystemInterface cloud()
29+
* @method static FilesystemInterface cloud()
2930
* @method static bool copy(string $from, string $to)
3031
* @method static void createDirectory(string $location, array $config = [])
31-
* @method static \BlitzPHP\Filesystem\FilesystemInterface createFtpDriver(array $config)
32-
* @method static \BlitzPHP\Filesystem\FilesystemInterface createLocalDriver(array $config)
33-
* @method static \BlitzPHP\Filesystem\FilesystemInterface createS3Driver(array $config)
34-
* @method static \BlitzPHP\Filesystem\FilesystemInterface createScopedDriver(array $config)
35-
* @method static \BlitzPHP\Filesystem\FilesystemInterface createSftpDriver(array $config)
32+
* @method static FilesystemInterface createFtpDriver(array $config)
33+
* @method static FilesystemInterface createLocalDriver(array $config)
34+
* @method static FilesystemInterface createS3Driver(array $config)
35+
* @method static FilesystemInterface createScopedDriver(array $config)
36+
* @method static FilesystemInterface createSftpDriver(array $config)
3637
* @method static bool delete(array|string $paths)
3738
* @method static bool deleteDirectory(string $directory)
3839
* @method static array directories(string|null $directory = null, bool $recursive = false)
3940
* @method static bool directoryExists(string $path)
4041
* @method static bool directoryMissing(string $path)
41-
* @method static \BlitzPHP\Filesystem\FilesystemInterface disk(string|null $name = null)
42+
* @method static FilesystemInterface disk(string|null $name = null)
4243
* @method static \Symfony\Component\HttpFoundation\StreamedResponse download(string $path, string|null $name = null, array $headers = [])
43-
* @method static \BlitzPHP\Filesystem\FilesystemInterface drive(string|null $name = null)
44+
* @method static FilesystemInterface drive(string|null $name = null)
4445
* @method static bool exists(string $path)
4546
* @method static FilesystemManager extend(string $driver, Closure $callback)
4647
* @method static bool fileExists(string $path)
@@ -67,7 +68,7 @@
6768
* @method static bool missing(string $path)
6869
* @method static void mixin(object $mixin, bool $replace = true)
6970
* @method static bool move(string $from, string $to)
70-
* @method static \BlitzPHP\Filesystem\FilesystemAdapter|mixed when((Closure | mixed | null) $value = null, (callable | null) $callback = null, (callable | null) $default = null)
71+
* @method static FilesystemAdapter|mixed when((Closure | mixed | null) $value = null, (callable | null) $callback = null, (callable | null) $default = null)
7172
* @method static string path(string $path)
7273
* @method static bool prepend(string $path, string $data)
7374
* @method static bool providesTemporaryUrls()
@@ -88,7 +89,7 @@
8889
* @method static void write(string $location, string $contents, array $config = [])
8990
* @method static bool writeStream(string $path, resource $resource, array $options = [])
9091
*
91-
* @see FilesystemManager
92+
* @see \BlitzPHP\Filesystem\FilesystemManager
9293
*/
9394
final class Storage extends Facade
9495
{

src/Middlewares/FileViewer.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
/**
4+
* This file is part of Blitz PHP framework.
5+
*
6+
* (c) 2022 Dimitri Sitchet Tomkeu <devcode.dst@gmail.com>
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
12+
namespace BlitzPHP\Middlewares;
13+
14+
use BlitzPHP\Filesystem\Adapters\FilesystemAdapter;
15+
use BlitzPHP\Filesystem\Exceptions\FileNotFoundException;
16+
use BlitzPHP\Filesystem\FilesystemManager;
17+
use BlitzPHP\Http\Request;
18+
use BlitzPHP\Http\Response;
19+
use Psr\Http\Message\ResponseInterface;
20+
use Psr\Http\Message\ServerRequestInterface;
21+
use Psr\Http\Server\MiddlewareInterface;
22+
use Psr\Http\Server\RequestHandlerInterface;
23+
24+
class FileViewer implements MiddlewareInterface
25+
{
26+
/**
27+
* Chemin d'accès du fichier qu'on souhaite affiché
28+
*/
29+
private string $path = '';
30+
31+
private ?FilesystemAdapter $disk = null;
32+
33+
public function __construct(private FilesystemManager $filesystem, private Response $response)
34+
{
35+
}
36+
37+
/**
38+
* @param Request $request
39+
*/
40+
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
41+
{
42+
$config = config('filesystems');
43+
44+
if ([] === $config['viewable'] ?? []) {
45+
return $handler->handle($request);
46+
}
47+
48+
$path = trim(urldecode($request->getPath()), '/');
49+
[$prefix, $this->path] = explode('/', $path, 2);
50+
51+
foreach ($config['disks'] as $name => $disk) {
52+
if (str_ends_with(trim($disk['url'], '/'), trim($prefix, '/'))) {
53+
$this->disk = $this->filesystem->disk($name);
54+
break;
55+
}
56+
}
57+
58+
if (null === $this->disk) {
59+
return $handler->handle($request);
60+
}
61+
62+
if (! $this->disk->exists($this->path)) {
63+
throw FileNotFoundException::fileNotFound($this->path);
64+
}
65+
66+
$path = $this->disk->path($this->path);
67+
68+
if ($request->boolean('download')) {
69+
return $this->response->download($path);
70+
}
71+
72+
return $this->response->file($path);
73+
}
74+
}

0 commit comments

Comments
 (0)