Compare commits

..

77 Commits
v1.0.0 ... main

Author SHA1 Message Date
6c301cff0c fix!: bug where it was possible to send text unauthenticated 2024-11-02 01:34:52 -03:00
b73c06f1ab docs: update on what SHOULD_AUTH actually does 2024-10-31 10:17:23 -03:00
0668e42ea8 style: br on a newline 2024-10-31 10:13:58 -03:00
f2e38fda23 feat: change uploaded filename to uppercase 2024-10-31 10:08:56 -03:00
ae81ead712 feat: add audio displaying 2024-10-29 19:28:30 -03:00
9262f436b6 fix: logic on naming url with secret 2024-10-29 19:21:23 -03:00
343af57742 docs: document -Fsecret 2024-10-29 17:52:38 -03:00
e216e2a1b5 feat: add longer url if "secret" field is present 2024-10-29 17:51:27 -03:00
6a2bf1ea4a feat: add Location header to response 2024-10-21 12:01:32 -03:00
fae72778d9 docs: fix indentation on code blocks 2024-10-21 11:27:27 -03:00
4190f15a50 docs: add fast setup instructions 2024-10-18 11:38:33 -03:00
5c951b3a6d build: remove unecessary files in final image
These aren't needed anymore because we started embedding `static` and
`templates` directly into the binary, so that makes it easier to:

1. Distribute
2. Setup
3. Run
2024-10-18 11:26:43 -03:00
ecb147533a feat: embed UI assets into binary for easier distributing 2024-10-18 11:18:35 -03:00
0b3f167fce build!: fix error that would make an initial setup not work properly 2024-10-17 19:18:28 -03:00
766efc53c3 ops: comment out replacing templates
this can cause issues since if the template isnt found then it blows up
2024-10-17 19:00:33 -03:00
ddfc788ac5 docs: add setup instructions 2024-10-17 18:56:09 -03:00
e753301e33 chore: remove duplicated features
some mistake on my end while rebasing
2024-10-17 18:48:08 -03:00
37f12ad067 fix: make files cover all space available 2024-10-17 18:22:16 -03:00
c3424c1bb3 refactor: set values on Application creation 2024-10-17 15:26:13 -03:00
a36cec5cb1 feat: add support for fish scripts 2024-10-17 13:08:29 -03:00
35b1b183b2 docs: add features to readme 2024-10-17 13:07:49 -03:00
d6b7dcc74e docs: add tutorial for jwt tokens 2024-10-17 13:06:12 -03:00
ed2c0e7ef9 feat: add tokens for authentication 2024-10-17 10:52:33 -03:00
7ed16a5397 feat: redirect to another link if file is not supported 2024-10-16 23:34:08 -03:00
18bc590525 docs: add syntax highlighting goal completed 2024-10-16 23:27:02 -03:00
eda09669f8 feat: make /last also use new template 2024-10-16 23:20:14 -03:00
c32a459147 docs: add another goal completed 2024-10-16 22:42:46 -03:00
6c87fe724c feat: add syntax highlighting to files using highlightjs 2024-10-16 22:41:59 -03:00
8b0300c738 feat: add timeUploaded to /tree 2024-10-16 22:41:59 -03:00
8ec85fdc1b feat: add uploadedTime to index page 2024-10-16 22:41:59 -03:00
4384cfaa35 feat: add text support for custom page 2024-10-16 22:41:59 -03:00
6d9dd1aa65 feat: add custom page handling for most media format
I haven't gotten text to work yet
2024-10-16 22:41:59 -03:00
ff566a2ff5 wip: add handling for extensions 2024-10-16 22:41:59 -03:00
c64c63d333 feat: remove download button and make header more useful 2024-10-16 22:41:59 -03:00
ebe2c461f5 wip: add custom template for displaying files 2024-10-16 22:41:59 -03:00
52fa208aab docs: change from my domain to standard localhost 2024-10-16 11:14:51 -03:00
c8b23c79b9 docs: add more examples to uploads 2024-10-15 22:59:01 -03:00
8afc49bd04 refactor: modularize main function 2024-10-15 22:41:51 -03:00
00d2f3e510 refactor: remove redundant uploadHandler 2024-10-15 18:37:37 -03:00
ba7ebb6210 docs: update docs on /upload 2024-10-15 15:39:54 -03:00
bdab72dcac feat: move upload page to home page 2024-10-15 15:35:45 -03:00
8bf4d7e5c4 feat: update default webpage with new buttons 2024-10-15 15:26:55 -03:00
b10bdf21b5 feat: add check on whether to restrict text uploading 2024-10-15 15:21:05 -03:00
030196cf51 fix: drop over-reliance on app.lastUploadedFile
It won't be good for future code maintainance if filepath for the actual
file is dependant on a variable that gets updated when something is
hashed
2024-10-15 15:14:22 -03:00
1bd55061bf feat: add form for uploading 2024-10-15 15:08:27 -03:00
6ef22dd93f refactor: further curl refactoring 2024-10-15 14:38:01 -03:00
0d6c6d02b1 refactor: refactor curl handler 2024-10-15 14:18:55 -03:00
e87382bc77 wip: add upload box 2024-10-15 13:33:18 -03:00
f5cb446264 refactor: split helper functions 2024-10-03 21:19:14 -03:00
58c8321a18 feat: add footer to homepage 2024-09-24 13:23:46 -03:00
9d5fbca929 fix: mkv videos not playing on browser 2024-09-19 15:47:24 -03:00
e341d2f166 fix: limited /tmp space inside container
causes big uploads to not go through
2024-09-19 15:39:11 -03:00
444c725fc6 fix: change to html/template for more security 2024-09-19 15:38:43 -03:00
e42f307bd1 ci: create docker image on tag creation 2024-09-19 13:17:29 -03:00
189c0c50fd ops: add templates dir to docker compose 2024-09-19 10:33:43 -03:00
92dd395f34 docs: mention theming templates 2024-09-19 10:30:28 -03:00
39c6a1f570 feat: add customizable file tree 2024-09-19 10:28:23 -03:00
914e303667 fix: check .env file existence 2024-09-18 14:00:04 -03:00
2c5a818c10 fix: set text mimetypes for common extensions 2024-09-18 13:58:05 -03:00
9e2dc9df91 feat: change filenames for md5 hashes 2024-09-18 13:48:04 -03:00
621cd34508 docs: add home page explanation 2024-09-18 10:34:42 -03:00
93194f4a1c feat: add static website example 2024-09-18 10:26:32 -03:00
20fd599ed7 chore: update gitignore 2024-09-18 10:24:21 -03:00
1211e730e9 fix!: fix docker path handling
after i trimmed down the image, it didn't have workdir /app, so we need
to fix it
2024-09-18 10:23:43 -03:00
1ab01ac492 fix: correct path handling for static
on docker, you'd need to do /static and not /app/static
2024-09-18 10:19:04 -03:00
9d23b391fe fix: more intuitive port handling 2024-09-18 10:16:03 -03:00
4ed73c3086 feat: add /last for checking last uploaded file 2024-09-18 10:03:53 -03:00
468fb823e9 feat: add static webpage 2024-09-18 09:55:26 -03:00
90f63f4c54 chore: reword docs 2024-09-06 16:51:14 -03:00
804366e331 fix: doesn't work with sh, just bash 2024-09-06 16:43:38 -03:00
cc7db8c441 fix: server wouldnt bind to any port after generate_config.sh 2024-09-06 16:43:22 -03:00
3c8224cb4d docs: update docs on variables 2024-09-06 16:35:01 -03:00
7dd62b2cd7 ops: change default port in docker compose 2024-09-06 16:34:38 -03:00
31e894b6bc feat: add customizable port and file directories 2024-09-06 16:34:20 -03:00
47994c2bb2 docs: add mention to docker image 2024-09-06 12:07:20 -03:00
91ca3254a7 ops: change build for image
Some checks are pending
build abyss / build (amd64, linux) (push) Waiting to run
build abyss / build (arm64, linux) (push) Waiting to run
build abyss / build (ppc64le, linux) (push) Waiting to run
build abyss / build (riscv64, linux) (push) Waiting to run
Build and Push Docker Image / build (push) Waiting to run
2024-09-06 11:15:56 -03:00
a65265af49 build: push docker images automatically 2024-09-06 11:15:43 -03:00
18 changed files with 1145 additions and 206 deletions

