Skip to content
Merged
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
11 changes: 11 additions & 0 deletions _internal/DeerLister.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace DeerLister;

use DeerLister\ExtensionHelper;
use DeerLister\Previews\ImagePreview;
use Twig\Loader\FilesystemLoader;
use Twig\Environment;
use Twig\TwigFilter;
Expand Down Expand Up @@ -302,6 +304,7 @@ public function render(string $directory, string $preview): string
$path = $this->getRelativePath($directory);

$title = $path == "" ? "Home" : basename($directory);
$imgPreview = null;
$readme = null;
$filesFilter = [];
$displayMode = null;
Expand Down Expand Up @@ -343,6 +346,11 @@ public function render(string $directory, string $preview): string
{
$title = $f["name"];
$f["preview"] = true;

if (in_array($f["extension"], ExtensionHelper::getImageExtensions()))
{
$imgPreview = $f["name"];
}
}

if (!$displayBack && $f["name"] === "..")
Expand All @@ -355,10 +363,13 @@ public function render(string $directory, string $preview): string
}
}

$protocol = (empty($_SERVER['HTTPS']) ? 'http' : 'https');
return $this->twig->render("index.html.twig",
[
"baseUrl" => "$protocol://$_SERVER[HTTP_HOST]",
"files" => $filesFilter,
"title" => $title,
"imgPreview" => $imgPreview,
"path" => [ "full" => $path, "exploded" => array_filter(explode("/", $path)) ],
"readme" => $readme,
"display" => "displays/" . ($displayMode ?? "normal") . ".html.twig",
Expand Down
4 changes: 3 additions & 1 deletion _internal/Displays/AudioDisplay.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

namespace DeerLister\Displays;

use DeerLister\ExtensionHelper;

/**
* Provides display for images
*/
class AudioDisplay implements FileDisplay
{
public function doesHandle(string $ext): bool
{
return in_array($ext, ["mp3", "wav", "ogg", "webm", "flac"]);
return in_array($ext, ExtensionHelper::getAudioExtensions());
}
}
4 changes: 3 additions & 1 deletion _internal/Displays/ImageDisplay.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

namespace DeerLister\Displays;

use DeerLister\ExtensionHelper;

/**
* Provides display for images
*/
class ImageDisplay implements FileDisplay
{
public function doesHandle(string $ext): bool
{
return in_array($ext, ["apng", "png", "jpg", "jpeg", "gif", "svg", "webp"]);
return in_array($ext, ExtensionHelper::getImageExtensions());
}
}
16 changes: 16 additions & 0 deletions _internal/ExtensionHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace DeerLister;

class ExtensionHelper
{
public static function getAudioExtensions() : array
{
return ["mp3", "wav", "ogg", "webm", "flac"];
}

public static function getImageExtensions() : array
{
return ["apng", "png", "jpg", "jpeg", "gif", "svg", "webp"];
}
}
3 changes: 2 additions & 1 deletion _internal/Previews/AudioPreview.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace DeerLister\Previews;

use Twig\Environment;
use DeerLister\ExtensionHelper;

/**
* Provides previews for audios
Expand All @@ -11,7 +12,7 @@ class AudioPreview implements FilePreview
{
public function doesHandle(string $filename, string $ext): bool
{
return in_array($ext, ["mp3", "wav", "ogg", "webm", "flac"]);
return in_array($ext, ExtensionHelper::getAudioExtensions());
}

public function renderPreview(string $path, string $extension, Environment $twig): string
Expand Down
3 changes: 2 additions & 1 deletion _internal/Previews/ImagePreview.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace DeerLister\Previews;

use Twig\Environment;
use DeerLister\ExtensionHelper;

/**
* Provides previews for images
Expand All @@ -11,7 +12,7 @@ class ImagePreview implements FilePreview
{
public function doesHandle(string $filename, string $ext): bool
{
return in_array($ext, ["apng", "png", "jpg", "jpeg", "gif", "svg", "webp"]);
return in_array($ext, ExtensionHelper::getImageExtensions());
}

public function renderPreview(string $path, string $extension, Environment $twig): string
Expand Down
3 changes: 3 additions & 0 deletions _internal/templates/base.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
<title>{{ title }}</title>

<meta property="og:title" content="{{ title }}">
{% if imgPreview is not null %}
<meta property="og:image" content="{{ baseUrl }}/{{ path.full }}/{{ imgPreview }}">
{% endif %}
<meta name="theme-color" content="#4169e1">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

Expand Down