/
IconsController.php
64 lines (51 loc) · 1.71 KB
/
IconsController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
namespace A17\Twill\Http\Controllers\Admin;
use A17\Twill\Services\Blocks\BlockMaker;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Collection;
use Str;
use Symfony\Component\Finder\SplFileInfo;
class IconsController extends Controller
{
public function __construct(Filesystem $files, BlockMaker $blockMaker)
{
parent::__construct();
$this->files = $files;
$this->blockMaker = $blockMaker;
}
public function index()
{
$icons = collect(
config('twill.block_editor.directories.source.icons')
)->reduce(function (Collection $keep, $path) {
if (!$this->files->exists($path)) {
return $keep;
}
$files = collect($this->files->files($path))->map(function (
SplFileInfo $file
) {
if (in_array($file->getFilename(), config('twill.internal_icons'))) {
return null;
}
return [
'name' => Str::before($file->getFilename(), '.svg'),
'url' => route('admin.icons.show', [
'file' => $file->getFilename(),
]),
];
})->filter();
return $keep->merge($files);
}, collect());
return view('twill::blocks.icons', compact('icons'));
}
public function show($file)
{
$file = $this->blockMaker->getIconFile($file, false);
if (!$this->files->exists($file)) {
abort(404);
}
return response()->stream(function () use ($file) {
echo $this->files->get($file);
}, 200, ["Content-Type" => "image/svg+xml"]);
}
}