28
.github/workflows/docker-tag.yaml vendored Normal file
View File

@ -0,0 +1,28 @@
name: Build and Push Docker Image Tag
on:
push:
tags:
- "*"
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Log in to Docker registry
run: echo "${{ secrets.DOCKER_PASSWORD }}" | docker login git.jabuxas.xyz -u ${{ secrets.DOCKER_USERNAME }} --password-stdin
- name: Build and push Docker image
run: |
docker build -t git.jabuxas.xyz/jabuxas/abyss:${{ github.ref_name }} .
docker push git.jabuxas.xyz/jabuxas/abyss:${{ github.ref_name }}
- name: Log out of Docker registry
run: docker logout git.jabuxas.xyz

31
.github/workflows/docker.yaml vendored Normal file
View File

@ -0,0 +1,31 @@
name: Build and Push Docker Image
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Log in to Docker registry
run: echo "${{ secrets.DOCKER_PASSWORD }}" | docker login git.jabuxas.xyz -u ${{ secrets.DOCKER_USERNAME }} --password-stdin
- name: Build and push Docker image
run: |
docker build -t git.jabuxas.xyz/jabuxas/abyss:latest .
docker push git.jabuxas.xyz/jabuxas/abyss:latest
- name: Log out of Docker registry
run: docker logout git.jabuxas.xyz

View File

@ -1,89 +0,0 @@
# .github/workflows/release.yaml
on: release
name: Build Release
jobs:
release-linux-386:
name: release linux/386
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- name: compile and release
uses: ngs/go-release.action@v1.0.1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GOARCH: "386"
GOOS: linux
EXTRA_FILES: "README.md LICENSE"
release-linux-amd64:
name: release linux/amd64
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- name: compile and release
uses: ngs/go-release.action@v1.0.1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GOARCH: amd64
GOOS: linux
EXTRA_FILES: "README.md LICENSE"
release-linux-arm:
name: release linux/386
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- name: compile and release
uses: ngs/go-release.action@v1.0.1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GOARCH: "arm"
GOOS: linux
EXTRA_FILES: "README.md LICENSE"
release-linux-arm64:
name: release linux/amd64
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- name: compile and release
uses: ngs/go-release.action@v1.0.1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GOARCH: arm64
GOOS: linux
EXTRA_FILES: "README.md LICENSE"
release-darwin-amd64:
name: release darwin/amd64
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- name: compile and release
uses: ngs/go-release.action@v1.0.1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GOARCH: amd64
GOOS: darwin
EXTRA_FILES: "README.md LICENSE"
release-windows-386:
name: release windows/386
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- name: compile and release
uses: ngs/go-release.action@v1.0.1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GOARCH: "386"
GOOS: windows
EXTRA_FILES: "README.md LICENSE"
release-windows-amd64:
name: release windows/amd64
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- name: compile and release
uses: ngs/go-release.action@v1.0.1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GOARCH: amd64
GOOS: windows
EXTRA_FILES: "README.md LICENSE"

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
files/ files/
abyss abyss
.env .env
dev/

View File

@ -3,7 +3,10 @@ FROM golang:1.23 AS builder
WORKDIR /app WORKDIR /app
COPY go.mod go.sum ./ COPY go.mod go.sum ./
COPY go.mod ./
# this is needed because we embed these files into the binary
COPY static/ ./static/
COPY templates/ ./templates
RUN go mod download RUN go mod download

183
README.md
View File

