Compare commits
6 Commits
v1.1.0
...
551a5b2b2a
| Author | SHA1 | Date | |
|---|---|---|---|
| 551a5b2b2a | |||
| 1a567a19fe | |||
| 9d1ca16704 | |||
| 25eee6f76a | |||
| 5d7c324fb7 | |||
| a0ee2db58f |
5
.dockerignore
Normal file
5
.dockerignore
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
.git
|
||||||
|
.github
|
||||||
|
*.md
|
||||||
|
library/
|
||||||
|
*_test.go
|
||||||
185
.github/workflows/release.yaml
vendored
185
.github/workflows/release.yaml
vendored
@@ -1,4 +1,4 @@
|
|||||||
name: Build and Release
|
name: Release
|
||||||
|
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
@@ -6,185 +6,32 @@ on:
|
|||||||
- 'v*'
|
- 'v*'
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build-and-release-linux:
|
release:
|
||||||
name: Build and Release for Linux
|
name: Build and Release
|
||||||
runs-on: [self-hosted, linux, arm64]
|
runs-on: [self-hosted, linux, arm64]
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout code
|
- name: Checkout
|
||||||
uses: actions/checkout@v3
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
- name: Set up Go
|
- name: Set up Go
|
||||||
uses: actions/setup-go@v3
|
uses: actions/setup-go@v5
|
||||||
with:
|
with:
|
||||||
go-version: '1.22.3'
|
go-version: '1.22.3'
|
||||||
|
|
||||||
- name: Build for Linux
|
- name: Build
|
||||||
run: go build -o myapp
|
run: make all
|
||||||
|
|
||||||
- name: Create Release
|
- name: Release
|
||||||
id: create_release
|
uses: softprops/action-gh-release@v2
|
||||||
uses: actions/create-release@v1
|
|
||||||
env:
|
|
||||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
||||||
with:
|
with:
|
||||||
tag_name: ${{ github.ref_name }}
|
tag_name: ${{ github.ref_name }}
|
||||||
release_name: Release ${{ github.ref_name }}
|
name: ${{ github.ref_name }}
|
||||||
body: Release notes for ${{ github.ref_name }}
|
files: build/*
|
||||||
draft: false
|
generate_release_notes: true
|
||||||
prerelease: false
|
|
||||||
|
|
||||||
- name: Save upload_url
|
|
||||||
run: echo "UPLOAD_URL=${{ steps.create_release.outputs.upload_url }}" > upload_url.txt
|
|
||||||
shell: bash
|
|
||||||
|
|
||||||
- name: Upload upload_url artifact
|
|
||||||
uses: actions/upload-artifact@v3
|
|
||||||
with:
|
|
||||||
name: upload_url
|
|
||||||
path: upload_url.txt
|
|
||||||
|
|
||||||
build-and-release-windows:
|
|
||||||
name: Build and Release for Windows
|
|
||||||
runs-on: windows-latest
|
|
||||||
needs: build-and-release-linux
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- name: Checkout code
|
|
||||||
uses: actions/checkout@v3
|
|
||||||
|
|
||||||
- name: Set up Go
|
|
||||||
uses: actions/setup-go@v3
|
|
||||||
with:
|
|
||||||
go-version: '1.22.3'
|
|
||||||
|
|
||||||
- name: Build for Windows
|
|
||||||
run: go build -o myapp.exe
|
|
||||||
|
|
||||||
- name: Download upload_url artifact
|
|
||||||
uses: actions/download-artifact@v3
|
|
||||||
with:
|
|
||||||
name: upload_url
|
|
||||||
path: .
|
|
||||||
|
|
||||||
- name: Read upload_url
|
|
||||||
id: read_upload_url
|
|
||||||
run: |
|
|
||||||
UPLOAD_URL=$(cat upload_url.txt)
|
|
||||||
echo "::set-output name=upload_url::$UPLOAD_URL"
|
|
||||||
shell: bash
|
|
||||||
|
|
||||||
- name: Debug upload_url
|
|
||||||
run: |
|
|
||||||
echo "Upload URL: ${{ steps.read_upload_url.outputs.upload_url }}"
|
|
||||||
|
|
||||||
- name: Upload Windows Release Asset
|
|
||||||
if: startsWith(github.ref, 'refs/tags/')
|
|
||||||
uses: actions/upload-release-asset@v1
|
|
||||||
env:
|
env:
|
||||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
with:
|
|
||||||
upload_url: ${{ steps.read_upload_url.outputs.upload_url }}
|
|
||||||
asset_path: ./myapp.exe
|
|
||||||
asset_name: myapp-windows.exe
|
|
||||||
asset_content_type: application/octet-stream
|
|
||||||
|
|
||||||
|
- name: Clean up
|
||||||
# name: Yoink Release
|
if: always()
|
||||||
|
run: make clean
|
||||||
# on:
|
|
||||||
# push:
|
|
||||||
# tags:
|
|
||||||
# - 'v*'
|
|
||||||
|
|
||||||
# jobs:
|
|
||||||
# yoink-release:
|
|
||||||
# name: Yoink Build and Release
|
|
||||||
# runs-on: [self-hosted, linux, arm64]
|
|
||||||
|
|
||||||
# steps:
|
|
||||||
# - name: Checkout
|
|
||||||
# uses: actions/checkout@v3
|
|
||||||
|
|
||||||
# - name: Setup Go
|
|
||||||
# uses: actions/setup-go@v4
|
|
||||||
# with:
|
|
||||||
# go-version: '1.22.3'
|
|
||||||
|
|
||||||
# - name: Linux Build
|
|
||||||
# run: go build -o yoink
|
|
||||||
|
|
||||||
# - name: Create Release
|
|
||||||
# id: create_release
|
|
||||||
# uses: actions/create-release@v1
|
|
||||||
# env:
|
|
||||||
# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
||||||
# with:
|
|
||||||
# tag_name: ${{ github.ref_name }}
|
|
||||||
# release_name: Release ${{ github.ref_name }}
|
|
||||||
# body: Release notes for ${{ github.ref_name }}
|
|
||||||
# draft: false
|
|
||||||
# prerelease: false
|
|
||||||
|
|
||||||
# - name: Save upload_url
|
|
||||||
# run: echo "UPLOAD_URL=${{ steps.create_release.outputs.upload_url }}" > upload_url.txt
|
|
||||||
# shell: bash
|
|
||||||
|
|
||||||
# - name: Upload upload_url artifact
|
|
||||||
# uses: actions/upload-artifact@v3
|
|
||||||
# with:
|
|
||||||
# name: upload_url.txt
|
|
||||||
# path: ./upload_url.txt
|
|
||||||
|
|
||||||
# - name: Upload Linux Release Asset
|
|
||||||
# uses: actions/upload-release-asset@v1
|
|
||||||
# env:
|
|
||||||
# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
||||||
# with:
|
|
||||||
# upload_url: ${{ steps.create_release.outputs.upload_url }}
|
|
||||||
# asset_path: ./yoink
|
|
||||||
# asset_name: yoink
|
|
||||||
# asset_content_type: application/x-executable
|
|
||||||
|
|
||||||
# build-and-release-win:
|
|
||||||
# runs-on: windows-latest
|
|
||||||
# needs: yoink-release
|
|
||||||
|
|
||||||
# steps:
|
|
||||||
# - name: Checkout
|
|
||||||
# uses: actions/checkout@v3
|
|
||||||
|
|
||||||
# - name: Setup Go
|
|
||||||
# uses: actions/setup-go@v4
|
|
||||||
# with:
|
|
||||||
# go-version: '1.22.3'
|
|
||||||
|
|
||||||
# - name: Windows Build
|
|
||||||
# run: go build -o yoink.exe
|
|
||||||
|
|
||||||
# - name: Download upload_url artifact
|
|
||||||
# uses: actions/download-artifact@v3
|
|
||||||
# with:
|
|
||||||
# name: upload_url.txt
|
|
||||||
# path: .
|
|
||||||
|
|
||||||
# - name: Read upload_url artifact
|
|
||||||
# id: read_upload_url
|
|
||||||
# run: |
|
|
||||||
# UPLOAD_URL=$(cat upload_url.txt)
|
|
||||||
# echo "::set-output name=upload_url::$UPLOAD_URL"
|
|
||||||
# shell: bash
|
|
||||||
|
|
||||||
# - name: Debug upload_url
|
|
||||||
# run: echo ${{ steps.read_upload_url.outputs.upload_url }}
|
|
||||||
# shell: bash
|
|
||||||
|
|
||||||
# - name: Upload Windows Release Asset
|
|
||||||
# uses: actions/upload-release-asset@v1
|
|
||||||
# env:
|
|
||||||
# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
||||||
# with:
|
|
||||||
# upload_url: ${{ steps.read_upload_url.outputs.upload_url }}
|
|
||||||
# asset_path: ./yoink.exe
|
|
||||||
# asset_name: yoink.exe
|
|
||||||
# asset_content_type: application/x-executable
|
|
||||||
|
|||||||
11
.gitignore
vendored
11
.gitignore
vendored
@@ -20,3 +20,14 @@ go.work.sum
|
|||||||
|
|
||||||
# env file
|
# env file
|
||||||
.env
|
.env
|
||||||
|
|
||||||
|
# Built binary
|
||||||
|
yoink
|
||||||
|
yoink.exe
|
||||||
|
|
||||||
|
# Comic library (downloaded content)
|
||||||
|
library/
|
||||||
|
|
||||||
|
# IDE
|
||||||
|
.vscode/
|
||||||
|
.idea/
|
||||||
37
Dockerfile
Normal file
37
Dockerfile
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
# ── Build stage ────────────────────────────────────────────────────────────
|
||||||
|
FROM mcr.microsoft.com/oss/go/microsoft/golang:1.22-bullseye AS builder
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
# Restore modules in a separate layer so it's cached until go.mod/go.sum change
|
||||||
|
COPY go.mod go.sum ./
|
||||||
|
RUN go mod download && go mod verify
|
||||||
|
|
||||||
|
# Copy source and build a fully static binary
|
||||||
|
COPY . .
|
||||||
|
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
|
||||||
|
go build -ldflags="-s -w" -trimpath -o yoink .
|
||||||
|
|
||||||
|
# ── Runtime stage ──────────────────────────────────────────────────────────
|
||||||
|
# distroless/base-debian12:nonroot — minimal attack surface, non-root by default
|
||||||
|
FROM gcr.io/distroless/base-debian12:nonroot
|
||||||
|
|
||||||
|
LABEL org.opencontainers.image.title="yoink" \
|
||||||
|
org.opencontainers.image.description="Comic downloader web UI" \
|
||||||
|
org.opencontainers.image.source="https://git.brizzle.dev/bryan/yoink-go"
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
COPY --from=builder --chown=nonroot:nonroot /app/yoink .
|
||||||
|
|
||||||
|
ENV YOINK_LIBRARY=/library
|
||||||
|
|
||||||
|
VOLUME ["/library"]
|
||||||
|
EXPOSE 8080
|
||||||
|
|
||||||
|
HEALTHCHECK --interval=30s --timeout=5s --start-period=5s --retries=3 \
|
||||||
|
CMD ["/app/yoink", "healthcheck"]
|
||||||
|
|
||||||
|
USER nonroot
|
||||||
|
|
||||||
|
CMD ["/app/yoink", "serve"]
|
||||||
14
Makefile
14
Makefile
@@ -1,7 +1,9 @@
|
|||||||
BIN := yoink
|
BIN := yoink
|
||||||
BUILD_DIR := build
|
BUILD_DIR := build
|
||||||
|
REGISTRY := git.brizzle.dev/bryan/yoink-go
|
||||||
|
VERSION := $(shell git describe --tags --always --dirty)
|
||||||
|
|
||||||
.PHONY: all windows linux darwin clean
|
.PHONY: all windows linux darwin clean docker-build docker-push
|
||||||
|
|
||||||
all: windows linux darwin
|
all: windows linux darwin
|
||||||
|
|
||||||
@@ -16,5 +18,15 @@ darwin:
|
|||||||
GOOS=darwin GOARCH=amd64 go build -o $(BUILD_DIR)/$(BIN)-darwin-amd64
|
GOOS=darwin GOARCH=amd64 go build -o $(BUILD_DIR)/$(BIN)-darwin-amd64
|
||||||
GOOS=darwin GOARCH=arm64 go build -o $(BUILD_DIR)/$(BIN)-darwin-arm64
|
GOOS=darwin GOARCH=arm64 go build -o $(BUILD_DIR)/$(BIN)-darwin-arm64
|
||||||
|
|
||||||
|
docker-build:
|
||||||
|
podman build --format docker \
|
||||||
|
-t $(REGISTRY):$(VERSION) \
|
||||||
|
-t $(REGISTRY):latest \
|
||||||
|
.
|
||||||
|
|
||||||
|
docker-push: docker-build
|
||||||
|
podman push $(REGISTRY):$(VERSION)
|
||||||
|
podman push $(REGISTRY):latest
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -rf $(BUILD_DIR)
|
rm -rf $(BUILD_DIR)
|
||||||
|
|||||||
93
README.md
93
README.md
@@ -1,6 +1,6 @@
|
|||||||
# yoink
|
# yoink
|
||||||
|
|
||||||
A CLI tool for downloading comics from readallcomics.com and packaging them as `.cbz` archives.
|
A tool for downloading comics from readallcomics.com and packaging them as `.cbz` archives. Available as a CLI command or a self-hosted web application.
|
||||||
|
|
||||||
## How it works
|
## How it works
|
||||||
|
|
||||||
@@ -9,17 +9,33 @@ A CLI tool for downloading comics from readallcomics.com and packaging them as `
|
|||||||
3. Packages the images into a `.cbz` (Comic Book Zip) archive
|
3. Packages the images into a `.cbz` (Comic Book Zip) archive
|
||||||
4. Cleans up downloaded images, keeping only the cover (`001`)
|
4. Cleans up downloaded images, keeping only the cover (`001`)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
Build from source (requires Go 1.22.3+):
|
### From source
|
||||||
|
|
||||||
|
Requires Go 1.22.3+:
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
go build -o yoink
|
go build -o yoink
|
||||||
```
|
```
|
||||||
|
|
||||||
Pre-built binaries for Linux (arm64) and Windows are available on the [releases page](../../releases).
|
### Pre-built binaries
|
||||||
|
|
||||||
## Usage
|
Pre-built binaries for Linux (arm64) and Windows are available on the [releases page](https://git.brizzle.dev/bryan/yoink-go/releases).
|
||||||
|
|
||||||
|
### Docker
|
||||||
|
|
||||||
|
```shell
|
||||||
|
docker pull git.brizzle.dev/bryan/yoink-go:latest
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## CLI
|
||||||
|
|
||||||
|
Download a single comic issue:
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
yoink <url>
|
yoink <url>
|
||||||
@@ -37,16 +53,79 @@ The comic title is extracted from the page and used to name the archive. Output
|
|||||||
<library>/<Title>/<Title>.cbz
|
<library>/<Title>/<Title>.cbz
|
||||||
```
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Web UI
|
||||||
|
|
||||||
|
Yoink includes a self-hosted web interface for browsing and downloading comics from your browser.
|
||||||
|
|
||||||
|
### Running directly
|
||||||
|
|
||||||
|
```shell
|
||||||
|
yoink serve
|
||||||
|
```
|
||||||
|
|
||||||
|
By default the server listens on port `8080`. Use the `-p` flag to change it:
|
||||||
|
|
||||||
|
```shell
|
||||||
|
yoink serve -p 3000
|
||||||
|
```
|
||||||
|
|
||||||
|
### Running with Docker
|
||||||
|
|
||||||
|
A `docker-compose.yml` is included for quick deployment:
|
||||||
|
|
||||||
|
```shell
|
||||||
|
docker compose up -d
|
||||||
|
```
|
||||||
|
|
||||||
|
Or with Podman:
|
||||||
|
|
||||||
|
```shell
|
||||||
|
podman compose up -d
|
||||||
|
```
|
||||||
|
|
||||||
|
The web UI is then available at `http://localhost:8080`.
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
- **Download queue** — paste a comic URL into the input bar and track download progress in real time
|
||||||
|
- **Library grid** — browse your downloaded comics as a 150×300 cover grid
|
||||||
|
- **Filter & sort** — filter by title and sort by newest, oldest, A–Z, or Z–A
|
||||||
|
- **One-click download** — click any cover to download the `.cbz` archive directly
|
||||||
|
|
||||||
|
### Library volume
|
||||||
|
|
||||||
|
Downloaded comics are stored at the path set by `YOINK_LIBRARY`. When using Docker, mount this as a volume to persist your library across container restarts:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
# docker-compose.yml
|
||||||
|
services:
|
||||||
|
yoink:
|
||||||
|
image: git.brizzle.dev/bryan/yoink-go:latest
|
||||||
|
ports:
|
||||||
|
- "8080:8080"
|
||||||
|
volumes:
|
||||||
|
- ./library:/library
|
||||||
|
environment:
|
||||||
|
- YOINK_LIBRARY=/library
|
||||||
|
restart: unless-stopped
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
## Configuration
|
## Configuration
|
||||||
|
|
||||||
| Variable | Default | Description |
|
| Variable | Default | Description |
|
||||||
|-----------------|--------------|--------------------------------------|
|
|-----------------|------------|-----------------------------------|
|
||||||
| `YOINK_LIBRARY` | `~/.yoink` | Directory where comics are stored |
|
| `YOINK_LIBRARY` | `~/.yoink` | Directory where comics are stored |
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
YOINK_LIBRARY=/mnt/media/comics yoink https://readallcomics.com/some-comic-001/
|
YOINK_LIBRARY=/mnt/media/comics yoink https://readallcomics.com/some-comic-001/
|
||||||
```
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
## Dependencies
|
## Dependencies
|
||||||
|
|
||||||
- [goquery](https://github.com/PuerkitoBio/goquery) — HTML parsing
|
- [goquery](https://github.com/PuerkitoBio/goquery) — HTML parsing
|
||||||
|
|||||||
28
cli/healthcheck.go
Normal file
28
cli/healthcheck.go
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
package cli
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
var healthcheckCmd = &cobra.Command{
|
||||||
|
Use: "healthcheck",
|
||||||
|
Short: "Check if the web server is running (used by Docker HEALTHCHECK)",
|
||||||
|
Args: cobra.NoArgs,
|
||||||
|
Hidden: true,
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
port, _ := cmd.Flags().GetString("port")
|
||||||
|
resp, err := http.Get(fmt.Sprintf("http://localhost:%s/health", port))
|
||||||
|
if err != nil || resp.StatusCode != http.StatusOK {
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
healthcheckCmd.Flags().StringP("port", "p", "8080", "Port the server is listening on")
|
||||||
|
cli.AddCommand(healthcheckCmd)
|
||||||
|
}
|
||||||
36
cli/serve.go
Normal file
36
cli/serve.go
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
package cli
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
"yoink/web"
|
||||||
|
)
|
||||||
|
|
||||||
|
var serveCmd = &cobra.Command{
|
||||||
|
Use: "serve",
|
||||||
|
Short: "Start the Yoink web UI",
|
||||||
|
Args: cobra.NoArgs,
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
library, ok := os.LookupEnv("YOINK_LIBRARY")
|
||||||
|
if !ok {
|
||||||
|
userHome, _ := os.UserHomeDir()
|
||||||
|
library = filepath.Join(userHome, ".yoink")
|
||||||
|
}
|
||||||
|
|
||||||
|
port, _ := cmd.Flags().GetString("port")
|
||||||
|
addr := fmt.Sprintf(":%s", port)
|
||||||
|
|
||||||
|
if err := web.Listen(addr, library); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
serveCmd.Flags().StringP("port", "p", "8080", "Port to listen on")
|
||||||
|
cli.AddCommand(serveCmd)
|
||||||
|
}
|
||||||
10
docker-compose.yml
Normal file
10
docker-compose.yml
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
services:
|
||||||
|
yoink:
|
||||||
|
build: .
|
||||||
|
ports:
|
||||||
|
- "0.0.0.0:8080:8080"
|
||||||
|
volumes:
|
||||||
|
- ./library:/library
|
||||||
|
environment:
|
||||||
|
- YOINK_LIBRARY=/library
|
||||||
|
restart: unless-stopped
|
||||||
247
web/server.go
Normal file
247
web/server.go
Normal file
@@ -0,0 +1,247 @@
|
|||||||
|
package web
|
||||||
|
|
||||||
|
import (
|
||||||
|
"embed"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io/fs"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"sort"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/PuerkitoBio/goquery"
|
||||||
|
"yoink/comic"
|
||||||
|
)
|
||||||
|
|
||||||
|
//go:embed static
|
||||||
|
var staticFiles embed.FS
|
||||||
|
|
||||||
|
type JobStatus string
|
||||||
|
|
||||||
|
const (
|
||||||
|
StatusPending JobStatus = "pending"
|
||||||
|
StatusRunning JobStatus = "running"
|
||||||
|
StatusComplete JobStatus = "complete"
|
||||||
|
StatusError JobStatus = "error"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Job struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
URL string `json:"url"`
|
||||||
|
Title string `json:"title"`
|
||||||
|
Status JobStatus `json:"status"`
|
||||||
|
Error string `json:"error,omitempty"`
|
||||||
|
CreatedAt time.Time `json:"created_at"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type ComicEntry struct {
|
||||||
|
Title string `json:"title"`
|
||||||
|
CoverURL string `json:"cover_url"`
|
||||||
|
FileURL string `json:"file_url"`
|
||||||
|
DownloadedAt time.Time `json:"downloaded_at"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Server struct {
|
||||||
|
libraryPath string
|
||||||
|
jobs map[string]*Job
|
||||||
|
mu sync.RWMutex
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewServer(libraryPath string) *Server {
|
||||||
|
return &Server{
|
||||||
|
libraryPath: libraryPath,
|
||||||
|
jobs: make(map[string]*Job),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) Handler() http.Handler {
|
||||||
|
mux := http.NewServeMux()
|
||||||
|
|
||||||
|
// Embedded static assets
|
||||||
|
staticFS, _ := fs.Sub(staticFiles, "static")
|
||||||
|
mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.FS(staticFS))))
|
||||||
|
|
||||||
|
// Library files: covers (inline) and cbz downloads (attachment)
|
||||||
|
mux.Handle("/covers/", http.StripPrefix("/covers/", http.FileServer(http.Dir(s.libraryPath))))
|
||||||
|
mux.Handle("/files/", http.StripPrefix("/files/", s.downloadHandler()))
|
||||||
|
|
||||||
|
// API
|
||||||
|
mux.HandleFunc("/api/download", s.handleDownload)
|
||||||
|
mux.HandleFunc("/api/comics", s.handleComics)
|
||||||
|
mux.HandleFunc("/api/jobs", s.handleJobs)
|
||||||
|
mux.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
})
|
||||||
|
|
||||||
|
// SPA root
|
||||||
|
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.URL.Path != "/" {
|
||||||
|
http.NotFound(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
data, _ := staticFiles.ReadFile("static/index.html")
|
||||||
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||||
|
w.Write(data)
|
||||||
|
})
|
||||||
|
|
||||||
|
return mux
|
||||||
|
}
|
||||||
|
|
||||||
|
// downloadHandler wraps the library file server to force Content-Disposition: attachment.
|
||||||
|
func (s *Server) downloadHandler() http.Handler {
|
||||||
|
fs := http.FileServer(http.Dir(s.libraryPath))
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Header().Set("Content-Disposition", "attachment")
|
||||||
|
fs.ServeHTTP(w, r)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) handleDownload(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method != http.MethodPost {
|
||||||
|
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var req struct {
|
||||||
|
URL string `json:"url"`
|
||||||
|
}
|
||||||
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil || strings.TrimSpace(req.URL) == "" {
|
||||||
|
http.Error(w, "invalid request", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
job := &Job{
|
||||||
|
ID: fmt.Sprintf("%d", time.Now().UnixNano()),
|
||||||
|
URL: req.URL,
|
||||||
|
Status: StatusPending,
|
||||||
|
CreatedAt: time.Now(),
|
||||||
|
}
|
||||||
|
|
||||||
|
s.mu.Lock()
|
||||||
|
s.jobs[job.ID] = job
|
||||||
|
s.mu.Unlock()
|
||||||
|
|
||||||
|
go s.runJob(job)
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
json.NewEncoder(w).Encode(job)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) runJob(job *Job) {
|
||||||
|
s.mu.Lock()
|
||||||
|
job.Status = StatusRunning
|
||||||
|
s.mu.Unlock()
|
||||||
|
|
||||||
|
markupCh := make(chan *goquery.Document)
|
||||||
|
imageCh := make(chan []string)
|
||||||
|
|
||||||
|
c := comic.NewComic(job.URL, s.libraryPath, imageCh, markupCh)
|
||||||
|
|
||||||
|
s.mu.Lock()
|
||||||
|
job.Title = c.Title
|
||||||
|
s.mu.Unlock()
|
||||||
|
|
||||||
|
errs := c.Download(len(c.Filelist))
|
||||||
|
if len(errs) > 0 {
|
||||||
|
s.mu.Lock()
|
||||||
|
job.Status = StatusError
|
||||||
|
job.Error = errs[0].Error()
|
||||||
|
s.mu.Unlock()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := c.Archive(); err != nil {
|
||||||
|
s.mu.Lock()
|
||||||
|
job.Status = StatusError
|
||||||
|
job.Error = err.Error()
|
||||||
|
s.mu.Unlock()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.Cleanup()
|
||||||
|
|
||||||
|
s.mu.Lock()
|
||||||
|
job.Status = StatusComplete
|
||||||
|
s.mu.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) handleComics(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
|
||||||
|
entries := []ComicEntry{}
|
||||||
|
|
||||||
|
dirs, err := os.ReadDir(s.libraryPath)
|
||||||
|
if err != nil {
|
||||||
|
json.NewEncoder(w).Encode(entries)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, dir := range dirs {
|
||||||
|
if !dir.IsDir() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
title := dir.Name()
|
||||||
|
dirPath := filepath.Join(s.libraryPath, title)
|
||||||
|
|
||||||
|
var coverURL, fileURL string
|
||||||
|
var downloadedAt time.Time
|
||||||
|
|
||||||
|
files, _ := os.ReadDir(dirPath)
|
||||||
|
for _, f := range files {
|
||||||
|
name := f.Name()
|
||||||
|
|
||||||
|
if strings.HasSuffix(name, ".cbz") {
|
||||||
|
fileURL = "/files/" + url.PathEscape(title) + "/" + url.PathEscape(name)
|
||||||
|
if info, err := f.Info(); err == nil {
|
||||||
|
downloadedAt = info.ModTime()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cover kept by Cleanup: "<Title> 001.jpg"
|
||||||
|
stripped := strings.TrimSpace(strings.TrimPrefix(name, title))
|
||||||
|
if strings.HasPrefix(strings.ToLower(stripped), "001") {
|
||||||
|
coverURL = "/covers/" + url.PathEscape(title) + "/" + url.PathEscape(name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if fileURL != "" {
|
||||||
|
entries = append(entries, ComicEntry{
|
||||||
|
Title: title,
|
||||||
|
CoverURL: coverURL,
|
||||||
|
FileURL: fileURL,
|
||||||
|
DownloadedAt: downloadedAt,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Default: newest first
|
||||||
|
sort.Slice(entries, func(i, j int) bool {
|
||||||
|
return entries[i].DownloadedAt.After(entries[j].DownloadedAt)
|
||||||
|
})
|
||||||
|
|
||||||
|
json.NewEncoder(w).Encode(entries)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) handleJobs(w http.ResponseWriter, r *http.Request) {
|
||||||
|
s.mu.RLock()
|
||||||
|
jobs := make([]*Job, 0, len(s.jobs))
|
||||||
|
for _, j := range s.jobs {
|
||||||
|
jobs = append(jobs, j)
|
||||||
|
}
|
||||||
|
s.mu.RUnlock()
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
json.NewEncoder(w).Encode(jobs)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Listen(addr string, libraryPath string) error {
|
||||||
|
srv := NewServer(libraryPath)
|
||||||
|
fmt.Printf("Yoink web server listening on %s\n", addr)
|
||||||
|
return http.ListenAndServe(addr, srv.Handler())
|
||||||
|
}
|
||||||
1026
web/static/index.html
Normal file
1026
web/static/index.html
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user