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

Add share button to preview #15

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Validate composer.json and composer.lock
run: composer validate --strict

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: vendor
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
Expand All @@ -36,7 +36,7 @@ jobs:
run: ./publish.sh

- name: Upload release
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v4
with:
name: Release
path: ./publish
9 changes: 7 additions & 2 deletions _internal/DeerLister.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function __construct()
// Convert a size in byte to something more diggest
$this->twig->addFilter(new TwigFilter("humanFileSize", function($size) {
$units = ["B", "KB", "MB", "GB"];
for ($i = 0; $size > 1024; $i++) $size /= 1024;
for ($i = 0; $size > 1024 && $i < count($units); $i++) $size /= 1024;

return round($size, 2) . $units[$i];
}));
Expand Down Expand Up @@ -153,6 +153,7 @@ private function readDirectory(string $directory, mixed $config): array|false
array_push($files,
[
"name" => $name,
"share" => urlencode($name),
Copy link
Member

Choose a reason for hiding this comment

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

This seems redundant, we already have the name and url encoding it should be done by the link builder.

"isFolder" => $isFolder,
"icon" => $isFolder ? Icons::getFolderIcon() : Icons::getIcon($ext),
"lastModified" => $modified,
Expand Down Expand Up @@ -288,7 +289,7 @@ public function registerFileDisplay(string $name, string $display)
$this->fileDisplays[$name] = $instance;
}

public function render(string $directory): string
public function render(string $directory, string $preview): string
{
// read the directory
if (($files = $this->readDirectory($directory, $this->config)) === false)
Expand Down Expand Up @@ -333,6 +334,10 @@ public function render(string $directory): string
}
}

if (urlencode($f["name"]) === $preview) {
Copy link
Member

Choose a reason for hiding this comment

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

Opening brace on new line.

$f["preview"] = true;
}

if (!$displayBack && $f["name"] === "..")
{ }
else if (!$displayOthers && isset($displayMode) && !$this->fileDisplays[$displayMode]->doesHandle($f["extension"]))
Expand Down
5 changes: 5 additions & 0 deletions _internal/css/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ body {
text-decoration: none;
}

.button:hover {
box-shadow: .15em .15em lightgrey;
cursor: pointer;
}

.codeblock {
padding: 1em;
border-radius: 5px;
Expand Down
62 changes: 44 additions & 18 deletions _internal/js/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,45 +12,71 @@ document.querySelector(".modal").addEventListener("click", function(event) {
event.currentTarget.style.display = "none"
})

const prev = document.getElementById("selected-preview")
if (prev != undefined) {
showFile(prev.href, prev.dataset.preview, prev.dataset.filename, prev.dataset.share)
}

function fileClicked(event) {
const target = event.currentTarget

const file = target.dataset.preview
const filename = target.dataset.filename
const share = target.dataset.share

// check if file is previewable
if (file) {
event.preventDefault()

// fetch file preview
fetch("/?preview=" + encodeURIComponent(file))
.then(response => {
if (!response.ok) {
throw "Failed to preview file: " + response.statusText
}

return response.text()
})
.then(content => {
showFileModal(target, filename, content);
document.querySelectorAll('pre code').forEach((el) => {
hljs.highlightElement(el);
});
})
showFile(target.href, file, filename, share)
}
}

function showFileModal(target, filename, content) {
function showFile(href, file, filename, shareName) {

// fetch file preview
fetch("/?preview=" + encodeURIComponent(file))
.then(response => {
if (!response.ok) {
throw "Failed to preview file: " + response.statusText
}

return response.text()
})
.then(content => {
showFileModal(href, filename, content, shareName);
document.querySelectorAll('pre code').forEach((el) => {
hljs.highlightElement(el);
});
})
}

function showFileModal(href, filename, content, shareName) {
// clone template
const clone = template.content.cloneNode(true)

// fill out
clone.querySelector("h2").innerText = filename
clone.querySelector("div").innerHTML = content

clone.querySelector("a").href = target.href
clone.querySelector("a").href = href

// add into modal and show it
document.querySelector(".modal-body").innerHTML = clone
document.querySelector(".modal-body").innerHTML = ""
document.querySelector(".modal-body").appendChild(clone)
document.getElementById("modal").style.display = "block"

document.getElementById("share").addEventListener("click", _ => {
const nodes = document.querySelectorAll(".path > a")
const dir = nodes[nodes.length - 1]
Comment on lines +70 to +71
Copy link
Member

@TheIndra55 TheIndra55 Sep 12, 2024

Choose a reason for hiding this comment

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

Surely there's a better way to get the directory, like from the URL

const url = `${window.location.origin}${window.location.pathname}?dir=${dir.innerHTML}&share=${shareName}`
Copy link
Member

Choose a reason for hiding this comment

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

URL encode

if (navigator.share)
{
navigator.share({url: url});
}
else
{
window.prompt("Copy to share", url)
}
})
}
8 changes: 6 additions & 2 deletions _internal/templates/displays/audio.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@
</div>
{% for file in files %}
<a href="{{ file | getFilePath(path.full) }}" data-filename="{{ file.name }}"
{%if file.extension | canBeDisplayed(displayName) %}
{% if file.extension | canBeDisplayed(displayName) %}
class="audio-preview"
{%elseif file.filePreview %}
{% elseif file.filePreview %}
data-preview="{{ file.filePreview }}"
data-share="{{ file.share }}"
{% endif %}
{% if file.preview %}
id="selected-preview"
{% endif %}
>
<div class="file-info">
Expand Down
2 changes: 1 addition & 1 deletion _internal/templates/displays/image.html.twig
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="display-image-flex-container">
{% for file in files %}
<a href="{{ file | getFilePath(path.full) }}" data-filename="{{ file.name }}" {%if file.filePreview %} data-preview="{{ file.filePreview }}" {% endif %}>
<a href="{{ file | getFilePath(path.full) }}" data-filename="{{ file.name }}" {%if file.filePreview %} data-preview="{{ file.filePreview }}" data-share="{{ file.share }}" {% endif %} {% if file.preview %} id="selected-preview" {% endif %}>
<div class="display-image">
{%if file.name == ".." %}
<i class="fa-solid fa-arrow-left"></i>
Expand Down
2 changes: 1 addition & 1 deletion _internal/templates/displays/normal.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<div class="last-modified">Last Modified</div>
</div>
{% for file in files %}
<a href="{{ file | getFilePath(path.full) }}" data-filename="{{ file.name }}" {%if file.filePreview %} data-preview="{{ file.filePreview }}" {% endif %}>
<a href="{{ file | getFilePath(path.full) }}" data-filename="{{ file.name }}" {%if file.filePreview %} data-preview="{{ file.filePreview }}" data-share="{{ file.share }}" {% endif %} {% if file.preview %} id="selected-preview" {% endif %}>
<div class="file-info">
<div class="icon"><i class="{{ file.icon }}"></i></div>
<div class="name">{{ file.name }}</div>
Expand Down
2 changes: 1 addition & 1 deletion _internal/templates/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
</div>

<template id="filepreview">
<h2 class="preview-title"></h2> <a class="button" download>Download</a>
<h2 class="preview-title"></h2> <a class="button" download>Download</a> <a class="button" id="share">Share</a>

<div class="preview-content">

Expand Down
2 changes: 1 addition & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@
exit;
}

echo $lister->render($_GET["dir"] ?? "");
echo $lister->render($_GET["dir"] ?? "", $_GET["share"] ?? "");