@ -1,65 +1,196 @@
# abyss # abyss
abyss is a basic single user http server made for uploading files (logs, images) and then sharing them to the internet abyss is a basic and mostly single user http server written in go made for uploading files (logs, images) and then sharing them to the internet
note: this is a project made for learning purposes, you should use other more mature projects if running in production. probably. <figure>
<img src="https://github.com/user-attachments/assets/eae42368-d8b5-4c42-ac8a-0e1486fcd0d4" alt="homepage"/>
<figcaption>this is abyss' default home page<figcaption/>
</figure>
## table of contents ## table of contents
- [features](#features)
- [running abyss](#running) - [running abyss](#running)
- [installing with docker](#docker) - [installing with docker](#docker)
- [installing manually](#manual) - [installing manually](#directly)
- [uploading files](#uploading) - [uploading files](#uploading)
- [theming](#theming)
- [docs](#docs) - [docs](#docs)
- [todo list](#todo) - [todo list](#todo)
- [more pictures](#pictures)
## running: ## features
- run `./generate_config.sh` to setup the necessary environment variables - **file uploads**: supports uploading various file types, including images, videos, and documents.
- **flexible media display**: automatically renders uploaded files on a webpage based on their type (images, pdfs, videos, or plain text).
- **easily customizable interface**: allows for easy modification of color schemes and layout to suit specific design needs.
- **syntax highlighting for code**: syntax highlighting available by default for code files, with support for multiple programming languages. (can be tweaked/changed and even removed)
- **security considerations**: as it is single user, it's mostly secure but there are still some edges to sharpen
- **easy and fast setup**: assets files are baked into the binary, so setting it up is as easy as compiling/grabbing a binary from gh actions and running it!
## running
#### (recommended) setting it up
- clone the repository and cd into it:
```bash
git clone https://github.com/jabuxas/abyss.git --depth 1 && cd abyss
```
- then run `./generate_config.sh` to setup the necessary environment variables
- after that, you can use either docker or run it directly
### docker ### docker
- to run with docker, you can use docker compose: - to run with docker, you can use the `docker-compose.yml` file available in this repo. to do so, run:
```bash ```bash
docker compose up -d # might be docker-compose depending on distro docker compose up -d # might be docker-compose depending on distro
``` ```
- dont change inside port of 8999 unless you know what you're doing - you can optionally use the [docker image](https://git.jabuxas.xyz/jabuxas/-/packages/container/abyss/latest) directly and set it up how you want
- when updating, run with `--build` instead:
```bash ### directly
docker compose up --build -d
```
### manual - to run it manually, build it with `go build -o abyss` or grab a binary from github actions and run:
- to run it manually, build it with `go build -o abyss` and run: ```bash
./abyss
```
```bash - you will need to either:
./abyss - create a `.env` file in `$(pwd)` and set up the necessary variables as in [docs](#docs)
``` - run it with the variables prepended: `AUTH_USERNAME=admin AUTH_PASSWORD=admin ./abyss` (example)
- and then (hopefully) create a service that does that automatically and runs `abyss`
## uploading ## uploading
- then, simply upload your files with curl: #### with curl
```bash - you can upload both with the main key and with jwt tokens
curl -F "file=@/path/to/file" -H "X-Auth: "$(cat /path/to/.key) http://localhost:8999/
``` ##### main key
- to upload your files with main key:
```bash
curl -F "file=@/path/to/file" -H "X-Auth: "$(cat /path/to/.key) http://localhost:3235/
```
- it is also possible to add a `-Fsecret=` to your POST to make filenames bigger and harder to guess.
- you should probably create an `alias` or a `function` to do this automatically for you.
<details>
<summary>click for an example for bash/zsh:</summary>
```bash
pst() {
local file
if [[ -p /dev/stdin ]]; then
file=$(mktemp)
cat > "$file"
elif [[ -n $1 ]]; then
file="$1"
else
echo "Usage: pst [file]"
return 1
fi
curl -F "file=@$file" -H "X-Auth: $(cat ~/.key)" http://localhost:3235/
if [[ -p /dev/stdin ]]; then
rm "$file"
fi
}
```
</details>
<details>
<summary>click for an example for fish shell:</summary>
```bash
function pst
set -l file
if command test -p /dev/stdin
set file "/tmp/tmp.txt"
cat > $file
else if test -n "$argv[1]"
set file "$argv[1]"
end
curl -F "file=@$file" -H "X-Auth: $(cat ~/.key)" http://localhost:3235/
if command test -p /dev/stdin
rm "$file"
end
end
```
</details>
##### with jwt tokens
- you first need to generate them:
```bash
curl -u admin http://localhost:3235/token # you can also access the url in the browser directly
```
- the user will be the value of `$AUTH_USERNAME` and password the value of `$AUTH_PASSWORD`
- then you use the token in place of the main key:
```bash
curl -F"file=@/path/to/file.jpg" -H "X-Auth: your-token" http://localhost:3235/
```
#### through the browser
- you can only upload text through the browser, to do so, simply write text in the form in the default webpage and click upload.
- this upload can be restricted to need authentication or not, controlled by an environment variable.
## theming
- there is an example homepage in `static/` you can edit directly, which the server will serve automatically
- if running with docker, it's also possible to override `/static` inside the container with your own page.
- otherwise you will need to clone this repository and edit `static/` and `templates/` manually, or recreate the structure.
- same thing with templates in `templates/`
- it is preferred to use `dev/` for that reason, since it is git-ignored and that way makes it easier if wanting to update regularly without making changes to the tree
## docs ## docs
- `ABYSS_URL`: this is used for the correct formatting of the response of `curl`. - `ABYSS_URL`: this is used for the correct formatting of the response of `curl`.
- `AUTH_USERNAME | AUTH_PASSWORD`: this is used to access `http://localhost:8999/tree`, which shows all uploaded files - `AUTH_USERNAME | AUTH_PASSWORD`: this is used to access `/tree`, which shows all uploaded files
- `UPLOAD_KEY`: this is key checked when uploading files. if the key doesn't match with server's one, then it refuses uploading. - `UPLOAD_KEY`: this is key checked when uploading files. if the key doesn't match with server's one, then it refuses uploading.
- `ABYSS_FILEDIR`: this points to the directory where abyss will save the uploads to. defaults to `./files`
- `ABYSS_PORT`: this is the port the server will run on. safe to leave empty. defaults to 3235
- `SHOULD_AUTH`: if it is `yes`, then to upload text you will need authentication (same auth as `/tree`), any value other than that and upload is authless
## todo: ## todo:
- [x] add upload of logs funcionality (like 0x0.st) - [x] add upload of logs funcionality (like 0x0.st)
- [x] add docker easy setup - [x] add docker easy setup
- ~~add db for tracking of file names~~ (dont need that)
- [x] add file browser (like file://) - [x] add file browser (like file://)
- [x] add file extension in its name - [x] add file extension in its name
- [x] login prompt when accessing /tree - [x] login prompt when accessing /tree
- [ ] add rate limits - [x] home page
- [x] custom file displaying!!
- [x] syntax highlighting
- [ ] create example services (openrc/systemd)
- [ ] distribute it in some distros
## pictures
<figure>
<img src="https://github.com/user-attachments/assets/32ce9b3a-8c0f-4bb5-bdcf-3a602e0c81e6"/>
<figcaption>this is abyss' default directory list<figcaption/>
</figure>
<figure>
<img src="https://github.com/user-attachments/assets/e842e481-13ee-464b-be43-5ba0f4bb43ec"/>
<figcaption>this is abyss' default file presentation<figcaption/>
</figure>

View File

@ -2,6 +2,7 @@ package main
import ( import (
"log" "log"
"log/slog"
"net/http" "net/http"
"os" "os"
"time" "time"
@ -9,40 +10,37 @@ import (
"github.com/joho/godotenv" "github.com/joho/godotenv"
) )
const (
filesDir = "./files"
port = ":8999"
)
func main() { func main() {
app := new(Application) err := godotenv.Load()
if err != nil {
godotenv.Load() slog.Warn("no .env file detected, getting env from running process")
app.auth.username = os.Getenv("AUTH_USERNAME")
app.auth.password = os.Getenv("AUTH_PASSWORD")
app.url = os.Getenv("ABYSS_URL")
app.key = os.Getenv("UPLOAD_KEY")
if app.auth.username == "" {
log.Fatal("basic auth username must be provided")
} }
if app.auth.password == "" { app := &Application{
log.Fatal("basic auth password must be provided") auth: struct {
username string
password string
}{
username: os.Getenv("AUTH_USERNAME"),
password: os.Getenv("AUTH_PASSWORD"),
},
url: os.Getenv("ABYSS_URL"),
key: os.Getenv("UPLOAD_KEY"),
filesDir: os.Getenv("ABYSS_FILEDIR"),
port: os.Getenv("ABYSS_PORT"),
authUpload: os.Getenv("SHOULD_AUTH"),
} }
parseEnv(app)
mux := http.NewServeMux() mux := http.NewServeMux()
mux.HandleFunc("/", app.fileHandler)
mux.HandleFunc( setupHandlers(mux, app)
"/tree/",
app.basicAuth(app.treeHandler),
)
srv := &http.Server{ srv := &http.Server{
Addr: port, Addr: app.port,
Handler: mux, Handler: mux,
IdleTimeout: time.Minute, IdleTimeout: 10 * time.Second,
ReadTimeout: 10 * time.Second, ReadTimeout: 10 * time.Second,
WriteTimeout: 60 * time.Second, WriteTimeout: 60 * time.Second,
} }
@ -53,3 +51,53 @@ func main() {
log.Fatalf("Failed to start server: %v", err) log.Fatalf("Failed to start server: %v", err)
} }
} }
func parseEnv(app *Application) {
if app.auth.username == "" {
log.Fatal("basic auth username must be provided")
}
if app.auth.password == "" {
log.Fatal("basic auth password must be provided")
}
if app.key == "" {
slog.Warn("no upload key detected")
}
if app.filesDir == "" {
slog.Warn("file dir is not set, running on default ./files")
app.filesDir = "./files"
}
if app.port == "" {
slog.Info("running on default port")
app.port = ":3235"
} else {
slog.Info("running on modified port")
app.port = ":" + app.port
}
if app.url == "" {
slog.Warn("no root url detected, defaulting to localhost.")
app.url = "localhost" + app.port
}
}
func setupHandlers(mux *http.ServeMux, app *Application) {
mux.HandleFunc("/", app.indexHandler)
mux.Handle(
"/tree/",
http.StripPrefix(
"/tree",
BasicAuth(app.fileListingHandler, app),
),
)
mux.HandleFunc("/last", app.lastUploadedHandler)
mux.HandleFunc("/token", BasicAuth(app.createTokenHandler, app))
mux.HandleFunc("/files/", app.fileHandler)
}

View File

@ -1,10 +1,14 @@
services: services:
paste: paste:
build: . image: git.jabuxas.xyz/jabuxas/abyss:latest
ports: ports:
- "58080:8999" - "3235:3235"
volumes: volumes:
- ./files:/app/files - ./files:/files
restart: unless-stopped # - ./dev/home:/static:ro
# - ./dev/templates:/templates:ro
env_file: env_file:
- .env - .env
tmpfs:
- /tmp
restart: unless-stopped

75
file_display.go Normal file
View File

@ -0,0 +1,75 @@
package main
import (
"embed"
"html/template"
"log/slog"
"net/http"
"os"
"path/filepath"
"strings"
)
var extensions = map[string]string{
".mp4": "video", ".mkv": "video", ".webm": "video",
".pdf": "pdf",
".png": "image", ".jpg": "image", ".jpeg": "image", ".webp": "image",
".mp3": "audio", ".aac": "audio", ".wav": "audio", ".flac": "audio", ".ogg": "audio",
".sh": "text", ".bash": "text", ".zsh": "text",
".bat": "text", ".cmd": "text", ".ps1": "text",
".ini": "text", ".cfg": "text", ".conf": "text",
".toml": "text", ".yml": "text", ".yaml": "text",
".c": "text", ".cpp": "text", ".h": "text",
".go": "text", ".py": "text", ".js": "text",
".ts": "text", ".html": "text", ".htm": "text",
".xml": "text", ".css": "text", ".java": "text",
".rs": "text", ".rb": "text", ".php": "text",
".pl": "text", ".sql": "text", ".md": "text",
".log": "text", ".txt": "text", ".csv": "text",
".json": "text", ".env": "text", ".sum": "text",
".gitignore": "text", ".dockerfile": "text", ".Makefile": "text",
".rst": "text", ".el": "text", ".fish": "text",
}
//go:embed templates/files.html
var filesTemplate embed.FS
func DisplayFile(app *Application, file string, w http.ResponseWriter) {
var tmpl *template.Template
if _, err := os.Stat("./templates/dirlist.html"); err == nil {
tmpl = template.Must(template.ParseFiles("templates/files.html"))
} else {
tmpl = template.Must(template.ParseFS(filesTemplate, "templates/files.html"))
}
fileStat, _ := os.Stat("." + file)
fileContent, _ := os.ReadFile("." + file)
fileInfo := FileInfo{
Name: file,
Path: filepath.Join(app.url, file),
Type: getType(file),
Content: string(fileContent),
TimeUploaded: fileStat.ModTime().
UTC().
Format("2006-01-02 15:04:05 UTC"),
}
if err := tmpl.Execute(w, fileInfo); err != nil {
slog.Warn(err.Error())
}
}
func getType(file string) string {
extension := strings.ToLower(filepath.Ext(file))
if fileType, exists := extensions[extension]; exists {
return fileType
}
return "unknown"
}

View File

@ -1,10 +1,17 @@
#!/bin/sh #!/bin/bash
echo "Press enter to use the default value '[value]' or use a custom value" echo "Press enter to use the default value '[value]' or use a custom value"
read -p "Server domain name - this is the end url of where abyss will be hosted []: " -e ABYSS_URL read -p "Server domain name - this is the end url of where abyss will be hosted []: " -e ABYSS_URL
read -p "Upload key - this is the key you will need to have to be able to make uploads to the server []: " -e UPLOAD_KEY read -p "Upload key - this is the key you will need to have to be able to make uploads to the server []: " -e UPLOAD_KEY
read -p "Files Directory - this is the dir location for storing the files [./files]: " -e ABYSS_FILEDIR
if [ -z $ABYSS_FILEDIR ]; then
ABYSS_FILEDIR="./files"
fi
read -p "Server port - this is the port the server will run on; type just the port number. []: " -e ABYSS_PORT
read -p "Auth username - this is the username to access /tree (show all uploaded files) [admin]: " -e AUTH_USERNAME read -p "Auth username - this is the username to access /tree (show all uploaded files) [admin]: " -e AUTH_USERNAME
if [ -z $AUTH_USERNAME ]; then if [ -z $AUTH_USERNAME ]; then
AUTH_USERNAME="admin" AUTH_USERNAME="admin"
@ -15,16 +22,30 @@ if [ -z $AUTH_PASSWORD ]; then
AUTH_PASSWORD="admin" AUTH_PASSWORD="admin"
fi fi
read -p "Auth for upload form - should password be needed to upload text through the browser? [yes]: " -e SHOULD_AUTH
if [ -z $SHOULD_AUTH ]; then
SHOULD_AUTH="yes"
fi
cat << EOF > .env cat << EOF > .env
# This is the full name of the final domain for the server. Example: paste.abyss.dev # This is the full name of the final domain for the server. Example: paste.abyss.dev
ABYSS_URL=$ABYSS_URL ABYSS_URL=$ABYSS_URL
# Where abyss will store files. It's fine to leave it empty. Defaults to "./files"
ABYSS_FILEDIR=$ABYSS_FILEDIR
# The port the server will run on, it's fine to leave it empty. Defaults to :3235
ABYSS_PORT=$ABYSS_PORT
# This is the username of the user for accessing /tree # This is the username of the user for accessing /tree
AUTH_USERNAME=$AUTH_USERNAME AUTH_USERNAME=$AUTH_USERNAME
# This is the password of the user for accessing /tree # This is the password of the user for accessing /tree
AUTH_PASSWORD=$AUTH_PASSWORD AUTH_PASSWORD=$AUTH_PASSWORD
# This is whether you need a password to upload text (through browser or curl)
SHOULD_AUTH=$SHOULD_AUTH
# This is the key needed to make uploads. Include it as X-Auth in curl. # This is the key needed to make uploads. Include it as X-Auth in curl.
# Tip: Save it somewhere and use it in curl with \$(cat /path/to/key) # Tip: Save it somewhere and use it in curl with \$(cat /path/to/key)
UPLOAD_KEY=$UPLOAD_KEY UPLOAD_KEY=$UPLOAD_KEY

2
go.mod
View File

@ -3,3 +3,5 @@ module github.com/jabuxas/abyss
go 1.22.6 go 1.22.6
require github.com/joho/godotenv v1.5.1 require github.com/joho/godotenv v1.5.1
require github.com/golang-jwt/jwt/v5 v5.2.1 // indirect

2
go.sum
View File

@ -1,2 +1,4 @@
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=

View File

@ -1,14 +1,19 @@
package main package main
import ( import (
"crypto/sha256" "embed"
"crypto/subtle"
"fmt" "fmt"
"html/template"
"io" "io"
"io/fs"
"log/slog"
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
"strings"
"time" "time"
"github.com/golang-jwt/jwt/v5"
) )
type Application struct { type Application struct {
@ -18,16 +23,65 @@ type Application struct {
} }
url string url string
key string key string
filesDir string
port string
authUpload string
lastUploadedFile string
} }
func (app *Application) fileHandler(w http.ResponseWriter, r *http.Request) { //go:embed static/**
if r.Method == http.MethodPost { var static embed.FS
app.uploadHandler(w, r)
//go:embed templates/dirlist.html
var treeTemplate embed.FS
func (app *Application) fileListingHandler(w http.ResponseWriter, r *http.Request) {
dir := app.filesDir + r.URL.Path
files, err := os.ReadDir(dir)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return return
} }
name := filepath.Clean(r.URL.Path) var fileInfos []FileInfo
path := filepath.Join(filesDir, name) for _, file := range files {
filePath := filepath.Join(dir, file.Name())
info, err := os.Stat(filePath)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fileInfos = append(fileInfos, FileInfo{
Name: file.Name(),
Path: filepath.Join(r.URL.Path, file.Name()),
Size: info.Size(),
FormattedSize: FormatFileSize(info.Size()),
TimeUploaded: info.ModTime().
UTC().
Format("2006-01-02 15:04:05 UTC"),
})
}
var tmpl *template.Template
if _, err := os.Stat("./templates/dirlist.html"); err == nil {
tmpl = template.Must(template.ParseFiles("templates/dirlist.html"))
} else {
tmpl = template.Must(template.ParseFS(treeTemplate, "templates/dirlist.html"))
}
templateData := TemplateData{
Files: fileInfos,
URL: app.url,
}
if err := tmpl.Execute(w, templateData); err != nil {
slog.Warn(err.Error())
}
}
func (app *Application) fileHandler(w http.ResponseWriter, r *http.Request) {
path := fmt.Sprintf(".%s", filepath.Clean(r.URL.Path))
if !filepath.IsLocal(path) { if !filepath.IsLocal(path) {
http.Error(w, "Wrong url", http.StatusBadRequest) http.Error(w, "Wrong url", http.StatusBadRequest)
@ -36,22 +90,106 @@ func (app *Application) fileHandler(w http.ResponseWriter, r *http.Request) {
if fileInfo, err := os.Stat(path); err == nil && !fileInfo.IsDir() { if fileInfo, err := os.Stat(path); err == nil && !fileInfo.IsDir() {
http.ServeFile(w, r, path) http.ServeFile(w, r, path)
} else { return
http.NotFound(w, r)
} }
} }
func (app *Application) treeHandler(w http.ResponseWriter, r *http.Request) { func (app *Application) indexHandler(w http.ResponseWriter, r *http.Request) {
http.StripPrefix("/tree/", http.FileServer(http.Dir(filesDir))).ServeHTTP(w, r) if _, err := os.Stat(app.filesDir); err != nil {
if err := os.Mkdir(app.filesDir, 0750); err != nil {
http.Error(w, "Error creating storage directory", http.StatusInternalServerError)
}
}
if r.Method == http.MethodPost {
app.uploadHandler(w, r)
return
}
name := filepath.Clean(r.URL.Path)
path := filepath.Join(app.filesDir, name)
if !filepath.IsLocal(path) {
http.Error(w, "Wrong url", http.StatusBadRequest)
return
}
if fileInfo, err := os.Stat(path); err == nil && !fileInfo.IsDir() {
DisplayFile(app, "/"+path, w)
return
}
if _, err := os.Stat("./static"); err == nil {
http.StripPrefix("/", http.FileServer(http.Dir("./static"))).ServeHTTP(w, r)
} else {
fs, _ := fs.Sub(static, "static")
http.StripPrefix("/", http.FileServer(http.FS(fs))).ServeHTTP(w, r)
}
}
func (app *Application) lastUploadedHandler(w http.ResponseWriter, r *http.Request) {
if app.lastUploadedFile == "" {
http.Error(w, "No new files uploaded yet", http.StatusNotFound)
return
}
DisplayFile(app, "/"+app.lastUploadedFile, w)
} }
func (app *Application) uploadHandler(w http.ResponseWriter, r *http.Request) { func (app *Application) uploadHandler(w http.ResponseWriter, r *http.Request) {
if contentType := r.Header.Get("Content-Type"); contentType == "application/x-www-form-urlencoded" {
if app.authUpload == "yes" {
BasicAuth(app.formHandler, app)(w, r)
} else {
app.formHandler(w, r)
}
} else if strings.Split(contentType, ";")[0] == "multipart/form-data" {
app.curlHandler(w, r)
} else {
http.Error(w, "Method not allowed", http.StatusUnauthorized)
}
}
func (app *Application) formHandler(w http.ResponseWriter, r *http.Request) {
content := r.FormValue("content")
if err := os.WriteFile("/tmp/file.txt", []byte(content), 0666); err != nil {
http.Error(w, "Couldn't parse content body", http.StatusNoContent)
}
file, err := os.Open("/tmp/file.txt")
if err != nil {
http.Error(w, "Couldn't find file", http.StatusNotFound)
}
defer file.Close()
full := true
if len(r.Form["secret"]) == 0 {
full = false
}
filename := app.publicURL(file, ".txt", full)
// reopening file because hash consumes it
file, err = os.Open("/tmp/file.txt")
if err != nil {
http.Error(w, "Couldn't find file", http.StatusNotFound)
}
defer file.Close()
err = SaveFile(app.lastUploadedFile, file)
if err != nil {
fmt.Fprintf(w, "Error parsing file: %s", err.Error())
}
ResponseURLHandler(w, app.url, filename)
}
func (app *Application) curlHandler(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" { if r.URL.Path != "/" {
http.Error(w, "Method not allowed", http.StatusUnauthorized) http.Error(w, "Method not allowed", http.StatusUnauthorized)
return return
} }
if !app.checkAuth(r) { if !CheckAuth(r, app.key) {
http.Error(w, "You're not authorized.", http.StatusBadRequest) http.Error(w, "You're not authorized.", http.StatusBadRequest)
return return
} }
@ -59,66 +197,52 @@ func (app *Application) uploadHandler(w http.ResponseWriter, r *http.Request) {
file, handler, err := r.FormFile("file") file, handler, err := r.FormFile("file")
if err != nil { if err != nil {
http.Error(w, "Error retrieving the file", http.StatusBadRequest) http.Error(w, "Error retrieving the file", http.StatusBadRequest)
slog.Warn(err.Error())
return return
} }
defer file.Close() defer file.Close()
if _, err := os.Stat(filesDir); err != nil { full := true
if err := os.Mkdir(filesDir, 0750); err != nil { if len(r.Form["secret"]) == 0 {
http.Error(w, "Error creating storage directory", http.StatusInternalServerError) full = false
}
} }
filename := app.publicURL(file, filepath.Ext(handler.Filename), full)
time := int64(float64(time.Now().Unix()) * 2.71828) // euler :) // reopen the file for copying, as the hash process consumed the file reader
file, _, err = r.FormFile("file")
filename := fmt.Sprintf("%d%s", time, filepath.Ext(handler.Filename))
filepath := fmt.Sprintf("%s/%s", filesDir, filename)
dst, err := os.Create(filepath)
if err != nil { if err != nil {
http.Error(w, "Error creating file\n", http.StatusInternalServerError) http.Error(w, "Error retrieving the file", http.StatusBadRequest)
}
defer dst.Close()
if _, err := io.Copy(dst, file); err != nil {
http.Error(w, "Error copying the file", http.StatusInternalServerError)
}
if app.url == "" {
fmt.Fprintf(w, "http://localhost%s/%s\n", port, filename)
} else {
fmt.Fprintf(w, "http://%s/%s\n", app.url, filename)
}
}
func (app *Application) checkAuth(r *http.Request) bool {
return r.Header.Get("X-Auth") == string(app.key)
}
func (app *Application) basicAuth(next http.HandlerFunc) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
username, password, ok := r.BasicAuth()
if ok {
// hash password received
usernameHash := sha256.Sum256([]byte(username))
passwordHash := sha256.Sum256([]byte(password))
// hash our password
expectedUsernameHash := sha256.Sum256([]byte(app.auth.username))
expectedPasswordHash := sha256.Sum256([]byte(app.auth.password))
// compare hashes
usernameMatch := (subtle.ConstantTimeCompare(usernameHash[:], expectedUsernameHash[:]) == 1)
passwordMatch := (subtle.ConstantTimeCompare(passwordHash[:], expectedPasswordHash[:]) == 1)
if usernameMatch && passwordMatch {
next.ServeHTTP(w, r)
return return
} }
defer file.Close()
if err = SaveFile(app.lastUploadedFile, file); err != nil {
fmt.Fprintf(w, "Error parsing file: %s", err.Error())
} }
w.Header().Set("WWW-Authenticate", `Basic realm="restricted", charset="UTF-8`) ResponseURLHandler(w, app.url, filename)
http.Error(w, "Unauthorized", http.StatusUnauthorized) }
})
func (app *Application) publicURL(file io.Reader, extension string, full bool) string {
filename, _ := HashFile(file, extension, full)
filepath := filepath.Join(app.filesDir, filename)
app.lastUploadedFile = filepath
return filename
}
func (app *Application) createTokenHandler(w http.ResponseWriter, r *http.Request) {
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
"exp": time.Now().Add(time.Hour * 2).Unix(),
})
tokenString, err := token.SignedString([]byte(app.key))
if err != nil {
http.Error(w, "Error generating token", http.StatusInternalServerError)
return
}
fmt.Fprintf(w, "%s", tokenString)
} }

134
helpers.go Normal file
View File

@ -0,0 +1,134 @@
package main
import (
"crypto/md5"
"crypto/sha256"
"crypto/subtle"
"encoding/hex"
"fmt"
"io"
"net/http"
"os"
"strings"
"github.com/golang-jwt/jwt/v5"
)
type FileInfo struct {
Name string
Path string
Size int64
FormattedSize string
Type string
Content string
TimeUploaded string
}
type TemplateData struct {
Files []FileInfo
URL string
}
func CheckAuth(r *http.Request, key string) bool {
receivedKey := r.Header.Get("X-Auth")
if receivedKey == key {
return true
} else if err := validateToken(receivedKey, key); err == nil {
return true
}
return false
}
func validateToken(tokenString, key string) error {
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
}
return []byte(key), nil
})
if err != nil {
return err
}
if _, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
return nil
} else {
return fmt.Errorf("invalid token")
}
}
func FormatFileSize(size int64) string {
if size < 1024 {
return fmt.Sprintf("%d B", size)
} else if size < 1024*1024 {
return fmt.Sprintf("%.2f KB", float64(size)/1024)
} else if size < 1024*1024*1024 {
return fmt.Sprintf("%.2f MB", float64(size)/(1024*1024))
}
return fmt.Sprintf("%.2f GB", float64(size)/(1024*1024*1024))
}
func HashFile(file io.Reader, extension string, full bool) (string, error) {
hasher := md5.New()
if _, err := io.Copy(hasher, file); err != nil {
return "", err
}
sha1Hash := strings.ToUpper(hex.EncodeToString(hasher.Sum(nil)))
filename := fmt.Sprintf("%s%s", sha1Hash, extension)
if full {
return filename, nil
} else {
return fmt.Sprintf("%s%s", sha1Hash[:5], extension), nil
}
}
func SaveFile(name string, file io.Reader) error {
dst, err := os.Create(name)
if err != nil {
return err
}
defer dst.Close()
if _, err := io.Copy(dst, file); err != nil {
return err
}
return nil
}
func BasicAuth(next http.HandlerFunc, app *Application) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
username, password, ok := r.BasicAuth()
if ok {
// hash password received
usernameHash := sha256.Sum256([]byte(username))
passwordHash := sha256.Sum256([]byte(password))
// hash our password
expectedUsernameHash := sha256.Sum256([]byte(app.auth.username))
expectedPasswordHash := sha256.Sum256([]byte(app.auth.password))
// compare hashes
usernameMatch := (subtle.ConstantTimeCompare(usernameHash[:], expectedUsernameHash[:]) == 1)
passwordMatch := (subtle.ConstantTimeCompare(passwordHash[:], expectedPasswordHash[:]) == 1)
if usernameMatch && passwordMatch {
next.ServeHTTP(w, r)
return
}
}
w.Header().Set("WWW-Authenticate", `Basic realm="restricted", charset="UTF-8`)
http.Error(w, "Unauthorized", http.StatusUnauthorized)
})
}
func ResponseURLHandler(w http.ResponseWriter, url, filename string) {
pasteURL := fmt.Sprintf("http://%s/%s\n", url, filename)
w.Header().Set("Location", pasteURL)
w.WriteHeader(http.StatusCreated)
fmt.Fprintf(w, "%s", pasteURL)
}

35
static/index.html Normal file
View File

@ -0,0 +1,35 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Abyss - A minimalist pastebin webserver" />
<title>abyss paste</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<h1>abyss paste</h1>
<div>
<a href="/tree">
<button>all uploaded files</button>
</a>
<a href="/last">
<button>last uploaded file</button>
</a>
</div>
<form action="/" method="POST">
<textarea name="content" placeholder="Enter your content here..."></textarea>
<br />
<button type="submit">upload</button>
</form>
<footer>
abyss paste - powered by
<a href="https://github.com/jabuxas/abyss" target="_blank">abyss</a>
</footer>
</body>
</html>

72
static/style.css Normal file
View File

@ -0,0 +1,72 @@
body {
font-family: Arial, sans-serif;
background-color: #1d1f21;
color: #c5c6c7;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
margin: 0;
}
h1 {
font-size: 3rem;
margin-bottom: 1rem;
}
button {
padding: 0.75rem 2rem;
background-color: #66fcf1;
border: none;
border-radius: 5px;
color: #1f2833;
font-size: 1rem;
cursor: pointer;
transition: background-color 0.3s;
}
button:hover {
background-color: #45a29e;
}
footer {
position: absolute;
bottom: 1rem;
font-size: 0.8rem;
color: #777;
}
footer a {
color: #66fcf1;
text-decoration: none;
font-weight: bold;
}
div {
display: flex;
gap: 10px;
align-items: center;
}
form {
display: flex;
flex-direction: column;
align-items: center;
margin-top: 30px;
gap: 10px;
}
textarea {
background-color: #333;
color: #fff;
border: 1px solid #555;
padding: 10px;
width: 400px;
height: 150px;
border-radius: 5px;
}
textarea::placeholder {
color: #bbb;
}

89
templates/dirlist.html Normal file
View File

@ -0,0 +1,89 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>dir listing</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
h1 {
font-size: 24px;
margin-bottom: 10px;
}
header {
display: flex;
justify-content: center;
align-items: center;
}
.file-listing {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
.file-listing th,
.file-listing td {
border: 1px solid #ddd;
padding: 12px;
text-align: left;
}
.file-listing th {
background-color: #333;
color: white;
}
.file-listing tr:nth-child(even) {
background-color: #f9f9f9;
}
.file-listing tr:hover {
background-color: #f1f1f1;
}
.file-listing td a {
color: #0288d1;
text-decoration: none;
}
.file-listing td a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<header>
<h1>{{ .URL }}</h1>
</header>
<table class="file-listing">
<thead>
<tr>
<th>Name</th>
<th>Time Uploaded</th>
<th>Size</th>
</tr>
</thead>
<tbody>
{{range .Files}}
<tr>
<td>
<a href="{{.Path}}">{{.Name}}</a>
</td>
<td>{{.TimeUploaded}}</td>
<td>{{.FormattedSize}}</td>
</tr>
{{end}}
</tbody>
</table>
</body>
</html>

228
templates/files.html Normal file
View File

@ -0,0 +1,228 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>abyss paste</title>
{{if eq .Type "text"}}
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
<script>
window.onload = function () {
var allPre, i, j;
allPre = document.getElementsByTagName("pre");
for (i = 0, j = allPre.length; i < j; i++) {
hljs.highlightBlock(allPre[i]);
}
};
</script>
<style>
pre.hljs {
display: block;
overflow-x: auto;
padding: 1em;
}
.hljs {
color: #ffffff;
}
.hljs-comment {
color: #7d7d7d;
}
.hljs-punctuation,
.hljs-tag {
color: #dcdcdc;
}
.hljs-tag .hljs-attr,
.hljs-tag .hljs-name {
color: #f1f1f1;
}
.hljs-attribute,
.hljs-doctag,
.hljs-keyword,
.hljs-meta .hljs-keyword,
.hljs-name,
.hljs-selector-tag {
font-weight: bold;
color: #ff9d00;
}
.hljs-deletion,
.hljs-number,
.hljs-quote,
.hljs-selector-class,
.hljs-selector-id,
.hljs-string,
.hljs-template-tag,
.hljs-type {
color: #d19a66;
}
.hljs-section,
.hljs-title {
color: #61afef;
font-weight: bold;
}
.hljs-link,
.hljs-operator,
.hljs-regexp,
.hljs-selector-attr,
.hljs-selector-pseudo,
.hljs-symbol,
.hljs-template-variable,
.hljs-variable {
color: #c678dd;
}
.hljs-literal {
color: #dcaeea;
}
.hljs-addition,
.hljs-built_in,
.hljs-bullet,
.hljs-code {
color: #98c379;
}
.hljs-meta {
color: #56b6c2;
}
.hljs-meta .hljs-string {
color: #e5c07b;
}
.hljs-emphasis {
font-style: italic;
}
.hljs-strong {
font-weight: bold;
}
</style>
{{end}}
<style>
body {
margin: 0;
padding: 0;
background-color: #1d1f21;
color: #c5c6c7;
font-family: "Arial", sans-serif;
display: flex;
flex-direction: column;
height: 100vh;
}
header,
footer {
background-color: #2e2e2e;
text-align: center;
font-size: 1rem;
font-weight: bold;
position: sticky;
top: 0;
z-index: 10;
padding: 10px;
}
.content {
flex-grow: 1;
display: flex;
justify-content: center;
align-items: center;
padding: 10px;
}
img,
video,
embed,
iframe {
max-width: 100%;
max-height: 100%;
border-radius: 8px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}
video {
background-color: #000;
}
.pdf-embed {
width: 100%;
height: 100%;
border: none;
}
pre {
flex: 1;
width: 100%;
height: 100%;
white-space: pre;
font-family: monospace;
font-size: 1rem;
background-color: #2e2e2e;
padding: 10px;
border-radius: 8px;
overflow: auto;
scrollbar-width: thin;
scrollbar-color: #686868 #2e2e2e;
box-sizing: border-box;
}
pre::-webkit-scrollbar {
width: 12px;
}
pre::-webkit-scrollbar-track {
background: #2e2e2e;
border-radius: 10px;
}
pre::-webkit-scrollbar-thumb {
background-color: #686868;
border-radius: 10px;
border: 3px solid #2e2e2e;
}
a {
color: #0288d1;
text-decoration: none;
}
</style>
</head>
<body>
<header>{{.Path}}</header>
<div class="content">
{{if eq .Type "text"}}
<pre>{{.Content}}</pre>
{{else if eq .Type "image"}}
<img src="{{.Name}}" alt="Image" />
{{else if eq .Type "pdf"}}
<embed src="{{.Name}}" type="application/pdf" class="pdf-embed" />
{{else if eq .Type "video"}}
<video controls>
<source src="{{.Name}}" type="video/mp4" />
Your browser does not support the video tag.
</video>
{{else if eq .Type "audio"}}
<audio controls src="{{.Name}}"><audio />
{{else}}
<p>
Couldn't detect file from extension, visit
<a href="http://{{.Path}}">this link</a> to see/download your file.
</p>
{{end}}
</div>
<footer>file uploaded in {{.TimeUploaded}}</footer>
</body>
</html>