Compare commits
68 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
070d4a862c | ||
| 6543d3a170 | |||
| 0e08b1669c | |||
|
f4310ed688
|
|||
|
1c82b619c4
|
|||
|
6e016b802b
|
|||
| 78ed401392 | |||
|
72b85ec281
|
|||
|
b2e3268ad1
|
|||
|
1af43b111c
|
|||
|
03312a0079
|
|||
|
a91163f845
|
|||
| 7d28d1cea8 | |||
| 3fda737af2 | |||
|
8cf3d4dda8
|
|||
|
2e18921a27
|
|||
|
f1efb1d67c
|
|||
|
457ede5b62
|
|||
|
7eb724d01f
|
|||
|
189bbb0874
|
|||
|
68da5f9658
|
|||
|
83cd0b722b
|
|||
| ca176e1a76 | |||
| 54656f2630 | |||
| f38b0c69d9 | |||
|
b1ba08933a
|
|||
|
a049610291
|
|||
|
c46538a55f
|
|||
|
fe6b7c78f6
|
|||
| f9c2ac64d7 | |||
| 1f42b2a877 | |||
|
e03226a7ee
|
|||
|
f1015ab62e
|
|||
|
acf172933d
|
|||
|
99f75f1cd1
|
|||
| c7712982f3 | |||
|
bf78384fa8
|
|||
|
7445627f7e
|
|||
|
cc28f0f3c2
|
|||
|
ace79838fe
|
|||
|
8a63f73839
|
|||
|
da03138d5c
|
|||
|
0bae45a824
|
|||
|
7159bae9f7
|
|||
| 5b6e1e6b01 | |||
|
4b03c7c59b
|
|||
|
1dd8aa594d
|
|||
|
72889d3083
|
|||
|
bd87baa40a
|
|||
|
2f738413f3
|
|||
|
f6a447d7f4
|
|||
| 551e53ad63 | |||
|
707de8fcf1
|
|||
| 64a6eb20a0 | |||
|
67b17c1df7
|
|||
|
142c09e624
|
|||
| 0b3797dc19 | |||
| d4dae21d8f | |||
| 9f2677485e | |||
|
8aa915e6dc
|
|||
|
37c390f911
|
|||
|
2f9552e771
|
|||
|
dfe21445e5
|
|||
|
5397ba0907
|
|||
|
8c010665e1
|
|||
|
916d3004de
|
|||
|
7edf4ed9c5
|
|||
|
93d262d293
|
6
.gitignore
vendored
6
.gitignore
vendored
@@ -1 +1,7 @@
|
|||||||
config.toml
|
config.toml
|
||||||
|
drmdtool
|
||||||
|
drmdtool_*
|
||||||
|
src/DRMDTool
|
||||||
|
*.exe
|
||||||
|
uploads/
|
||||||
|
src/uploads/
|
||||||
|
|||||||
40
Makefile
Normal file
40
Makefile
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
GOCMD=go
|
||||||
|
GOBUILD=$(GOCMD) build
|
||||||
|
GOCLEAN=$(GOCMD) clean
|
||||||
|
GOTEST=$(GOCMD) test
|
||||||
|
GOGET=$(GOCMD) get
|
||||||
|
BINARY_NAME=drmdtool
|
||||||
|
SRC_DIR=src
|
||||||
|
|
||||||
|
all: test build
|
||||||
|
|
||||||
|
build:
|
||||||
|
cd $(SRC_DIR) && $(GOBUILD) -o ../$(BINARY_NAME) -v
|
||||||
|
|
||||||
|
test:
|
||||||
|
cd $(SRC_DIR) && $(GOTEST) -v ./...
|
||||||
|
|
||||||
|
clean:
|
||||||
|
$(GOCLEAN)
|
||||||
|
rm -f $(BINARY_NAME)
|
||||||
|
|
||||||
|
run:
|
||||||
|
cd $(SRC_DIR) && $(GOBUILD) -o ../$(BINARY_NAME) -v
|
||||||
|
./$(BINARY_NAME)
|
||||||
|
|
||||||
|
deps:
|
||||||
|
$(GOGET) github.com/BurntSushi/toml
|
||||||
|
$(GOGET) github.com/beevik/etree
|
||||||
|
$(GOGET) github.com/asticode/go-astisub
|
||||||
|
|
||||||
|
# Cross compilation
|
||||||
|
build-linux:
|
||||||
|
cd $(SRC_DIR) && CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(GOBUILD) -o ../$(BINARY_NAME)_linux -v
|
||||||
|
|
||||||
|
build-windows:
|
||||||
|
cd $(SRC_DIR) && CGO_ENABLED=0 GOOS=windows GOARCH=amd64 $(GOBUILD) -o ../$(BINARY_NAME).exe -v
|
||||||
|
|
||||||
|
build-mac:
|
||||||
|
cd $(SRC_DIR) && CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 $(GOBUILD) -o ../$(BINARY_NAME)_mac -v
|
||||||
|
|
||||||
|
.PHONY: all build test clean run deps build-linux build-windows build-mac
|
||||||
88
README.md
88
README.md
@@ -7,14 +7,76 @@ drmdtool is a utility for processing .drmd files using N_m3u8DL-RE.
|
|||||||
Create a `config.toml` file in the same directory as the drmdtool executable:
|
Create a `config.toml` file in the same directory as the drmdtool executable:
|
||||||
|
|
||||||
```toml
|
```toml
|
||||||
|
[General]
|
||||||
BaseDir = "/path/to/save/downloads"
|
BaseDir = "/path/to/save/downloads"
|
||||||
Format = "mkv"
|
Format = "mkv"
|
||||||
|
TempBaseDir = "/tmp/nre"
|
||||||
|
EnableConsole = true
|
||||||
|
MaxUploadMB = 32
|
||||||
|
|
||||||
[N_m3u8DL-RE]
|
[WatchFolder]
|
||||||
|
Path = "/path/to/watched/folder"
|
||||||
|
PollingInterval = 10
|
||||||
|
UsePolling = true
|
||||||
|
UseInotify = false
|
||||||
|
|
||||||
|
[N_m3u8DLRE]
|
||||||
Path = "/path/to/N_m3u8DL-RE"
|
Path = "/path/to/N_m3u8DL-RE"
|
||||||
|
|
||||||
|
[Server]
|
||||||
|
Host = "127.0.0.1"
|
||||||
|
Port = 8080
|
||||||
|
ReadTimeoutSec = 30
|
||||||
|
WriteTimeoutSec = 30
|
||||||
|
IdleTimeoutSec = 60
|
||||||
|
ReadHeaderTimeoutS = 10
|
||||||
|
|
||||||
|
[Security]
|
||||||
|
AuthToken = ""
|
||||||
```
|
```
|
||||||
|
|
||||||
Adjust the paths and format as needed. (mkv, mp4)
|
### Configuration Options
|
||||||
|
|
||||||
|
- **General**
|
||||||
|
- `BaseDir`: Directory where downloaded files will be saved.
|
||||||
|
- `Format`: Output format for the downloaded files (e.g., `mkv`, `mp4`).
|
||||||
|
- `TempBaseDir`: Temporary directory for intermediate files.
|
||||||
|
- `EnableConsole`: Boolean to enable or disable console output.
|
||||||
|
- `MaxUploadMB`: Maximum allowed upload size for the web UI.
|
||||||
|
|
||||||
|
- **WatchFolder**
|
||||||
|
- `Path`: Directory to watch for new `.drmd` files.
|
||||||
|
- `PollingInterval`: Interval in seconds for polling the watch folder.
|
||||||
|
- `UsePolling`: Boolean to enable or disable folder polling.
|
||||||
|
- `UseInotify`: Boolean to enable or disable inotify for file watching.
|
||||||
|
|
||||||
|
- **N_m3u8DLRE**
|
||||||
|
- `Path`: Path to the N_m3u8DL-RE executable.
|
||||||
|
|
||||||
|
- **Server**
|
||||||
|
- `Host`: Bind address for the web server (`127.0.0.1` recommended).
|
||||||
|
- `Port`: Web server port.
|
||||||
|
- `ReadTimeoutSec`, `WriteTimeoutSec`, `IdleTimeoutSec`, `ReadHeaderTimeoutS`: HTTP timeout settings.
|
||||||
|
|
||||||
|
- **Security**
|
||||||
|
- `AuthToken`: Optional token for protecting all endpoints. Recommended when binding to a non-loopback host.
|
||||||
|
|
||||||
|
### Environment Variable Overrides
|
||||||
|
|
||||||
|
You can override the configuration options using environment variables. The following environment variables are supported:
|
||||||
|
|
||||||
|
- `BASE_DIR`: Overrides `General.BaseDir`
|
||||||
|
- `FORMAT`: Overrides `General.Format`
|
||||||
|
- `TEMP_BASE_DIR`: Overrides `General.TempBaseDir`
|
||||||
|
- `ENABLE_CONSOLE`: Overrides `General.EnableConsole` (set to `true` or `false`)
|
||||||
|
- `MAX_UPLOAD_MB`: Overrides `General.MaxUploadMB`
|
||||||
|
- `WATCHED_FOLDER`: Overrides `WatchFolder.Path`
|
||||||
|
- `USE_POLLING`: Overrides `WatchFolder.UsePolling` (set to `true` or `false`)
|
||||||
|
- `USE_INOTIFY`: Overrides `WatchFolder.UseInotify` (set to `true` or `false`)
|
||||||
|
- `POLLING_INTERVAL`: Overrides `WatchFolder.PollingInterval`
|
||||||
|
- `SERVER_HOST`: Overrides `Server.Host`
|
||||||
|
- `SERVER_PORT`: Overrides `Server.Port`
|
||||||
|
- `AUTH_TOKEN`: Overrides `Security.AuthToken`
|
||||||
|
|
||||||
## Web UI Usage
|
## Web UI Usage
|
||||||
|
|
||||||
@@ -25,8 +87,10 @@ Adjust the paths and format as needed. (mkv, mp4)
|
|||||||
|
|
||||||
2. Open a web browser and go to `http://localhost:8080`
|
2. Open a web browser and go to `http://localhost:8080`
|
||||||
|
|
||||||
3. Use the interface to upload .drmd files and monitor download progress
|
If `Security.AuthToken` is configured, include it as a query parameter:
|
||||||
|
`http://localhost:8080/?token=YOUR_TOKEN`
|
||||||
|
|
||||||
|
3. Use the interface to upload .drmd files and monitor download progress
|
||||||
|
|
||||||
## CLI Usage
|
## CLI Usage
|
||||||
|
|
||||||
@@ -38,8 +102,16 @@ To process a file directly from the command line:
|
|||||||
|
|
||||||
This will download the file and save it in the base directory specified in the config.
|
This will download the file and save it in the base directory specified in the config.
|
||||||
|
|
||||||
## TODO
|
# Previews
|
||||||
- ~~Filename Sanitation (Makes new directory on /... oops)~~
|
|
||||||
- ~~GoPlay Fix~~
|
## Index Page
|
||||||
- Windows?
|
|
||||||
- Proper UI?
|

|
||||||
|
|
||||||
|
## Select Page
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
## Progress Page
|
||||||
|
|
||||||
|

|
||||||
|
|||||||
41
config.go
41
config.go
@@ -1,41 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"os"
|
|
||||||
|
|
||||||
"github.com/BurntSushi/toml"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Config struct {
|
|
||||||
BaseDir string
|
|
||||||
Format string
|
|
||||||
N_m3u8DLRE struct {
|
|
||||||
Path string
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var config Config
|
|
||||||
|
|
||||||
func loadConfig() {
|
|
||||||
configFile, err := os.Open("config.toml")
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("Error opening config file:", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer configFile.Close()
|
|
||||||
|
|
||||||
byteValue, _ := io.ReadAll(configFile)
|
|
||||||
|
|
||||||
if _, err := toml.Decode(string(byteValue), &config); err != nil {
|
|
||||||
fmt.Println("Error decoding config file:", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if config.N_m3u8DLRE.Path == "" {
|
|
||||||
fmt.Println("Error: N_m3u8DL-RE path is not specified in the config file")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
26
config.template.toml
Normal file
26
config.template.toml
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
[General]
|
||||||
|
BaseDir = "/mnt/media"
|
||||||
|
Format = "mkv"
|
||||||
|
TempBaseDir = "/tmp/nre"
|
||||||
|
EnableConsole = true
|
||||||
|
MaxUploadMB = 32
|
||||||
|
|
||||||
|
[WatchFolder]
|
||||||
|
Path = "/mnt/watched"
|
||||||
|
PollingInterval = 10
|
||||||
|
UsePolling = false
|
||||||
|
UseInotify = false
|
||||||
|
|
||||||
|
[N_m3u8DLRE]
|
||||||
|
Path = "nre"
|
||||||
|
|
||||||
|
[Server]
|
||||||
|
Host = "127.0.0.1"
|
||||||
|
Port = 8080
|
||||||
|
ReadTimeoutSec = 30
|
||||||
|
WriteTimeoutSec = 30
|
||||||
|
IdleTimeoutSec = 60
|
||||||
|
ReadHeaderTimeoutS = 10
|
||||||
|
|
||||||
|
[Security]
|
||||||
|
AuthToken = ""
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
BaseDir = "/mnt/media"
|
|
||||||
Format = "mkv"
|
|
||||||
|
|
||||||
[N_m3u8DLRE]
|
|
||||||
Path = "nre"
|
|
||||||
171
downloaders.go
171
downloaders.go
@@ -1,171 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/base64"
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"net/http"
|
|
||||||
"os"
|
|
||||||
"os/exec"
|
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
func processInputFile(inputFile string) error {
|
|
||||||
jsonFile, err := os.Open(inputFile)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("error opening file %s: %v", inputFile, err)
|
|
||||||
}
|
|
||||||
defer jsonFile.Close()
|
|
||||||
|
|
||||||
byteValue, err := io.ReadAll(jsonFile)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("error reading file %s: %v", inputFile, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
byteValue = removeBOM(byteValue)
|
|
||||||
|
|
||||||
var items Items
|
|
||||||
err = json.Unmarshal(byteValue, &items)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("error unmarshaling JSON: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
for i, item := range items.Items {
|
|
||||||
updateProgress(filepath.Base(inputFile), float64(i)/float64(len(items.Items))*100, item.Filename)
|
|
||||||
err := downloadFile(item)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("Error downloading file: %v\n", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
updateProgress(filepath.Base(inputFile), 100, "")
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func removeBOM(input []byte) []byte {
|
|
||||||
if len(input) >= 3 && input[0] == 0xEF && input[1] == 0xBB && input[2] == 0xBF {
|
|
||||||
return input[3:]
|
|
||||||
}
|
|
||||||
return input
|
|
||||||
}
|
|
||||||
|
|
||||||
func downloadFile(item Item) error {
|
|
||||||
fmt.Println("Downloading:", item.Filename)
|
|
||||||
|
|
||||||
mpdPath := item.MPD
|
|
||||||
if !isValidURL(item.MPD) {
|
|
||||||
decodedMPD, err := base64.StdEncoding.DecodeString(item.MPD)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("error decoding base64 MPD: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
tempFile, err := os.CreateTemp("", "temp_mpd_*.mpd")
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("error creating temporary MPD file: %v", err)
|
|
||||||
}
|
|
||||||
defer os.Remove(tempFile.Name())
|
|
||||||
|
|
||||||
if _, err := tempFile.Write(decodedMPD); err != nil {
|
|
||||||
return fmt.Errorf("error writing to temporary MPD file: %v", err)
|
|
||||||
}
|
|
||||||
if err := tempFile.Close(); err != nil {
|
|
||||||
return fmt.Errorf("error closing temporary MPD file: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
mpdPath = tempFile.Name()
|
|
||||||
} else if strings.HasPrefix(item.MPD, "https://pubads.g.doubleclick.net") {
|
|
||||||
resp, err := http.Get(item.MPD)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("error downloading MPD: %v", err)
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
|
|
||||||
mpdContent, err := io.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("error reading MPD content: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
fixedMPDContent, err := fixGoPlay(string(mpdContent))
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("error fixing MPD content: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
tempFile, err := os.CreateTemp("", "fixed_mpd_*.mpd")
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("error creating temporary MPD file: %v", err)
|
|
||||||
}
|
|
||||||
defer os.Remove(tempFile.Name())
|
|
||||||
|
|
||||||
if _, err := tempFile.WriteString(fixedMPDContent); err != nil {
|
|
||||||
return fmt.Errorf("error writing to temporary MPD file: %v", err)
|
|
||||||
}
|
|
||||||
if err := tempFile.Close(); err != nil {
|
|
||||||
return fmt.Errorf("error closing temporary MPD file: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
mpdPath = tempFile.Name()
|
|
||||||
}
|
|
||||||
|
|
||||||
command := getDownloadCommand(item, mpdPath)
|
|
||||||
|
|
||||||
if item.Subtitles != "" {
|
|
||||||
subtitlePaths, err := downloadAndConvertSubtitles(item.Subtitles)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("Error processing subtitles: %v\n", err)
|
|
||||||
} else {
|
|
||||||
for _, path := range subtitlePaths {
|
|
||||||
fmt.Println("Adding subtitle:", path)
|
|
||||||
command += fmt.Sprintf(" --mux-import \"path=%s:lang=nl:name=Nederlands\"", path)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
cmd := exec.Command("bash", "-c", command)
|
|
||||||
|
|
||||||
cmd.Stdout = os.Stdout
|
|
||||||
cmd.Stderr = os.Stderr
|
|
||||||
|
|
||||||
err := cmd.Run()
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("error executing download command: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Println("Download completed successfully")
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func getDownloadCommand(item Item, mpdPath string) string {
|
|
||||||
metadata := parseMetadata(item.Metadata)
|
|
||||||
keys := getKeys(item.Keys)
|
|
||||||
|
|
||||||
command := fmt.Sprintf("%s %s", config.N_m3u8DLRE.Path, mpdPath)
|
|
||||||
|
|
||||||
for _, key := range keys {
|
|
||||||
if key != "" {
|
|
||||||
command += fmt.Sprintf(" --key %s", key)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
command += " --auto-select"
|
|
||||||
|
|
||||||
sanitizedFilename := sanitizeFilename(item.Filename)
|
|
||||||
|
|
||||||
filename := fmt.Sprintf("\"%s\"", sanitizedFilename)
|
|
||||||
command += fmt.Sprintf(" --save-name %s", filename)
|
|
||||||
|
|
||||||
command += fmt.Sprintf(" --mux-after-done format=%s", config.Format)
|
|
||||||
|
|
||||||
saveDir := config.BaseDir
|
|
||||||
if metadata.Type == "serie" {
|
|
||||||
saveDir = filepath.Join(saveDir, "Series", metadata.Title, metadata.Season)
|
|
||||||
} else {
|
|
||||||
saveDir = filepath.Join(saveDir, "Movies", metadata.Title)
|
|
||||||
}
|
|
||||||
command += fmt.Sprintf(" --save-dir \"%s\"", saveDir)
|
|
||||||
|
|
||||||
fmt.Println(command)
|
|
||||||
|
|
||||||
return command
|
|
||||||
}
|
|
||||||
96
handlers.go
96
handlers.go
@@ -1,96 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"net/http"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
func handleRoot(w http.ResponseWriter, r *http.Request) {
|
|
||||||
if r.URL.Path != "/" {
|
|
||||||
http.NotFound(w, r)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
progressMutex.Lock()
|
|
||||||
jobs := make(map[string]*ProgressInfo)
|
|
||||||
for k, v := range progress {
|
|
||||||
jobs[k] = v
|
|
||||||
}
|
|
||||||
progressMutex.Unlock()
|
|
||||||
|
|
||||||
err := templates.ExecuteTemplate(w, "index", struct{ Jobs map[string]*ProgressInfo }{jobs})
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func handleUpload(w http.ResponseWriter, r *http.Request) {
|
|
||||||
file, header, err := r.FormFile("file")
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer file.Close()
|
|
||||||
|
|
||||||
filename := fmt.Sprintf("%d_%s", time.Now().UnixNano(), header.Filename)
|
|
||||||
filepath := filepath.Join(uploadDir, filename)
|
|
||||||
|
|
||||||
newFile, err := os.Create(filepath)
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer newFile.Close()
|
|
||||||
|
|
||||||
_, err = io.Copy(newFile, file)
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
err := processInputFile(filepath)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("Error processing file: %v\n", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
os.Remove(filepath)
|
|
||||||
}()
|
|
||||||
|
|
||||||
http.Redirect(w, r, "/progress?filename="+filename, http.StatusSeeOther)
|
|
||||||
}
|
|
||||||
|
|
||||||
func handleProgress(w http.ResponseWriter, r *http.Request) {
|
|
||||||
filename := r.URL.Query().Get("filename")
|
|
||||||
fmt.Printf("Handling progress request for filename: %s\n", filename)
|
|
||||||
|
|
||||||
if r.Header.Get("Accept") == "application/json" {
|
|
||||||
progressInfo := getProgress(filename)
|
|
||||||
fmt.Printf("Progress info for %s: %+v\n", filename, progressInfo)
|
|
||||||
|
|
||||||
if progressInfo == nil {
|
|
||||||
w.WriteHeader(http.StatusNotFound)
|
|
||||||
json.NewEncoder(w).Encode(map[string]string{"error": "No progress information found"})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
err := json.NewEncoder(w).Encode(progressInfo)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("Error encoding progress info: %v\n", err)
|
|
||||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
err := templates.ExecuteTemplate(w, "progress", struct{ Filename string }{filename})
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
BIN
images/index.png
Normal file
BIN
images/index.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 33 KiB |
BIN
images/progress.png
Normal file
BIN
images/progress.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 29 KiB |
BIN
images/select.png
Normal file
BIN
images/select.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 41 KiB |
109
main.go
109
main.go
@@ -1,109 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"flag"
|
|
||||||
"fmt"
|
|
||||||
"html/template"
|
|
||||||
"net/http"
|
|
||||||
"os"
|
|
||||||
"strings"
|
|
||||||
"sync"
|
|
||||||
|
|
||||||
"embed"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Item struct {
|
|
||||||
MPD string
|
|
||||||
Keys string
|
|
||||||
Filename string
|
|
||||||
Description string
|
|
||||||
Subtitles string
|
|
||||||
Poster string
|
|
||||||
Metadata string
|
|
||||||
}
|
|
||||||
|
|
||||||
type Items struct {
|
|
||||||
Items []Item
|
|
||||||
}
|
|
||||||
|
|
||||||
type Metadata struct {
|
|
||||||
Title string
|
|
||||||
Type string
|
|
||||||
Season string
|
|
||||||
}
|
|
||||||
|
|
||||||
var progressMutex sync.Mutex
|
|
||||||
var progress = make(map[string]*ProgressInfo)
|
|
||||||
|
|
||||||
const uploadDir = "uploads"
|
|
||||||
|
|
||||||
type ProgressInfo struct {
|
|
||||||
Percentage float64
|
|
||||||
CurrentFile string
|
|
||||||
}
|
|
||||||
|
|
||||||
var templates *template.Template
|
|
||||||
|
|
||||||
//go:embed templates
|
|
||||||
var templateFS embed.FS
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
if err := os.MkdirAll(uploadDir, 0755); err != nil {
|
|
||||||
fmt.Printf("Error creating upload directory: %v\n", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
templates = template.Must(template.ParseFS(templateFS, "templates/*"))
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
loadConfig()
|
|
||||||
inputFile := flag.String("f", "", "Path to the input JSON file")
|
|
||||||
flag.Parse()
|
|
||||||
|
|
||||||
if *inputFile == "" {
|
|
||||||
startWebServer()
|
|
||||||
} else {
|
|
||||||
processInputFile(*inputFile)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func startWebServer() {
|
|
||||||
http.HandleFunc("/", handleRoot)
|
|
||||||
http.HandleFunc("/upload", handleUpload)
|
|
||||||
http.HandleFunc("/progress", handleProgress)
|
|
||||||
|
|
||||||
fmt.Println("Starting web server on http://0.0.0.0:8080")
|
|
||||||
http.ListenAndServe(":8080", nil)
|
|
||||||
}
|
|
||||||
|
|
||||||
func updateProgress(filename string, value float64, currentFile string) {
|
|
||||||
progressMutex.Lock()
|
|
||||||
defer progressMutex.Unlock()
|
|
||||||
progress[filename] = &ProgressInfo{
|
|
||||||
Percentage: value,
|
|
||||||
CurrentFile: currentFile,
|
|
||||||
}
|
|
||||||
fmt.Printf("Progress updated for %s: %.2f%%, Current file: %s\n", filename, value, currentFile)
|
|
||||||
}
|
|
||||||
|
|
||||||
func getProgress(filename string) *ProgressInfo {
|
|
||||||
progressMutex.Lock()
|
|
||||||
defer progressMutex.Unlock()
|
|
||||||
return progress[filename]
|
|
||||||
}
|
|
||||||
|
|
||||||
func getKeys(keys string) []string {
|
|
||||||
return strings.Split(keys, ",")
|
|
||||||
}
|
|
||||||
|
|
||||||
func parseMetadata(metadata string) Metadata {
|
|
||||||
parts := strings.Split(metadata, ";")
|
|
||||||
if len(parts) != 3 {
|
|
||||||
return Metadata{}
|
|
||||||
}
|
|
||||||
return Metadata{
|
|
||||||
Title: strings.TrimSpace(parts[0]),
|
|
||||||
Type: strings.TrimSpace(parts[1]),
|
|
||||||
Season: "S" + strings.TrimSpace(parts[2]),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
250
src/config.go
Normal file
250
src/config.go
Normal file
@@ -0,0 +1,250 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/BurntSushi/toml"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
General struct {
|
||||||
|
BaseDir string
|
||||||
|
Format string
|
||||||
|
TempBaseDir string
|
||||||
|
EnableConsole bool
|
||||||
|
MaxUploadMB int
|
||||||
|
}
|
||||||
|
WatchFolder struct {
|
||||||
|
Path string
|
||||||
|
UsePolling bool
|
||||||
|
UseInotify bool
|
||||||
|
PollingInterval int
|
||||||
|
}
|
||||||
|
Server struct {
|
||||||
|
Host string
|
||||||
|
Port int
|
||||||
|
ReadTimeoutSec int
|
||||||
|
WriteTimeoutSec int
|
||||||
|
IdleTimeoutSec int
|
||||||
|
ReadHeaderTimeoutS int
|
||||||
|
}
|
||||||
|
Security struct {
|
||||||
|
AuthToken string
|
||||||
|
}
|
||||||
|
N_m3u8DLRE struct {
|
||||||
|
Path string
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var config Config
|
||||||
|
|
||||||
|
func loadConfig(path string) {
|
||||||
|
configFile, err := os.Open(path)
|
||||||
|
if err != nil {
|
||||||
|
logger.LogError("Config", fmt.Sprintf("Error opening config file: %v", err))
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
defer configFile.Close()
|
||||||
|
|
||||||
|
byteValue, err := io.ReadAll(configFile)
|
||||||
|
if err != nil {
|
||||||
|
logger.LogError("Config", fmt.Sprintf("Error reading config file: %v", err))
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := toml.Decode(string(byteValue), &config); err != nil {
|
||||||
|
logger.LogError("Config", fmt.Sprintf("Error decoding config file: %v", err))
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
overrideConfigWithEnv()
|
||||||
|
setDefaultConfigValues()
|
||||||
|
|
||||||
|
if err := validatePaths(); err != nil {
|
||||||
|
logger.LogError("Config", fmt.Sprintf("Configuration error: %v", err))
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
if config.WatchFolder.PollingInterval <= 0 {
|
||||||
|
config.WatchFolder.PollingInterval = 10
|
||||||
|
}
|
||||||
|
|
||||||
|
logConfig()
|
||||||
|
}
|
||||||
|
|
||||||
|
func setDefaultConfigValues() {
|
||||||
|
if config.General.MaxUploadMB <= 0 {
|
||||||
|
config.General.MaxUploadMB = 32
|
||||||
|
}
|
||||||
|
|
||||||
|
if strings.TrimSpace(config.Server.Host) == "" {
|
||||||
|
config.Server.Host = "127.0.0.1"
|
||||||
|
}
|
||||||
|
|
||||||
|
if config.Server.Port <= 0 {
|
||||||
|
config.Server.Port = 8080
|
||||||
|
}
|
||||||
|
|
||||||
|
if config.Server.ReadTimeoutSec <= 0 {
|
||||||
|
config.Server.ReadTimeoutSec = 30
|
||||||
|
}
|
||||||
|
|
||||||
|
if config.Server.WriteTimeoutSec <= 0 {
|
||||||
|
config.Server.WriteTimeoutSec = 30
|
||||||
|
}
|
||||||
|
|
||||||
|
if config.Server.IdleTimeoutSec <= 0 {
|
||||||
|
config.Server.IdleTimeoutSec = 60
|
||||||
|
}
|
||||||
|
|
||||||
|
if config.Server.ReadHeaderTimeoutS <= 0 {
|
||||||
|
config.Server.ReadHeaderTimeoutS = 10
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func overrideConfigWithEnv() {
|
||||||
|
if envBaseDir := os.Getenv("BASE_DIR"); envBaseDir != "" {
|
||||||
|
config.General.BaseDir = envBaseDir
|
||||||
|
}
|
||||||
|
if envFormat := os.Getenv("FORMAT"); envFormat != "" {
|
||||||
|
config.General.Format = envFormat
|
||||||
|
}
|
||||||
|
if envTempBaseDir := os.Getenv("TEMP_BASE_DIR"); envTempBaseDir != "" {
|
||||||
|
config.General.TempBaseDir = envTempBaseDir
|
||||||
|
}
|
||||||
|
if envEnableConsole := os.Getenv("ENABLE_CONSOLE"); envEnableConsole != "" {
|
||||||
|
config.General.EnableConsole = strings.ToLower(envEnableConsole) == "true"
|
||||||
|
}
|
||||||
|
if envWatchedFolder := os.Getenv("WATCHED_FOLDER"); envWatchedFolder != "" {
|
||||||
|
config.WatchFolder.Path = envWatchedFolder
|
||||||
|
}
|
||||||
|
if envUsePolling := os.Getenv("USE_POLLING"); envUsePolling != "" {
|
||||||
|
config.WatchFolder.UsePolling = strings.ToLower(envUsePolling) == "true"
|
||||||
|
}
|
||||||
|
if envUseInotify := os.Getenv("USE_INOTIFY"); envUseInotify != "" {
|
||||||
|
config.WatchFolder.UseInotify = strings.ToLower(envUseInotify) == "true"
|
||||||
|
}
|
||||||
|
if envPollingInterval := os.Getenv("POLLING_INTERVAL"); envPollingInterval != "" {
|
||||||
|
if interval, err := strconv.Atoi(envPollingInterval); err == nil {
|
||||||
|
config.WatchFolder.PollingInterval = interval
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if envMaxUploadMB := os.Getenv("MAX_UPLOAD_MB"); envMaxUploadMB != "" {
|
||||||
|
if value, err := strconv.Atoi(envMaxUploadMB); err == nil {
|
||||||
|
config.General.MaxUploadMB = value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if envHost := os.Getenv("SERVER_HOST"); envHost != "" {
|
||||||
|
config.Server.Host = envHost
|
||||||
|
}
|
||||||
|
if envPort := os.Getenv("SERVER_PORT"); envPort != "" {
|
||||||
|
if value, err := strconv.Atoi(envPort); err == nil {
|
||||||
|
config.Server.Port = value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if envAuthToken := os.Getenv("AUTH_TOKEN"); envAuthToken != "" {
|
||||||
|
config.Security.AuthToken = envAuthToken
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func validatePaths() error {
|
||||||
|
if strings.TrimSpace(config.General.Format) == "" {
|
||||||
|
return errors.New("format is not specified")
|
||||||
|
}
|
||||||
|
|
||||||
|
allowedFormats := map[string]bool{"mkv": true, "mp4": true}
|
||||||
|
if !allowedFormats[strings.ToLower(config.General.Format)] {
|
||||||
|
return fmt.Errorf("unsupported format: %s (supported: mkv, mp4)", config.General.Format)
|
||||||
|
}
|
||||||
|
|
||||||
|
paths := []struct {
|
||||||
|
name string
|
||||||
|
path string
|
||||||
|
}{
|
||||||
|
{"BaseDir", config.General.BaseDir},
|
||||||
|
{"TempBaseDir", config.General.TempBaseDir},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, p := range paths {
|
||||||
|
if p.path == "" {
|
||||||
|
return fmt.Errorf("%s is not specified", p.name)
|
||||||
|
}
|
||||||
|
if _, err := os.Stat(p.path); os.IsNotExist(err) {
|
||||||
|
if p.name == "TempBaseDir" {
|
||||||
|
if mkErr := os.MkdirAll(p.path, 0755); mkErr != nil {
|
||||||
|
return fmt.Errorf("unable to create %s: %v", p.name, mkErr)
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
return fmt.Errorf("%s does not exist: %s", p.name, p.path)
|
||||||
|
} else if err != nil {
|
||||||
|
return fmt.Errorf("error accessing %s: %v", p.name, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if config.WatchFolder.UsePolling || config.WatchFolder.UseInotify {
|
||||||
|
if config.WatchFolder.Path == "" {
|
||||||
|
return fmt.Errorf("WatchedFolder is not specified")
|
||||||
|
}
|
||||||
|
if _, err := os.Stat(config.WatchFolder.Path); os.IsNotExist(err) {
|
||||||
|
return fmt.Errorf("WatchedFolder does not exist: %s", config.WatchFolder.Path)
|
||||||
|
} else if err != nil {
|
||||||
|
return fmt.Errorf("error accessing WatchedFolder: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if strings.TrimSpace(config.N_m3u8DLRE.Path) == "" {
|
||||||
|
return errors.New("N_m3u8DLRE path is not specified")
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := exec.LookPath(config.N_m3u8DLRE.Path); err != nil {
|
||||||
|
return fmt.Errorf("N_m3u8DLRE executable not found in PATH: %s", config.N_m3u8DLRE.Path)
|
||||||
|
}
|
||||||
|
|
||||||
|
if config.Server.Port <= 0 || config.Server.Port > 65535 {
|
||||||
|
return fmt.Errorf("invalid server port: %d", config.Server.Port)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func logConfig() {
|
||||||
|
configInfo := fmt.Sprintf(`
|
||||||
|
Configuration Loaded:
|
||||||
|
General:
|
||||||
|
BaseDir: %s
|
||||||
|
Format: %s
|
||||||
|
TempBaseDir: %s
|
||||||
|
EnableConsole: %t
|
||||||
|
MaxUploadMB: %d
|
||||||
|
WatchFolder:
|
||||||
|
Path: %s
|
||||||
|
UsePolling: %t
|
||||||
|
UseInotify: %t
|
||||||
|
PollingInterval: %d
|
||||||
|
Server:
|
||||||
|
Host: %s
|
||||||
|
Port: %d
|
||||||
|
ReadTimeoutSec: %d
|
||||||
|
WriteTimeoutSec: %d
|
||||||
|
IdleTimeoutSec: %d
|
||||||
|
ReadHeaderTimeoutS: %d
|
||||||
|
Security:
|
||||||
|
AuthTokenConfigured: %t
|
||||||
|
N_m3u8DLRE:
|
||||||
|
Path: %s
|
||||||
|
`, config.General.BaseDir, config.General.Format, config.General.TempBaseDir, config.General.EnableConsole,
|
||||||
|
config.General.MaxUploadMB,
|
||||||
|
config.WatchFolder.Path, config.WatchFolder.UsePolling, config.WatchFolder.UseInotify, config.WatchFolder.PollingInterval,
|
||||||
|
config.Server.Host, config.Server.Port, config.Server.ReadTimeoutSec, config.Server.WriteTimeoutSec, config.Server.IdleTimeoutSec, config.Server.ReadHeaderTimeoutS,
|
||||||
|
strings.TrimSpace(config.Security.AuthToken) != "",
|
||||||
|
config.N_m3u8DLRE.Path)
|
||||||
|
|
||||||
|
logger.LogInfo("Config", configInfo)
|
||||||
|
}
|
||||||
216
src/downloaders.go
Normal file
216
src/downloaders.go
Normal file
@@ -0,0 +1,216 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/base64"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
var decryptionKeyRegex = regexp.MustCompile(`^[0-9a-fA-F]{32}:[0-9a-fA-F]{32}$`)
|
||||||
|
|
||||||
|
type websocketBroadcastWriter struct {
|
||||||
|
filename string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w websocketBroadcastWriter) Write(p []byte) (int, error) {
|
||||||
|
if config.General.EnableConsole && len(p) > 0 {
|
||||||
|
message := append([]byte(nil), p...)
|
||||||
|
broadcast(w.filename, message)
|
||||||
|
}
|
||||||
|
return len(p), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func removeBOM(input []byte) []byte {
|
||||||
|
if len(input) >= 3 && input[0] == 0xEF && input[1] == 0xBB && input[2] == 0xBF {
|
||||||
|
return input[3:]
|
||||||
|
}
|
||||||
|
return input
|
||||||
|
}
|
||||||
|
|
||||||
|
func downloadFile(drmdFilename string, item Item, jobInfo *JobInfo) error {
|
||||||
|
logger.LogInfo("Download File", fmt.Sprintf("Starting download for: %s", item.Filename))
|
||||||
|
|
||||||
|
if err := os.MkdirAll(config.General.TempBaseDir, 0755); err != nil {
|
||||||
|
logger.LogError("Download File", fmt.Sprintf("Error creating temp base dir: %v", err))
|
||||||
|
return fmt.Errorf("error creating temp base dir: %v", err)
|
||||||
|
}
|
||||||
|
tempDir, err := os.MkdirTemp(config.General.TempBaseDir, sanitizeFilename(item.Filename)+"_")
|
||||||
|
if err != nil {
|
||||||
|
logger.LogError("Download File", fmt.Sprintf("Error creating temporary directory: %v", err))
|
||||||
|
return fmt.Errorf("error creating temporary directory: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
jobInfo.SetTempDir(tempDir)
|
||||||
|
|
||||||
|
mpdPath := item.MPD
|
||||||
|
if !isValidURL(item.MPD) {
|
||||||
|
decodedMPD, err := base64.StdEncoding.DecodeString(item.MPD)
|
||||||
|
if err != nil {
|
||||||
|
logger.LogError("Download File", fmt.Sprintf("Error decoding base64 MPD: %v", err))
|
||||||
|
return fmt.Errorf("error decoding base64 MPD: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
tempFile, err := os.CreateTemp("", "temp_mpd_*.mpd")
|
||||||
|
if err != nil {
|
||||||
|
logger.LogError("Download File", fmt.Sprintf("Error creating temporary MPD file: %v", err))
|
||||||
|
return fmt.Errorf("error creating temporary MPD file: %v", err)
|
||||||
|
}
|
||||||
|
defer os.Remove(tempFile.Name())
|
||||||
|
|
||||||
|
if _, err := tempFile.Write(decodedMPD); err != nil {
|
||||||
|
logger.LogError("Download File", fmt.Sprintf("Error writing to temporary MPD file: %v", err))
|
||||||
|
return fmt.Errorf("error writing to temporary MPD file: %v", err)
|
||||||
|
}
|
||||||
|
if err := tempFile.Close(); err != nil {
|
||||||
|
logger.LogError("Download File", fmt.Sprintf("Error closing temporary MPD file: %v", err))
|
||||||
|
return fmt.Errorf("error closing temporary MPD file: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
mpdPath = tempFile.Name()
|
||||||
|
} else if strings.HasPrefix(item.MPD, "https://pubads.g.doubleclick.net") {
|
||||||
|
mpdContent, err := fetchRemoteContent(item.MPD)
|
||||||
|
if err != nil {
|
||||||
|
logger.LogError("Download File", fmt.Sprintf("Error downloading MPD: %v", err))
|
||||||
|
return fmt.Errorf("error downloading MPD: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fixedMPDContent, err := fixGoPlay(string(mpdContent))
|
||||||
|
if err != nil {
|
||||||
|
logger.LogError("Download File", fmt.Sprintf("Error fixing MPD content: %v", err))
|
||||||
|
return fmt.Errorf("error fixing MPD content: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
tempFile, err := os.CreateTemp("", "fixed_mpd_*.mpd")
|
||||||
|
if err != nil {
|
||||||
|
logger.LogError("Download File", fmt.Sprintf("Error creating temporary MPD file: %v", err))
|
||||||
|
return fmt.Errorf("error creating temporary MPD file: %v", err)
|
||||||
|
}
|
||||||
|
defer os.Remove(tempFile.Name())
|
||||||
|
|
||||||
|
if _, err := tempFile.WriteString(fixedMPDContent); err != nil {
|
||||||
|
logger.LogError("Download File", fmt.Sprintf("Error writing to temporary MPD file: %v", err))
|
||||||
|
return fmt.Errorf("error writing to temporary MPD file: %v", err)
|
||||||
|
}
|
||||||
|
if err := tempFile.Close(); err != nil {
|
||||||
|
logger.LogError("Download File", fmt.Sprintf("Error closing temporary MPD file: %v", err))
|
||||||
|
return fmt.Errorf("error closing temporary MPD file: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
mpdPath = tempFile.Name()
|
||||||
|
}
|
||||||
|
|
||||||
|
args, err := getDownloadArgs(item, mpdPath, tempDir)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if item.Subtitles != "" {
|
||||||
|
subtitlePaths, err := downloadAndConvertSubtitles(item.Subtitles)
|
||||||
|
if err != nil {
|
||||||
|
logger.LogError("Download File", fmt.Sprintf("Error processing subtitles: %v", err))
|
||||||
|
} else {
|
||||||
|
for _, path := range subtitlePaths {
|
||||||
|
logger.LogInfo("Download File", fmt.Sprintf("Adding subtitle: %s", path))
|
||||||
|
args = append(args, "--mux-import", fmt.Sprintf("path=%s:lang=nl:name=Nederlands", path))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd := exec.Command(config.N_m3u8DLRE.Path, args...)
|
||||||
|
jobInfo.SetCmd(cmd)
|
||||||
|
|
||||||
|
broadcastWriter := websocketBroadcastWriter{filename: drmdFilename}
|
||||||
|
cmd.Stdout = io.MultiWriter(os.Stdout, broadcastWriter)
|
||||||
|
cmd.Stderr = io.MultiWriter(os.Stderr, broadcastWriter)
|
||||||
|
|
||||||
|
err = cmd.Start()
|
||||||
|
if err != nil {
|
||||||
|
logger.LogError("Download File", fmt.Sprintf("Error starting download command: %v", err))
|
||||||
|
return fmt.Errorf("error starting download command: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
done := make(chan error)
|
||||||
|
go func() {
|
||||||
|
done <- cmd.Wait()
|
||||||
|
}()
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-jobInfo.AbortChan:
|
||||||
|
jobInfo.KillProcess()
|
||||||
|
_ = os.RemoveAll(tempDir)
|
||||||
|
logger.LogInfo("Download File", "Download aborted")
|
||||||
|
return ErrDownloadAborted
|
||||||
|
case err := <-done:
|
||||||
|
if jobInfo.IsPaused() {
|
||||||
|
logger.LogInfo("Download File", "Download paused")
|
||||||
|
return ErrDownloadPaused
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
if jobInfo.IsAborted() {
|
||||||
|
return ErrDownloadAborted
|
||||||
|
}
|
||||||
|
|
||||||
|
if jobInfo.IsPaused() {
|
||||||
|
return ErrDownloadPaused
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.LogError("Download File", fmt.Sprintf("Error executing download command: %v", err))
|
||||||
|
return fmt.Errorf("error executing download command: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.LogInfo("Download File", "Download completed successfully")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getDownloadArgs(item Item, mpdPath string, tempDir string) ([]string, error) {
|
||||||
|
metadata := parseMetadata(item.Metadata)
|
||||||
|
keys := getKeys(item.Keys)
|
||||||
|
|
||||||
|
args := []string{mpdPath}
|
||||||
|
|
||||||
|
for _, key := range keys {
|
||||||
|
if !decryptionKeyRegex.MatchString(key) {
|
||||||
|
return nil, fmt.Errorf("invalid decryption key format")
|
||||||
|
}
|
||||||
|
args = append(args, "--key", key)
|
||||||
|
}
|
||||||
|
|
||||||
|
args = append(args,
|
||||||
|
"--select-video", "best",
|
||||||
|
"--select-audio", "all",
|
||||||
|
"--select-subtitle", "all",
|
||||||
|
)
|
||||||
|
|
||||||
|
sanitizedFilename := sanitizeFilename(item.Filename)
|
||||||
|
args = append(args, "--save-name", sanitizedFilename)
|
||||||
|
args = append(args, "--mux-after-done", fmt.Sprintf("format=%s", config.General.Format))
|
||||||
|
|
||||||
|
saveDir := config.General.BaseDir
|
||||||
|
if metadata.Type == "serie" {
|
||||||
|
saveDir = filepath.Join(saveDir, "Series", metadata.Title, metadata.Season)
|
||||||
|
} else {
|
||||||
|
saveDir = filepath.Join(saveDir, "Movies", metadata.Title)
|
||||||
|
}
|
||||||
|
if err := os.MkdirAll(saveDir, 0755); err != nil {
|
||||||
|
return nil, fmt.Errorf("unable to create save directory: %w", err)
|
||||||
|
}
|
||||||
|
args = append(args, "--save-dir", saveDir)
|
||||||
|
args = append(args, "--tmp-dir", tempDir)
|
||||||
|
|
||||||
|
currentSpeedLimit := getGlobalSpeedLimit()
|
||||||
|
if currentSpeedLimit != "" {
|
||||||
|
if !speedLimitRegex.MatchString(currentSpeedLimit) {
|
||||||
|
return nil, errors.New("invalid speed limit format")
|
||||||
|
}
|
||||||
|
args = append(args, "-R", currentSpeedLimit)
|
||||||
|
}
|
||||||
|
|
||||||
|
return args, nil
|
||||||
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
module DRMDTool
|
module DRMDTool
|
||||||
|
|
||||||
go 1.23.0
|
go 1.25.0
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/BurntSushi/toml v1.4.0
|
github.com/BurntSushi/toml v1.4.0
|
||||||
@@ -8,9 +8,13 @@ require (
|
|||||||
github.com/beevik/etree v1.4.1
|
github.com/beevik/etree v1.4.1
|
||||||
)
|
)
|
||||||
|
|
||||||
|
require golang.org/x/sys v0.43.0 // indirect
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/asticode/go-astikit v0.20.0 // indirect
|
github.com/asticode/go-astikit v0.20.0 // indirect
|
||||||
github.com/asticode/go-astits v1.8.0 // indirect
|
github.com/asticode/go-astits v1.8.0 // indirect
|
||||||
golang.org/x/net v0.0.0-20200904194848-62affa334b73 // indirect
|
github.com/fsnotify/fsnotify v1.7.0
|
||||||
golang.org/x/text v0.3.2 // indirect
|
github.com/gorilla/websocket v1.5.3
|
||||||
|
golang.org/x/net v0.53.0 // indirect
|
||||||
|
golang.org/x/text v0.36.0 // indirect
|
||||||
)
|
)
|
||||||
@@ -10,6 +10,10 @@ github.com/beevik/etree v1.4.1 h1:PmQJDDYahBGNKDcpdX8uPy1xRCwoCGVUiW669MEirVI=
|
|||||||
github.com/beevik/etree v1.4.1/go.mod h1:gPNJNaBGVZ9AwsidazFZyygnd+0pAU38N4D+WemwKNs=
|
github.com/beevik/etree v1.4.1/go.mod h1:gPNJNaBGVZ9AwsidazFZyygnd+0pAU38N4D+WemwKNs=
|
||||||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
||||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
|
||||||
|
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
|
||||||
|
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
|
||||||
|
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||||
github.com/pkg/profile v1.4.0/go.mod h1:NWz/XGvpEW1FyYQ7fCx4dqYBLlfTcE+A9FLAkNKqjFE=
|
github.com/pkg/profile v1.4.0/go.mod h1:NWz/XGvpEW1FyYQ7fCx4dqYBLlfTcE+A9FLAkNKqjFE=
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
@@ -19,14 +23,18 @@ github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81P
|
|||||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||||
golang.org/x/net v0.0.0-20200904194848-62affa334b73 h1:MXfv8rhZWmFeqX3GNZRsd6vOLoaCHjYEX3qkRo3YBUA=
|
|
||||||
golang.org/x/net v0.0.0-20200904194848-62affa334b73/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
|
golang.org/x/net v0.0.0-20200904194848-62affa334b73/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
|
||||||
|
golang.org/x/net v0.53.0 h1:d+qAbo5L0orcWAr0a9JweQpjXF19LMXJE8Ey7hwOdUA=
|
||||||
|
golang.org/x/net v0.53.0/go.mod h1:JvMuJH7rrdiCfbeHoo3fCQU24Lf5JJwT9W3sJFulfgs=
|
||||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.43.0 h1:Rlag2XtaFTxp19wS8MXlJwTvoh8ArU6ezoyFsMyCTNI=
|
||||||
|
golang.org/x/sys v0.43.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
|
||||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
|
|
||||||
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
|
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
|
||||||
|
golang.org/x/text v0.36.0 h1:JfKh3XmcRPqZPKevfXVpI1wXPTqbkE5f7JA92a55Yxg=
|
||||||
|
golang.org/x/text v0.36.0/go.mod h1:NIdBknypM8iqVmPiuco0Dh6P5Jcdk8lJL0CUebqK164=
|
||||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
|
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
|
||||||
30
src/handlers_common.go
Normal file
30
src/handlers_common.go
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/url"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ProgressInfo struct {
|
||||||
|
Percentage float64
|
||||||
|
CurrentFile string
|
||||||
|
Paused bool
|
||||||
|
Status string
|
||||||
|
}
|
||||||
|
|
||||||
|
var speedLimitRegex = regexp.MustCompile(`^([1-9]\d*(\.\d+)?)(KBps|MBps|GBps)$`)
|
||||||
|
|
||||||
|
func withToken(path string) string {
|
||||||
|
token := strings.TrimSpace(config.Security.AuthToken)
|
||||||
|
if token == "" {
|
||||||
|
return path
|
||||||
|
}
|
||||||
|
|
||||||
|
separator := "?"
|
||||||
|
if strings.Contains(path, "?") {
|
||||||
|
separator = "&"
|
||||||
|
}
|
||||||
|
|
||||||
|
return path + separator + "token=" + url.QueryEscape(token)
|
||||||
|
}
|
||||||
174
src/handlers_jobs.go
Normal file
174
src/handlers_jobs.go
Normal file
@@ -0,0 +1,174 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func handlePause(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if !ensureMethod(w, r, http.MethodPost) || !ensureAuthorized(w, r) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
filename := r.URL.Query().Get("filename")
|
||||||
|
if filename == "" {
|
||||||
|
logger.LogError("Pause Handler", "Filename is required")
|
||||||
|
http.Error(w, "Filename is required", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
jobInfo, exists := getJob(filename)
|
||||||
|
if !exists {
|
||||||
|
logger.LogError("Pause Handler", "Job not found")
|
||||||
|
http.Error(w, "Job not found", http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
jobInfo.SetPaused(true)
|
||||||
|
logger.LogJobState(filename, "pausing")
|
||||||
|
jobInfo.KillProcess()
|
||||||
|
|
||||||
|
paused := true
|
||||||
|
setProgressStatus(filename, &paused, "paused")
|
||||||
|
|
||||||
|
_, _ = fmt.Fprintf(w, "Pause signal sent for %s", filename)
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleResume(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if !ensureMethod(w, r, http.MethodPost) || !ensureAuthorized(w, r) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
filename := r.URL.Query().Get("filename")
|
||||||
|
if filename == "" {
|
||||||
|
http.Error(w, "Filename is required", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
jobInfo, exists := getJob(filename)
|
||||||
|
if !exists {
|
||||||
|
http.Error(w, "Job not found", http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
jobInfo.SetPaused(false)
|
||||||
|
jobInfo.SignalResume()
|
||||||
|
|
||||||
|
paused := false
|
||||||
|
setProgressStatus(filename, &paused, "running")
|
||||||
|
|
||||||
|
_, _ = fmt.Fprintf(w, "Resume signal sent for %s", filename)
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleAbort(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if !ensureMethod(w, r, http.MethodPost) || !ensureAuthorized(w, r) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
filename := r.URL.Query().Get("filename")
|
||||||
|
if filename == "" {
|
||||||
|
http.Error(w, "Filename is required", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
jobInfo, exists := getJob(filename)
|
||||||
|
if !exists {
|
||||||
|
http.Error(w, "Job not found", http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
jobInfo.Abort()
|
||||||
|
jobInfo.KillProcess()
|
||||||
|
|
||||||
|
if tempDir := jobInfo.GetTempDir(); tempDir != "" {
|
||||||
|
_ = os.RemoveAll(tempDir)
|
||||||
|
}
|
||||||
|
|
||||||
|
paused := false
|
||||||
|
setProgressStatus(filename, &paused, "aborted")
|
||||||
|
|
||||||
|
_, _ = fmt.Fprintf(w, "Abort signal sent for %s", filename)
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleClearCompleted(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if !ensureMethod(w, r, http.MethodPost) || !ensureAuthorized(w, r) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
clearCompletedJobs()
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
_ = json.NewEncoder(w).Encode(map[string]bool{"success": true})
|
||||||
|
}
|
||||||
|
|
||||||
|
func clearCompletedJobs() {
|
||||||
|
progressMutex.Lock()
|
||||||
|
defer progressMutex.Unlock()
|
||||||
|
|
||||||
|
for filename, info := range progress {
|
||||||
|
if info.Percentage >= 100 || info.Status == "failed" || info.Status == "aborted" {
|
||||||
|
delete(progress, filename)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func updateProgress(filename string, value float64, currentFile, status string) {
|
||||||
|
paused := false
|
||||||
|
if jobInfo, exists := getJob(filename); exists {
|
||||||
|
paused = jobInfo.IsPaused()
|
||||||
|
}
|
||||||
|
|
||||||
|
progressMutex.Lock()
|
||||||
|
defer progressMutex.Unlock()
|
||||||
|
|
||||||
|
if existingProgress, ok := progress[filename]; ok {
|
||||||
|
existingProgress.Percentage = value
|
||||||
|
existingProgress.CurrentFile = currentFile
|
||||||
|
existingProgress.Paused = paused
|
||||||
|
if status != "" {
|
||||||
|
existingProgress.Status = status
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
progress[filename] = &ProgressInfo{
|
||||||
|
Percentage: value,
|
||||||
|
CurrentFile: currentFile,
|
||||||
|
Paused: paused,
|
||||||
|
Status: status,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleSetSpeedLimit(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if !ensureMethod(w, r, http.MethodPost) || !ensureAuthorized(w, r) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.LogInfo("Set Speed Limit", "Received request to set speed limit")
|
||||||
|
|
||||||
|
var requestData struct {
|
||||||
|
SpeedLimit string `json:"speedLimit"`
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := json.NewDecoder(r.Body).Decode(&requestData); err != nil {
|
||||||
|
logger.LogError("Set Speed Limit", "Invalid request body")
|
||||||
|
http.Error(w, "Invalid request", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
requestData.SpeedLimit = strings.TrimSpace(requestData.SpeedLimit)
|
||||||
|
if requestData.SpeedLimit == "unlimited" {
|
||||||
|
setGlobalSpeedLimit("")
|
||||||
|
} else {
|
||||||
|
if !speedLimitRegex.MatchString(requestData.SpeedLimit) {
|
||||||
|
http.Error(w, "Invalid speed limit format", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
setGlobalSpeedLimit(requestData.SpeedLimit)
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.LogInfo("Set Speed Limit", fmt.Sprintf("Global speed limit set to: %s", getGlobalSpeedLimit()))
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
}
|
||||||
128
src/handlers_pages.go
Normal file
128
src/handlers_pages.go
Normal file
@@ -0,0 +1,128 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func handleRoot(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if !ensureMethod(w, r, http.MethodGet) || !ensureAuthorized(w, r) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err := templates.ExecuteTemplate(w, "index", struct {
|
||||||
|
Jobs map[string]ProgressInfo
|
||||||
|
GlobalSpeedLimit string
|
||||||
|
AuthToken string
|
||||||
|
Nonce string
|
||||||
|
}{
|
||||||
|
Jobs: snapshotProgress(),
|
||||||
|
GlobalSpeedLimit: getGlobalSpeedLimit(),
|
||||||
|
AuthToken: url.QueryEscape(config.Security.AuthToken),
|
||||||
|
Nonce: cspNonce(r),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
logger.LogError("Handle Root", fmt.Sprintf("Error executing template: %v", err))
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleSelect(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if !ensureMethod(w, r, http.MethodGet) || !ensureAuthorized(w, r) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
filesParam := r.URL.Query().Get("files")
|
||||||
|
filenames := strings.Split(filesParam, ",")
|
||||||
|
|
||||||
|
allItems := make(map[string]map[string][]Item)
|
||||||
|
|
||||||
|
for _, filename := range filenames {
|
||||||
|
if filename == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
fullPath, pathErr := safeUploadPath(filename)
|
||||||
|
if pathErr != nil {
|
||||||
|
logger.LogError("Handle Select", fmt.Sprintf("Invalid filename %s: %v", filename, pathErr))
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := os.Stat(fullPath); os.IsNotExist(err) {
|
||||||
|
logger.LogError("Handle Select", fmt.Sprintf("File does not exist: %s", fullPath))
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
items, err := parseInputFile(fullPath)
|
||||||
|
if err != nil {
|
||||||
|
logger.LogError("Handle Select", fmt.Sprintf("Error parsing input file: %v", err))
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
sortItems(items)
|
||||||
|
|
||||||
|
groupedItems := groupItemsBySeason(items)
|
||||||
|
allItems[filename] = groupedItems
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(allItems) == 0 {
|
||||||
|
logger.LogError("Handle Select", "No valid files were processed")
|
||||||
|
http.Error(w, "No valid files were processed", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err := templates.ExecuteTemplate(w, "select", struct {
|
||||||
|
Filenames string
|
||||||
|
AllItems map[string]map[string][]Item
|
||||||
|
AuthToken string
|
||||||
|
Nonce string
|
||||||
|
}{
|
||||||
|
Filenames: filesParam,
|
||||||
|
AllItems: allItems,
|
||||||
|
AuthToken: url.QueryEscape(config.Security.AuthToken),
|
||||||
|
Nonce: cspNonce(r),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
logger.LogError("Handle Select", fmt.Sprintf("Error executing template: %v", err))
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleProgress(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if !ensureMethod(w, r, http.MethodGet) || !ensureAuthorized(w, r) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
filename := r.URL.Query().Get("filename")
|
||||||
|
|
||||||
|
if r.Header.Get("Accept") == "application/json" {
|
||||||
|
progressInfo := getProgress(filename)
|
||||||
|
|
||||||
|
if progressInfo == nil {
|
||||||
|
w.WriteHeader(http.StatusNotFound)
|
||||||
|
_ = json.NewEncoder(w).Encode(map[string]string{"error": "No progress information found"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
err := json.NewEncoder(w).Encode(progressInfo)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err := templates.ExecuteTemplate(w, "progress", struct {
|
||||||
|
Filename string
|
||||||
|
AuthToken string
|
||||||
|
Nonce string
|
||||||
|
}{Filename: filename, AuthToken: url.QueryEscape(config.Security.AuthToken), Nonce: cspNonce(r)})
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
}
|
||||||
158
src/handlers_upload_process.go
Normal file
158
src/handlers_upload_process.go
Normal file
@@ -0,0 +1,158 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func handleUpload(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if !ensureMethod(w, r, http.MethodPost) || !ensureAuthorized(w, r) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.LogInfo("Handle Upload", "Starting file upload")
|
||||||
|
r.Body = http.MaxBytesReader(w, r.Body, maxUploadBytes())
|
||||||
|
const multipartMemory = 4 << 20
|
||||||
|
err := r.ParseMultipartForm(multipartMemory)
|
||||||
|
if err != nil {
|
||||||
|
logger.LogError("Handle Upload", fmt.Sprintf("Error parsing multipart form: %v", err))
|
||||||
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
files := r.MultipartForm.File["files"]
|
||||||
|
if len(files) == 0 {
|
||||||
|
logger.LogError("Handle Upload", "No files uploaded")
|
||||||
|
http.Error(w, "No files uploaded", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
uploadedFiles := []string{}
|
||||||
|
|
||||||
|
for _, fileHeader := range files {
|
||||||
|
if strings.ToLower(filepath.Ext(fileHeader.Filename)) != ".drmd" {
|
||||||
|
http.Error(w, "Only .drmd files are allowed", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
file, err := fileHeader.Open()
|
||||||
|
if err != nil {
|
||||||
|
logger.LogError("Handle Upload", fmt.Sprintf("Error opening file: %v", err))
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
pattern := sanitizeFilename(fileHeader.Filename) + "_*.drmd"
|
||||||
|
tempFile, err := os.CreateTemp(uploadDir, pattern)
|
||||||
|
if err != nil {
|
||||||
|
_ = file.Close()
|
||||||
|
logger.LogError("Handle Upload", fmt.Sprintf("Error creating temporary file: %v", err))
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = io.Copy(tempFile, file)
|
||||||
|
_ = file.Close()
|
||||||
|
if err != nil {
|
||||||
|
_ = tempFile.Close()
|
||||||
|
_ = os.Remove(tempFile.Name())
|
||||||
|
logger.LogError("Handle Upload", fmt.Sprintf("Error copying file: %v", err))
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := tempFile.Close(); err != nil {
|
||||||
|
_ = os.Remove(tempFile.Name())
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
uploadedFiles = append(uploadedFiles, filepath.Base(tempFile.Name()))
|
||||||
|
|
||||||
|
_, err = parseInputFile(tempFile.Name())
|
||||||
|
if err != nil {
|
||||||
|
_ = os.Remove(tempFile.Name())
|
||||||
|
logger.LogError("Handle Upload", fmt.Sprintf("Error parsing input file: %v", err))
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
validFiles := []string{}
|
||||||
|
for _, file := range uploadedFiles {
|
||||||
|
if file != "" {
|
||||||
|
validFiles = append(validFiles, file)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(validFiles) == 0 {
|
||||||
|
logger.LogError("Handle Upload", "No valid files were uploaded")
|
||||||
|
http.Error(w, "No valid files were uploaded", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.LogInfo("Handle Upload", fmt.Sprintf("Redirecting to select with files: %v", validFiles))
|
||||||
|
http.Redirect(w, r, withToken("/select?files="+url.QueryEscape(strings.Join(validFiles, ","))), http.StatusSeeOther)
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleProcess(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if !ensureMethod(w, r, http.MethodPost) || !ensureAuthorized(w, r) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.LogInfo("Handle Process", "Starting process")
|
||||||
|
if err := r.ParseForm(); err != nil {
|
||||||
|
logger.LogError("Handle Process", fmt.Sprintf("Error parsing form: %v", err))
|
||||||
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
selectedItems := r.Form["items"]
|
||||||
|
if len(selectedItems) == 0 {
|
||||||
|
logger.LogError("Handle Process", "No items selected")
|
||||||
|
http.Error(w, "No items selected", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
itemsByFile := make(map[string][]string)
|
||||||
|
for _, item := range selectedItems {
|
||||||
|
parts := strings.SplitN(item, ":", 2)
|
||||||
|
if len(parts) != 2 {
|
||||||
|
logger.LogError("Handle Process", "Invalid item format")
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
filename, itemName := parts[0], parts[1]
|
||||||
|
itemsByFile[filename] = append(itemsByFile[filename], itemName)
|
||||||
|
}
|
||||||
|
|
||||||
|
for filename, items := range itemsByFile {
|
||||||
|
logger.LogInfo("Handle Process", fmt.Sprintf("Processing file: %s", filename))
|
||||||
|
fullPath, pathErr := safeUploadPath(filename)
|
||||||
|
if pathErr != nil {
|
||||||
|
http.Error(w, pathErr.Error(), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
allItems, err := parseInputFile(fullPath)
|
||||||
|
if err != nil {
|
||||||
|
logger.LogError("Handle Process", fmt.Sprintf("Error parsing input file: %v", err))
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
selectedItems := filterSelectedItems(allItems, items)
|
||||||
|
sortItems(selectedItems)
|
||||||
|
go func(targetFilename string, targetItems []Item) {
|
||||||
|
if err := processItems(targetFilename, targetItems); err != nil {
|
||||||
|
logger.LogError("Handle Process", fmt.Sprintf("Error processing %s: %v", targetFilename, err))
|
||||||
|
}
|
||||||
|
}(filename, selectedItems)
|
||||||
|
}
|
||||||
|
|
||||||
|
http.Redirect(w, r, withToken("/"), http.StatusSeeOther)
|
||||||
|
}
|
||||||
148
src/handlers_ws.go
Normal file
148
src/handlers_ws.go
Normal file
@@ -0,0 +1,148 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/gorilla/websocket"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
wsSendBuffer = 64
|
||||||
|
wsWriteWait = 10 * time.Second
|
||||||
|
)
|
||||||
|
|
||||||
|
var upgrader = websocket.Upgrader{
|
||||||
|
CheckOrigin: func(r *http.Request) bool {
|
||||||
|
origin := strings.TrimSpace(r.Header.Get("Origin"))
|
||||||
|
if origin == "" {
|
||||||
|
return strings.TrimSpace(config.Security.AuthToken) == ""
|
||||||
|
}
|
||||||
|
parsedOrigin, err := url.Parse(origin)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return strings.EqualFold(parsedOrigin.Host, r.Host)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
type wsClient struct {
|
||||||
|
conn *websocket.Conn
|
||||||
|
send chan []byte
|
||||||
|
once sync.Once
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *wsClient) close() {
|
||||||
|
c.once.Do(func() {
|
||||||
|
close(c.send)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
var clients = make(map[string]map[*wsClient]struct{})
|
||||||
|
var mu sync.Mutex
|
||||||
|
|
||||||
|
func handleWebSocket(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if !ensureMethod(w, r, http.MethodGet) || !ensureAuthorized(w, r) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if !config.General.EnableConsole {
|
||||||
|
http.Error(w, "Console output is disabled", http.StatusForbidden)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
filename := r.URL.Query().Get("filename")
|
||||||
|
if filename == "" {
|
||||||
|
http.Error(w, "Filename is required", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
conn, err := upgrader.Upgrade(w, r, nil)
|
||||||
|
if err != nil {
|
||||||
|
logger.LogError("WebSocket", fmt.Sprintf("Error while upgrading connection: %v", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.LogInfo("WebSocket", fmt.Sprintf("WebSocket connection established for filename: %s", filename))
|
||||||
|
|
||||||
|
client := &wsClient{conn: conn, send: make(chan []byte, wsSendBuffer)}
|
||||||
|
|
||||||
|
mu.Lock()
|
||||||
|
if clients[filename] == nil {
|
||||||
|
clients[filename] = make(map[*wsClient]struct{})
|
||||||
|
}
|
||||||
|
clients[filename][client] = struct{}{}
|
||||||
|
mu.Unlock()
|
||||||
|
|
||||||
|
go writePump(filename, client)
|
||||||
|
|
||||||
|
for {
|
||||||
|
if _, _, err := conn.NextReader(); err != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mu.Lock()
|
||||||
|
if set, ok := clients[filename]; ok {
|
||||||
|
if _, exists := set[client]; exists {
|
||||||
|
delete(set, client)
|
||||||
|
client.close()
|
||||||
|
}
|
||||||
|
if len(set) == 0 {
|
||||||
|
delete(clients, filename)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
mu.Unlock()
|
||||||
|
_ = conn.Close()
|
||||||
|
|
||||||
|
logger.LogInfo("WebSocket", fmt.Sprintf("WebSocket connection closed for filename: %s", filename))
|
||||||
|
}
|
||||||
|
|
||||||
|
func writePump(filename string, client *wsClient) {
|
||||||
|
defer client.conn.Close()
|
||||||
|
|
||||||
|
for message := range client.send {
|
||||||
|
if err := client.conn.SetWriteDeadline(time.Now().Add(wsWriteWait)); err != nil {
|
||||||
|
logger.LogError("WebSocket", fmt.Sprintf("SetWriteDeadline error: %v", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err := client.conn.WriteMessage(websocket.TextMessage, message); err != nil {
|
||||||
|
logger.LogError("Broadcast", fmt.Sprintf("Error writing to client for %s: %v", filename, err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func broadcast(filename string, message []byte) {
|
||||||
|
if !config.General.EnableConsole {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
mu.Lock()
|
||||||
|
set := clients[filename]
|
||||||
|
targets := make([]*wsClient, 0, len(set))
|
||||||
|
for c := range set {
|
||||||
|
targets = append(targets, c)
|
||||||
|
}
|
||||||
|
mu.Unlock()
|
||||||
|
|
||||||
|
for _, client := range targets {
|
||||||
|
select {
|
||||||
|
case client.send <- message:
|
||||||
|
default:
|
||||||
|
mu.Lock()
|
||||||
|
if set, ok := clients[filename]; ok {
|
||||||
|
if _, exists := set[client]; exists {
|
||||||
|
delete(set, client)
|
||||||
|
client.close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
mu.Unlock()
|
||||||
|
logger.LogError("Broadcast", fmt.Sprintf("Dropping slow client for %s", filename))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
109
src/integration_test.go
Normal file
109
src/integration_test.go
Normal file
@@ -0,0 +1,109 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func resetStateForTest() {
|
||||||
|
jobsMutex.Lock()
|
||||||
|
jobs = make(map[string]*JobInfo)
|
||||||
|
jobsMutex.Unlock()
|
||||||
|
|
||||||
|
progressMutex.Lock()
|
||||||
|
progress = make(map[string]*ProgressInfo)
|
||||||
|
progressMutex.Unlock()
|
||||||
|
|
||||||
|
setGlobalSpeedLimit("")
|
||||||
|
config = Config{}
|
||||||
|
setDefaultConfigValues()
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAuthTokenProtection(t *testing.T) {
|
||||||
|
resetStateForTest()
|
||||||
|
config.Security.AuthToken = "secret"
|
||||||
|
|
||||||
|
handler := newRouter()
|
||||||
|
|
||||||
|
req := httptest.NewRequest(http.MethodPost, "/clear-completed", nil)
|
||||||
|
rr := httptest.NewRecorder()
|
||||||
|
handler.ServeHTTP(rr, req)
|
||||||
|
|
||||||
|
if rr.Code != http.StatusUnauthorized {
|
||||||
|
t.Fatalf("expected status %d, got %d", http.StatusUnauthorized, rr.Code)
|
||||||
|
}
|
||||||
|
|
||||||
|
req = httptest.NewRequest(http.MethodPost, "/clear-completed?token=secret", nil)
|
||||||
|
rr = httptest.NewRecorder()
|
||||||
|
handler.ServeHTTP(rr, req)
|
||||||
|
|
||||||
|
if rr.Code != http.StatusOK {
|
||||||
|
t.Fatalf("expected status %d, got %d", http.StatusOK, rr.Code)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPauseResumeAbortFlow(t *testing.T) {
|
||||||
|
resetStateForTest()
|
||||||
|
handler := newRouter()
|
||||||
|
|
||||||
|
filename := "job.drmd"
|
||||||
|
setJob(filename, NewJobInfo())
|
||||||
|
updateProgress(filename, 10, "episode1", "running")
|
||||||
|
|
||||||
|
req := httptest.NewRequest(http.MethodPost, "/pause?filename="+filename, nil)
|
||||||
|
rr := httptest.NewRecorder()
|
||||||
|
handler.ServeHTTP(rr, req)
|
||||||
|
if rr.Code != http.StatusOK {
|
||||||
|
t.Fatalf("pause expected %d got %d", http.StatusOK, rr.Code)
|
||||||
|
}
|
||||||
|
|
||||||
|
job, ok := getJob(filename)
|
||||||
|
if !ok || !job.IsPaused() {
|
||||||
|
t.Fatalf("expected paused job state")
|
||||||
|
}
|
||||||
|
|
||||||
|
progressInfo := getProgress(filename)
|
||||||
|
if progressInfo == nil || progressInfo.Status != "paused" {
|
||||||
|
t.Fatalf("expected paused progress state")
|
||||||
|
}
|
||||||
|
|
||||||
|
req = httptest.NewRequest(http.MethodPost, "/resume?filename="+filename, nil)
|
||||||
|
rr = httptest.NewRecorder()
|
||||||
|
handler.ServeHTTP(rr, req)
|
||||||
|
if rr.Code != http.StatusOK {
|
||||||
|
t.Fatalf("resume expected %d got %d", http.StatusOK, rr.Code)
|
||||||
|
}
|
||||||
|
|
||||||
|
if job.IsPaused() {
|
||||||
|
t.Fatalf("expected resumed job state")
|
||||||
|
}
|
||||||
|
|
||||||
|
progressInfo = getProgress(filename)
|
||||||
|
if progressInfo == nil || progressInfo.Status != "running" {
|
||||||
|
t.Fatalf("expected running progress state")
|
||||||
|
}
|
||||||
|
|
||||||
|
req = httptest.NewRequest(http.MethodPost, "/abort?filename="+filename, nil)
|
||||||
|
rr = httptest.NewRecorder()
|
||||||
|
handler.ServeHTTP(rr, req)
|
||||||
|
if rr.Code != http.StatusOK {
|
||||||
|
t.Fatalf("abort expected %d got %d", http.StatusOK, rr.Code)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !job.IsAborted() {
|
||||||
|
t.Fatalf("expected aborted job state")
|
||||||
|
}
|
||||||
|
|
||||||
|
progressInfo = getProgress(filename)
|
||||||
|
if progressInfo == nil || progressInfo.Status != "aborted" {
|
||||||
|
t.Fatalf("expected aborted progress state")
|
||||||
|
}
|
||||||
|
|
||||||
|
req = httptest.NewRequest(http.MethodPost, "/abort?filename="+filename, nil)
|
||||||
|
rr = httptest.NewRecorder()
|
||||||
|
handler.ServeHTTP(rr, req)
|
||||||
|
if rr.Code != http.StatusOK {
|
||||||
|
t.Fatalf("second abort expected %d got %d", http.StatusOK, rr.Code)
|
||||||
|
}
|
||||||
|
}
|
||||||
36
src/logger.go
Normal file
36
src/logger.go
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Logger struct {
|
||||||
|
*log.Logger
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
Reset = "\033[0m"
|
||||||
|
Red = "\033[31m"
|
||||||
|
Green = "\033[32m"
|
||||||
|
Yellow = "\033[33m"
|
||||||
|
Blue = "\033[34m"
|
||||||
|
)
|
||||||
|
|
||||||
|
func NewLogger(prefix string) *Logger {
|
||||||
|
return &Logger{
|
||||||
|
Logger: log.New(os.Stdout, prefix, log.Ldate|log.Ltime),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Logger) LogInfo(jobName, message string) {
|
||||||
|
l.Printf("%s[INFO] [%s] %s%s", Green, jobName, message, Reset)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Logger) LogError(jobName, message string) {
|
||||||
|
l.Printf("%s[ERROR] [%s] %s%s", Red, jobName, message, Reset)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Logger) LogJobState(jobName, state string) {
|
||||||
|
l.Printf("%s[JOB STATE] [%s] %s%s", Yellow, jobName, state, Reset)
|
||||||
|
}
|
||||||
162
src/main.go
Normal file
162
src/main.go
Normal file
@@ -0,0 +1,162 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"html/template"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"syscall"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"embed"
|
||||||
|
)
|
||||||
|
|
||||||
|
var logger *Logger
|
||||||
|
|
||||||
|
type Item struct {
|
||||||
|
MPD string
|
||||||
|
Keys string
|
||||||
|
Filename string
|
||||||
|
Description string
|
||||||
|
Subtitles string
|
||||||
|
Poster string
|
||||||
|
Metadata string
|
||||||
|
}
|
||||||
|
|
||||||
|
type Items struct {
|
||||||
|
Items []Item
|
||||||
|
}
|
||||||
|
|
||||||
|
type Metadata struct {
|
||||||
|
Title string
|
||||||
|
Type string
|
||||||
|
Season string
|
||||||
|
}
|
||||||
|
|
||||||
|
const uploadDir = "uploads"
|
||||||
|
|
||||||
|
var templates *template.Template
|
||||||
|
|
||||||
|
//go:embed templates
|
||||||
|
var templateFS embed.FS
|
||||||
|
|
||||||
|
var globalSpeedLimit string
|
||||||
|
var globalSpeedLimitMutex sync.RWMutex
|
||||||
|
|
||||||
|
func getGlobalSpeedLimit() string {
|
||||||
|
globalSpeedLimitMutex.RLock()
|
||||||
|
defer globalSpeedLimitMutex.RUnlock()
|
||||||
|
return globalSpeedLimit
|
||||||
|
}
|
||||||
|
|
||||||
|
func setGlobalSpeedLimit(value string) {
|
||||||
|
globalSpeedLimitMutex.Lock()
|
||||||
|
defer globalSpeedLimitMutex.Unlock()
|
||||||
|
globalSpeedLimit = value
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
if err := os.MkdirAll(uploadDir, 0755); err != nil {
|
||||||
|
fmt.Printf("Error creating upload directory: %v\n", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
templates = template.Must(template.ParseFS(templateFS, "templates/*"))
|
||||||
|
|
||||||
|
logger = NewLogger("")
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
configPath := flag.String("config", "config.toml", "Path to config file")
|
||||||
|
inputFile := flag.String("f", "", "Path to the input JSON file")
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
|
loadConfig(*configPath)
|
||||||
|
|
||||||
|
if *inputFile == "" {
|
||||||
|
go watchFolder()
|
||||||
|
startWebServer()
|
||||||
|
} else {
|
||||||
|
items, err := parseInputFile(*inputFile)
|
||||||
|
if err != nil {
|
||||||
|
logger.LogError("Main", fmt.Sprintf("Error parsing input file: %v", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err := processItems(*inputFile, items); err != nil {
|
||||||
|
logger.LogError("Main", fmt.Sprintf("Error processing items: %v", err))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func startWebServer() {
|
||||||
|
server := &http.Server{
|
||||||
|
Addr: serverAddr(),
|
||||||
|
Handler: newRouter(),
|
||||||
|
ReadTimeout: time.Duration(config.Server.ReadTimeoutSec) * time.Second,
|
||||||
|
ReadHeaderTimeout: time.Duration(config.Server.ReadHeaderTimeoutS) * time.Second,
|
||||||
|
WriteTimeout: time.Duration(config.Server.WriteTimeoutSec) * time.Second,
|
||||||
|
IdleTimeout: time.Duration(config.Server.IdleTimeoutSec) * time.Second,
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||||
|
defer stop()
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
<-ctx.Done()
|
||||||
|
logger.LogInfo("Main", "Shutdown signal received, aborting jobs")
|
||||||
|
abortAllJobs()
|
||||||
|
|
||||||
|
shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
if err := server.Shutdown(shutdownCtx); err != nil {
|
||||||
|
logger.LogError("Main", fmt.Sprintf("Server shutdown error: %v", err))
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
logger.LogInfo("Main", fmt.Sprintf("Starting web server on http://%s", server.Addr))
|
||||||
|
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
||||||
|
logger.LogError("Main", fmt.Sprintf("Web server error: %v", err))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func abortAllJobs() {
|
||||||
|
jobsMutex.RLock()
|
||||||
|
jobList := make([]*JobInfo, 0, len(jobs))
|
||||||
|
for _, j := range jobs {
|
||||||
|
jobList = append(jobList, j)
|
||||||
|
}
|
||||||
|
jobsMutex.RUnlock()
|
||||||
|
|
||||||
|
for _, j := range jobList {
|
||||||
|
j.Abort()
|
||||||
|
j.KillProcess()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func getKeys(keys string) []string {
|
||||||
|
parts := strings.Split(keys, ",")
|
||||||
|
out := parts[:0]
|
||||||
|
for _, p := range parts {
|
||||||
|
p = strings.TrimSpace(p)
|
||||||
|
if p != "" {
|
||||||
|
out = append(out, p)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseMetadata(metadata string) Metadata {
|
||||||
|
parts := strings.Split(metadata, ";")
|
||||||
|
if len(parts) != 3 {
|
||||||
|
return Metadata{}
|
||||||
|
}
|
||||||
|
return Metadata{
|
||||||
|
Title: strings.TrimSpace(parts[0]),
|
||||||
|
Type: strings.TrimSpace(parts[1]),
|
||||||
|
Season: "S" + strings.TrimSpace(parts[2]),
|
||||||
|
}
|
||||||
|
}
|
||||||
176
src/main_test.go
Normal file
176
src/main_test.go
Normal file
@@ -0,0 +1,176 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSanitizeFilename(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
input string
|
||||||
|
expected string
|
||||||
|
}{
|
||||||
|
{"file:name.mp4", "file_name.mp4"},
|
||||||
|
{"file/name.mp4", "file_name.mp4"},
|
||||||
|
{"file\\name.mp4", "file_name.mp4"},
|
||||||
|
{"file?name.mp4", "file_name.mp4"},
|
||||||
|
{"file*name.mp4", "file_name.mp4"},
|
||||||
|
{"file<name>.mp4", "file_name_.mp4"},
|
||||||
|
{".hidden", "hidden"},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
result := sanitizeFilename(test.input)
|
||||||
|
if result != test.expected {
|
||||||
|
t.Errorf("sanitizeFilename(%q) = %q, want %q", test.input, result, test.expected)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIsValidURL(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
input string
|
||||||
|
expected bool
|
||||||
|
}{
|
||||||
|
{"https://example.com", true},
|
||||||
|
{"http://example.com", true},
|
||||||
|
{"ftp://example.com", true},
|
||||||
|
{"not a url", false},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
result := isValidURL(test.input)
|
||||||
|
if result != test.expected {
|
||||||
|
t.Errorf("isValidURL(%q) = %v, want %v", test.input, result, test.expected)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseMetadata(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
input string
|
||||||
|
expected Metadata
|
||||||
|
}{
|
||||||
|
{"Show Title; serie; 01", Metadata{Title: "Show Title", Type: "serie", Season: "S01"}},
|
||||||
|
{"Movie Title; movie; ", Metadata{Title: "Movie Title", Type: "movie", Season: "S"}},
|
||||||
|
{"Invalid Metadata", Metadata{}},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
result := parseMetadata(test.input)
|
||||||
|
if !reflect.DeepEqual(result, test.expected) {
|
||||||
|
t.Errorf("parseMetadata(%q) = %v, want %v", test.input, result, test.expected)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseInputFile(t *testing.T) {
|
||||||
|
tempFile, err := os.CreateTemp("", "test_input_*.json")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to create temp file: %v", err)
|
||||||
|
}
|
||||||
|
defer os.Remove(tempFile.Name())
|
||||||
|
|
||||||
|
testData := Items{
|
||||||
|
Items: []Item{
|
||||||
|
{MPD: "http://example.com/video1.mpd", Filename: "video1.mp4"},
|
||||||
|
{MPD: "http://example.com/video2.mpd", Filename: "video2.mp4"},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
jsonData, _ := json.Marshal(testData)
|
||||||
|
if _, err := tempFile.Write(jsonData); err != nil {
|
||||||
|
t.Fatalf("Failed to write to temp file: %v", err)
|
||||||
|
}
|
||||||
|
tempFile.Close()
|
||||||
|
|
||||||
|
items, err := parseInputFile(tempFile.Name())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("parseInputFile() returned an error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(items) != len(testData.Items) {
|
||||||
|
t.Errorf("parseInputFile() returned %d items, want %d", len(items), len(testData.Items))
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, item := range items {
|
||||||
|
if !reflect.DeepEqual(item, testData.Items[i]) {
|
||||||
|
t.Errorf("parseInputFile() item %d = %v, want %v", i, item, testData.Items[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGroupItemsBySeason(t *testing.T) {
|
||||||
|
items := []Item{
|
||||||
|
{Filename: "show1_s01e01.mp4", Metadata: "Show 1; serie; 01"},
|
||||||
|
{Filename: "show1_s01e02.mp4", Metadata: "Show 1; serie; 01"},
|
||||||
|
{Filename: "show2_s01e01.mp4", Metadata: "Show 2; serie; 01"},
|
||||||
|
{Filename: "movie1.mp4", Metadata: "Movie 1; movie; "},
|
||||||
|
}
|
||||||
|
|
||||||
|
grouped := groupItemsBySeason(items)
|
||||||
|
|
||||||
|
expectedGroups := map[string]int{
|
||||||
|
"Show 1 - S01": 2,
|
||||||
|
"Show 2 - S01": 1,
|
||||||
|
"Movies": 1,
|
||||||
|
}
|
||||||
|
|
||||||
|
for group, count := range expectedGroups {
|
||||||
|
if len(grouped[group]) != count {
|
||||||
|
t.Errorf("groupItemsBySeason() group %q has %d items, want %d", group, len(grouped[group]), count)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSafeUploadPath(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
input string
|
||||||
|
wantError bool
|
||||||
|
}{
|
||||||
|
{name: "valid filename", input: "file.drmd", wantError: false},
|
||||||
|
{name: "directory traversal", input: "../file.drmd", wantError: true},
|
||||||
|
{name: "empty", input: "", wantError: true},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tests {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
path, err := safeUploadPath(tc.input)
|
||||||
|
if tc.wantError && err == nil {
|
||||||
|
t.Fatalf("expected error for input %q", tc.input)
|
||||||
|
}
|
||||||
|
if !tc.wantError {
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
if filepath.Base(path) != tc.input {
|
||||||
|
t.Fatalf("expected basename %q, got %q", tc.input, filepath.Base(path))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestValidateRemoteURL(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
input string
|
||||||
|
wantError bool
|
||||||
|
}{
|
||||||
|
{name: "reject localhost", input: "http://localhost/test.vtt", wantError: true},
|
||||||
|
{name: "reject invalid scheme", input: "file:///tmp/test", wantError: true},
|
||||||
|
{name: "reject malformed", input: "::://", wantError: true},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tests {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
_, err := validateRemoteURL(tc.input)
|
||||||
|
if tc.wantError && err == nil {
|
||||||
|
t.Fatalf("expected error for %q", tc.input)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
19
src/router.go
Normal file
19
src/router.go
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "net/http"
|
||||||
|
|
||||||
|
func newRouter() http.Handler {
|
||||||
|
mux := http.NewServeMux()
|
||||||
|
mux.HandleFunc("/", handleRoot)
|
||||||
|
mux.HandleFunc("/upload", handleUpload)
|
||||||
|
mux.HandleFunc("/select", handleSelect)
|
||||||
|
mux.HandleFunc("/process", handleProcess)
|
||||||
|
mux.HandleFunc("/progress", handleProgress)
|
||||||
|
mux.HandleFunc("/abort", handleAbort)
|
||||||
|
mux.HandleFunc("/pause", handlePause)
|
||||||
|
mux.HandleFunc("/resume", handleResume)
|
||||||
|
mux.HandleFunc("/clear-completed", handleClearCompleted)
|
||||||
|
mux.HandleFunc("/ws", handleWebSocket)
|
||||||
|
mux.HandleFunc("/set-speed-limit", handleSetSpeedLimit)
|
||||||
|
return withSecurityHeaders(mux)
|
||||||
|
}
|
||||||
237
src/security.go
Normal file
237
src/security.go
Normal file
@@ -0,0 +1,237 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"crypto/rand"
|
||||||
|
"crypto/subtle"
|
||||||
|
"encoding/base64"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
var blockedCIDRs = mustParseCIDRs([]string{
|
||||||
|
"127.0.0.0/8",
|
||||||
|
"10.0.0.0/8",
|
||||||
|
"172.16.0.0/12",
|
||||||
|
"192.168.0.0/16",
|
||||||
|
"169.254.0.0/16",
|
||||||
|
"100.64.0.0/10",
|
||||||
|
"0.0.0.0/8",
|
||||||
|
"::1/128",
|
||||||
|
"fc00::/7",
|
||||||
|
"fe80::/10",
|
||||||
|
})
|
||||||
|
|
||||||
|
var secureDialer = &net.Dialer{
|
||||||
|
Timeout: 10 * time.Second,
|
||||||
|
KeepAlive: 30 * time.Second,
|
||||||
|
}
|
||||||
|
|
||||||
|
var secureTransport = &http.Transport{
|
||||||
|
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
|
||||||
|
host, port, err := net.SplitHostPort(addr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
ips, err := net.DefaultResolver.LookupIPAddr(ctx, host)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
for _, ip := range ips {
|
||||||
|
if isBlockedIP(ip.IP) {
|
||||||
|
return nil, fmt.Errorf("dial blocked: %s resolves to %s", host, ip.IP)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(ips) == 0 {
|
||||||
|
return nil, fmt.Errorf("no IPs for %s", host)
|
||||||
|
}
|
||||||
|
return secureDialer.DialContext(ctx, network, net.JoinHostPort(ips[0].IP.String(), port))
|
||||||
|
},
|
||||||
|
TLSHandshakeTimeout: 10 * time.Second,
|
||||||
|
ResponseHeaderTimeout: 30 * time.Second,
|
||||||
|
}
|
||||||
|
|
||||||
|
var secureHTTPClient = &http.Client{
|
||||||
|
Timeout: 30 * time.Second,
|
||||||
|
Transport: secureTransport,
|
||||||
|
CheckRedirect: func(req *http.Request, _ []*http.Request) error {
|
||||||
|
_, err := validateRemoteURL(req.URL.String())
|
||||||
|
return err
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
const maxRemoteFetchBytes int64 = 20 << 20
|
||||||
|
|
||||||
|
func mustParseCIDRs(cidrs []string) []*net.IPNet {
|
||||||
|
networks := make([]*net.IPNet, 0, len(cidrs))
|
||||||
|
for _, cidr := range cidrs {
|
||||||
|
_, network, err := net.ParseCIDR(cidr)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
networks = append(networks, network)
|
||||||
|
}
|
||||||
|
return networks
|
||||||
|
}
|
||||||
|
|
||||||
|
func maxUploadBytes() int64 {
|
||||||
|
return int64(config.General.MaxUploadMB) << 20
|
||||||
|
}
|
||||||
|
|
||||||
|
func serverAddr() string {
|
||||||
|
return fmt.Sprintf("%s:%d", config.Server.Host, config.Server.Port)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ensureMethod(w http.ResponseWriter, r *http.Request, method string) bool {
|
||||||
|
if r.Method != method {
|
||||||
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func ensureAuthorized(w http.ResponseWriter, r *http.Request) bool {
|
||||||
|
token := strings.TrimSpace(config.Security.AuthToken)
|
||||||
|
if token == "" {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
providedToken := strings.TrimSpace(r.Header.Get("X-DRMD-Token"))
|
||||||
|
if providedToken == "" {
|
||||||
|
providedToken = strings.TrimSpace(r.URL.Query().Get("token"))
|
||||||
|
}
|
||||||
|
|
||||||
|
if subtle.ConstantTimeCompare([]byte(providedToken), []byte(token)) != 1 {
|
||||||
|
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func validateRemoteURL(rawURL string) (*url.URL, error) {
|
||||||
|
parsedURL, err := url.Parse(strings.TrimSpace(rawURL))
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("invalid URL: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if parsedURL.Scheme != "http" && parsedURL.Scheme != "https" {
|
||||||
|
return nil, errors.New("only http(s) URLs are allowed")
|
||||||
|
}
|
||||||
|
|
||||||
|
host := parsedURL.Hostname()
|
||||||
|
if host == "" {
|
||||||
|
return nil, errors.New("URL host is required")
|
||||||
|
}
|
||||||
|
|
||||||
|
if strings.EqualFold(host, "localhost") {
|
||||||
|
return nil, errors.New("localhost URLs are not allowed")
|
||||||
|
}
|
||||||
|
|
||||||
|
ips, err := net.LookupIP(host)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("unable to resolve URL host: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, ip := range ips {
|
||||||
|
if isBlockedIP(ip) {
|
||||||
|
return nil, fmt.Errorf("URL host resolves to blocked IP range: %s", ip.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return parsedURL, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func isBlockedIP(ip net.IP) bool {
|
||||||
|
if ip.IsLoopback() || ip.IsPrivate() || ip.IsUnspecified() || ip.IsLinkLocalMulticast() || ip.IsLinkLocalUnicast() || ip.IsMulticast() {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, network := range blockedCIDRs {
|
||||||
|
if network.Contains(ip) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func fetchRemoteContent(rawURL string) ([]byte, error) {
|
||||||
|
validatedURL, err := validateRemoteURL(rawURL)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
req, err := http.NewRequest(http.MethodGet, validatedURL.String(), nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := secureHTTPClient.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||||
|
return nil, fmt.Errorf("unexpected status code %d", resp.StatusCode)
|
||||||
|
}
|
||||||
|
|
||||||
|
if resp.ContentLength > maxRemoteFetchBytes {
|
||||||
|
return nil, fmt.Errorf("remote content too large: %d bytes", resp.ContentLength)
|
||||||
|
}
|
||||||
|
|
||||||
|
limited := io.LimitReader(resp.Body, maxRemoteFetchBytes+1)
|
||||||
|
body, err := io.ReadAll(limited)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if int64(len(body)) > maxRemoteFetchBytes {
|
||||||
|
return nil, fmt.Errorf("remote content exceeded %d bytes", maxRemoteFetchBytes)
|
||||||
|
}
|
||||||
|
|
||||||
|
return body, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type ctxKey string
|
||||||
|
|
||||||
|
const nonceCtxKey ctxKey = "csp-nonce"
|
||||||
|
|
||||||
|
func generateNonce() string {
|
||||||
|
var b [16]byte
|
||||||
|
if _, err := rand.Read(b[:]); err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return base64.RawStdEncoding.EncodeToString(b[:])
|
||||||
|
}
|
||||||
|
|
||||||
|
func cspNonce(r *http.Request) string {
|
||||||
|
if v, ok := r.Context().Value(nonceCtxKey).(string); ok {
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func withSecurityHeaders(next http.Handler) http.Handler {
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
nonce := generateNonce()
|
||||||
|
w.Header().Set("X-Content-Type-Options", "nosniff")
|
||||||
|
w.Header().Set("X-Frame-Options", "DENY")
|
||||||
|
w.Header().Set("Referrer-Policy", "no-referrer")
|
||||||
|
csp := fmt.Sprintf(
|
||||||
|
"default-src 'self'; connect-src 'self'; img-src 'self' data:; style-src 'self' 'nonce-%s'; script-src 'self' 'nonce-%s'; base-uri 'self'; form-action 'self'; frame-ancestors 'none'",
|
||||||
|
nonce, nonce,
|
||||||
|
)
|
||||||
|
w.Header().Set("Content-Security-Policy", csp)
|
||||||
|
|
||||||
|
ctx := context.WithValue(r.Context(), nonceCtxKey, nonce)
|
||||||
|
next.ServeHTTP(w, r.WithContext(ctx))
|
||||||
|
})
|
||||||
|
}
|
||||||
67
src/state.go
Normal file
67
src/state.go
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "sync"
|
||||||
|
|
||||||
|
var (
|
||||||
|
jobsMutex sync.RWMutex
|
||||||
|
jobs = make(map[string]*JobInfo)
|
||||||
|
|
||||||
|
progressMutex sync.RWMutex
|
||||||
|
progress = make(map[string]*ProgressInfo)
|
||||||
|
)
|
||||||
|
|
||||||
|
func setJob(filename string, jobInfo *JobInfo) {
|
||||||
|
jobsMutex.Lock()
|
||||||
|
defer jobsMutex.Unlock()
|
||||||
|
jobs[filename] = jobInfo
|
||||||
|
}
|
||||||
|
|
||||||
|
func getJob(filename string) (*JobInfo, bool) {
|
||||||
|
jobsMutex.RLock()
|
||||||
|
defer jobsMutex.RUnlock()
|
||||||
|
jobInfo, ok := jobs[filename]
|
||||||
|
return jobInfo, ok
|
||||||
|
}
|
||||||
|
|
||||||
|
func removeJob(filename string) {
|
||||||
|
jobsMutex.Lock()
|
||||||
|
defer jobsMutex.Unlock()
|
||||||
|
delete(jobs, filename)
|
||||||
|
}
|
||||||
|
|
||||||
|
func getProgress(filename string) *ProgressInfo {
|
||||||
|
progressMutex.RLock()
|
||||||
|
defer progressMutex.RUnlock()
|
||||||
|
if p, ok := progress[filename]; ok {
|
||||||
|
snapshot := *p
|
||||||
|
return &snapshot
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func snapshotProgress() map[string]ProgressInfo {
|
||||||
|
progressMutex.RLock()
|
||||||
|
defer progressMutex.RUnlock()
|
||||||
|
|
||||||
|
result := make(map[string]ProgressInfo, len(progress))
|
||||||
|
for filename, info := range progress {
|
||||||
|
result[filename] = *info
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func setProgressStatus(filename string, paused *bool, status string) {
|
||||||
|
progressMutex.Lock()
|
||||||
|
defer progressMutex.Unlock()
|
||||||
|
info, ok := progress[filename]
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if paused != nil {
|
||||||
|
info.Paused = *paused
|
||||||
|
}
|
||||||
|
if status != "" {
|
||||||
|
info.Status = status
|
||||||
|
}
|
||||||
|
}
|
||||||
73
src/subtitles.go
Normal file
73
src/subtitles.go
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/asticode/go-astisub"
|
||||||
|
)
|
||||||
|
|
||||||
|
func downloadAndConvertSubtitles(subtitlesURLs string) ([]string, error) {
|
||||||
|
var subtitlePaths []string
|
||||||
|
urls := strings.Split(subtitlesURLs, ",")
|
||||||
|
|
||||||
|
for _, url := range urls {
|
||||||
|
logger.LogInfo("Subtitle Download", fmt.Sprintf("Downloading subtitle from %s", url))
|
||||||
|
vttPath, err := downloadSubtitle(url)
|
||||||
|
if err != nil {
|
||||||
|
logger.LogError("Subtitle Download", fmt.Sprintf("Error downloading subtitle: %v", err))
|
||||||
|
return nil, fmt.Errorf("error downloading subtitle: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
srtPath, err := convertVTTtoSRT(vttPath)
|
||||||
|
if err != nil {
|
||||||
|
logger.LogError("Subtitle Download", fmt.Sprintf("Error converting subtitle: %v", err))
|
||||||
|
return nil, fmt.Errorf("error converting subtitle: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
subtitlePaths = append(subtitlePaths, srtPath)
|
||||||
|
}
|
||||||
|
|
||||||
|
return subtitlePaths, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func downloadSubtitle(url string) (string, error) {
|
||||||
|
logger.LogInfo("Download Subtitle", fmt.Sprintf("Starting download from %s", url))
|
||||||
|
body, err := fetchRemoteContent(url)
|
||||||
|
if err != nil {
|
||||||
|
logger.LogError("Download Subtitle", fmt.Sprintf("Error getting subtitle URL: %v", err))
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
tempFile, err := os.CreateTemp("", "subtitle_*.vtt")
|
||||||
|
if err != nil {
|
||||||
|
logger.LogError("Download Subtitle", fmt.Sprintf("Error creating temp file: %v", err))
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
defer tempFile.Close()
|
||||||
|
|
||||||
|
_, err = io.Copy(tempFile, bytes.NewReader(body))
|
||||||
|
if err != nil {
|
||||||
|
logger.LogError("Download Subtitle", fmt.Sprintf("Error copying to temp file: %v", err))
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.LogInfo("Download Subtitle", "Subtitle downloaded successfully")
|
||||||
|
return tempFile.Name(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func convertVTTtoSRT(vttPath string) (string, error) {
|
||||||
|
srtPath := strings.TrimSuffix(vttPath, ".vtt") + ".srt"
|
||||||
|
s1, err := astisub.OpenFile(vttPath)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
if err := s1.Write(srtPath); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
logger.LogInfo("Convert VTT to SRT", fmt.Sprintf("Converted %s to %s", vttPath, srtPath))
|
||||||
|
return srtPath, nil
|
||||||
|
}
|
||||||
323
src/templates/index
Normal file
323
src/templates/index
Normal file
@@ -0,0 +1,323 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>Simple Downloader</title>
|
||||||
|
<style nonce="{{.Nonce}}">
|
||||||
|
body {
|
||||||
|
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
|
||||||
|
background-color: #1e1e1e;
|
||||||
|
color: #d4d4d4;
|
||||||
|
line-height: 1.6;
|
||||||
|
padding: 20px;
|
||||||
|
max-width: 900px;
|
||||||
|
margin: 0 auto;
|
||||||
|
box-sizing: border-box;
|
||||||
|
}
|
||||||
|
h1, h2 {
|
||||||
|
border-bottom: 1px solid #333;
|
||||||
|
padding-bottom: 10px;
|
||||||
|
word-wrap: break-word;
|
||||||
|
}
|
||||||
|
form {
|
||||||
|
margin-bottom: 20px;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
}
|
||||||
|
input[type="file"], input[type="submit"] {
|
||||||
|
background-color: #2d2d2d;
|
||||||
|
color: #d4d4d4;
|
||||||
|
padding: 8px 12px;
|
||||||
|
border-radius: 4px;
|
||||||
|
margin-bottom: 10px;
|
||||||
|
max-width: 100%;
|
||||||
|
}
|
||||||
|
input[type="submit"] {
|
||||||
|
cursor: pointer;
|
||||||
|
background-color: #4CAF50;
|
||||||
|
color: white;
|
||||||
|
}
|
||||||
|
input[type="submit"]:hover {
|
||||||
|
background-color: #45a049;
|
||||||
|
}
|
||||||
|
ul {
|
||||||
|
list-style-type: none;
|
||||||
|
padding: 0;
|
||||||
|
margin-bottom: 10px;
|
||||||
|
}
|
||||||
|
li {
|
||||||
|
background-color: #2d2d2d;
|
||||||
|
margin-bottom: 10px;
|
||||||
|
padding: 10px;
|
||||||
|
border-radius: 4px;
|
||||||
|
word-wrap: break-word;
|
||||||
|
}
|
||||||
|
.job-title {
|
||||||
|
font-size: 1.1em;
|
||||||
|
font-weight: bold;
|
||||||
|
margin-bottom: 5px;
|
||||||
|
}
|
||||||
|
.job-title a {
|
||||||
|
color: #58a6ff;
|
||||||
|
text-decoration: none;
|
||||||
|
}
|
||||||
|
.job-title a:hover {
|
||||||
|
text-decoration: underline;
|
||||||
|
}
|
||||||
|
.job-info {
|
||||||
|
font-size: 0.9em;
|
||||||
|
color: #a0a0a0;
|
||||||
|
}
|
||||||
|
.progress-text {
|
||||||
|
display: inline-block;
|
||||||
|
width: 5em;
|
||||||
|
}
|
||||||
|
.paused {
|
||||||
|
color: #ffa500;
|
||||||
|
}
|
||||||
|
.speed-limit {
|
||||||
|
font-size: 1em;
|
||||||
|
color: #a0a0a0;
|
||||||
|
margin-top: 10px;
|
||||||
|
}
|
||||||
|
@media (max-width: 600px) {
|
||||||
|
body {
|
||||||
|
padding: 10px;
|
||||||
|
}
|
||||||
|
h1, h2 {
|
||||||
|
font-size: 1.5em;
|
||||||
|
}
|
||||||
|
input[type="file"], input[type="submit"] {
|
||||||
|
font-size: 16px;
|
||||||
|
}
|
||||||
|
input[type="submit"], #clear-completed {
|
||||||
|
font-size: 16px;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
input[type="submit"], #clear-completed {
|
||||||
|
cursor: pointer;
|
||||||
|
color: white;
|
||||||
|
border: 1px solid #444;
|
||||||
|
padding: 8px 12px;
|
||||||
|
border-radius: 4px;
|
||||||
|
margin-bottom: 10px;
|
||||||
|
max-width: 100%;
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
|
#clear-completed {
|
||||||
|
background-color: #f44336;
|
||||||
|
}
|
||||||
|
#clear-completed:hover {
|
||||||
|
background-color: #d32f2f;
|
||||||
|
}
|
||||||
|
/* New CSS for speed limit form */
|
||||||
|
.settings-section {
|
||||||
|
margin-top: 30px;
|
||||||
|
}
|
||||||
|
.speed-limit-form {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: space-between;
|
||||||
|
gap: 10px;
|
||||||
|
margin-bottom: 20px;
|
||||||
|
}
|
||||||
|
.speed-limit-form .form-group {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 10px;
|
||||||
|
}
|
||||||
|
.speed-limit-form input[type="number"],
|
||||||
|
.speed-limit-form select,
|
||||||
|
.speed-limit-form button {
|
||||||
|
background-color: #2d2d2d;
|
||||||
|
color: #d4d4d4;
|
||||||
|
border: 1px solid #444;
|
||||||
|
padding: 8px 12px;
|
||||||
|
border-radius: 4px;
|
||||||
|
}
|
||||||
|
.speed-limit-form button {
|
||||||
|
cursor: pointer;
|
||||||
|
background-color: #4CAF50;
|
||||||
|
color: white;
|
||||||
|
}
|
||||||
|
.speed-limit-form button:hover {
|
||||||
|
background-color: #45a049;
|
||||||
|
}
|
||||||
|
.speed-limit-container {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
margin-bottom: 20px;
|
||||||
|
background-color: #2d2d2d;
|
||||||
|
padding: 8px 12px;
|
||||||
|
border-radius: 4px;
|
||||||
|
}
|
||||||
|
.speed-limit-container .form-group {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 10px;
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
|
.speed-limit-container input[type="number"] {
|
||||||
|
background-color: #2d2d2d;
|
||||||
|
color: #d4d4d4;
|
||||||
|
border: 1px solid #444;
|
||||||
|
padding: 8px 12px;
|
||||||
|
border-radius: 4px;
|
||||||
|
height: 40px;
|
||||||
|
box-sizing: border-box;
|
||||||
|
flex-grow: 1;
|
||||||
|
}
|
||||||
|
.speed-limit-container select,
|
||||||
|
.speed-limit-container button {
|
||||||
|
background-color: #2d2d2d;
|
||||||
|
color: #d4d4d4;
|
||||||
|
border: 1px solid #444;
|
||||||
|
padding: 8px 12px;
|
||||||
|
border-radius: 4px;
|
||||||
|
height: 40px;
|
||||||
|
box-sizing: border-box;
|
||||||
|
}
|
||||||
|
.speed-limit-container button {
|
||||||
|
cursor: pointer;
|
||||||
|
background-color: #4CAF50;
|
||||||
|
color: white;
|
||||||
|
}
|
||||||
|
.speed-limit-container button:hover {
|
||||||
|
background-color: #45a049;
|
||||||
|
}
|
||||||
|
.speed-limit-container .speed-limit {
|
||||||
|
color: #d4d4d4;
|
||||||
|
margin-left: auto;
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
}
|
||||||
|
.speed-limit-container .speed-limit span {
|
||||||
|
margin-left: 5px;
|
||||||
|
}
|
||||||
|
.current-speed-limit {
|
||||||
|
color: #d4d4d4;
|
||||||
|
margin-top: 10px;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>Simple Downloader</h1>
|
||||||
|
<form action="{{if .AuthToken}}/upload?token={{.AuthToken}}{{else}}/upload{{end}}" method="post" enctype="multipart/form-data">
|
||||||
|
<input type="file" name="files" accept=".drmd" multiple>
|
||||||
|
<input type="submit" value="Upload and Process">
|
||||||
|
</form>
|
||||||
|
<h2>Currently Running Jobs</h2>
|
||||||
|
<ul>
|
||||||
|
{{range $filename, $info := .Jobs}}
|
||||||
|
<li>
|
||||||
|
<div class="job-title">
|
||||||
|
<a href="{{if $.AuthToken}}/progress?filename={{$filename}}&token={{$.AuthToken}}{{else}}/progress?filename={{$filename}}{{end}}">{{$filename}}</a>
|
||||||
|
</div>
|
||||||
|
<div class="job-info">
|
||||||
|
Progress: <span class="progress-text">{{printf "%5.1f%%" $info.Percentage}}</span>
|
||||||
|
Current file: {{$info.CurrentFile}}
|
||||||
|
Status: {{$info.Status}}
|
||||||
|
{{if $info.Paused}}
|
||||||
|
<span class="paused">(Paused)</span>
|
||||||
|
{{end}}
|
||||||
|
</div>
|
||||||
|
</li>
|
||||||
|
{{else}}
|
||||||
|
<li>No active jobs</li>
|
||||||
|
{{end}}
|
||||||
|
</ul>
|
||||||
|
<button id="clear-completed">Clear Completed Jobs</button>
|
||||||
|
|
||||||
|
<div class="settings-section">
|
||||||
|
<h2>Settings</h2>
|
||||||
|
<div class="speed-limit-container">
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="speedLimitValue">Speed Limit:</label>
|
||||||
|
<input type="number" id="speedLimitValue" name="speedLimitValue" min="0" step="0.01" required>
|
||||||
|
<select id="speedLimitUnit" name="speedLimitUnit">
|
||||||
|
<option value="GBps">GBps</option>
|
||||||
|
<option value="MBps" selected>MBps</option>
|
||||||
|
<option value="KBps">KBps</option>
|
||||||
|
</select>
|
||||||
|
<button id="set-speed-limit" type="button">Set Limit</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<script nonce="{{.Nonce}}">
|
||||||
|
const authToken = "{{.AuthToken}}";
|
||||||
|
const currentSpeedLimitRaw = "{{if .GlobalSpeedLimit}}{{.GlobalSpeedLimit}}{{else}}0{{end}}";
|
||||||
|
|
||||||
|
function withToken(path) {
|
||||||
|
if (!authToken) {
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
|
const separator = path.includes('?') ? '&' : '?';
|
||||||
|
return `${path}${separator}token=${authToken}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
function clearCompleted() {
|
||||||
|
fetch(withToken('/clear-completed'), { method: 'POST' })
|
||||||
|
.then(response => response.json())
|
||||||
|
.then(data => {
|
||||||
|
if (data.success) {
|
||||||
|
location.reload();
|
||||||
|
} else {
|
||||||
|
alert('Failed to clear completed jobs');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function updateSpeedLimit(event) {
|
||||||
|
event.preventDefault();
|
||||||
|
|
||||||
|
const speedLimitValue = document.getElementById('speedLimitValue').value;
|
||||||
|
const speedLimitUnit = document.getElementById('speedLimitUnit').value;
|
||||||
|
const speedLimit = speedLimitValue === "0" ? "unlimited" : speedLimitValue + speedLimitUnit;
|
||||||
|
|
||||||
|
if (!validateSpeedLimit(speedLimitValue)) {
|
||||||
|
alert('Please enter a valid speed limit.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
fetch(withToken('/set-speed-limit'), {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
},
|
||||||
|
body: JSON.stringify({ speedLimit }),
|
||||||
|
}).then(response => {
|
||||||
|
if (response.ok) {
|
||||||
|
alert('Speed limit updated successfully');
|
||||||
|
} else {
|
||||||
|
alert('Failed to update speed limit');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function validateSpeedLimit(value) {
|
||||||
|
const number = parseFloat(value);
|
||||||
|
return !isNaN(number) && number >= 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
document.addEventListener('DOMContentLoaded', function() {
|
||||||
|
const speedLimitValueInput = document.getElementById('speedLimitValue');
|
||||||
|
const speedLimitUnitSelect = document.getElementById('speedLimitUnit');
|
||||||
|
|
||||||
|
const match = currentSpeedLimitRaw.match(/(\d+(\.\d+)?)([A-Za-z]+)/);
|
||||||
|
if (match) {
|
||||||
|
speedLimitValueInput.value = match[1];
|
||||||
|
speedLimitUnitSelect.value = match[3];
|
||||||
|
} else {
|
||||||
|
speedLimitValueInput.value = "0";
|
||||||
|
speedLimitUnitSelect.value = "MBps";
|
||||||
|
}
|
||||||
|
|
||||||
|
document.getElementById('clear-completed').addEventListener('click', clearCompleted);
|
||||||
|
document.getElementById('set-speed-limit').addEventListener('click', updateSpeedLimit);
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
262
src/templates/progress
Normal file
262
src/templates/progress
Normal file
@@ -0,0 +1,262 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>Processing {{.Filename}}</title>
|
||||||
|
<style nonce="{{.Nonce}}">
|
||||||
|
body {
|
||||||
|
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
|
||||||
|
background-color: #1e1e1e;
|
||||||
|
color: #d4d4d4;
|
||||||
|
line-height: 1.6;
|
||||||
|
padding: 20px;
|
||||||
|
max-width: 900px;
|
||||||
|
margin: 0 auto;
|
||||||
|
box-sizing: border-box;
|
||||||
|
}
|
||||||
|
h1 {
|
||||||
|
border-bottom: 1px solid #333;
|
||||||
|
padding-bottom: 10px;
|
||||||
|
word-wrap: break-word;
|
||||||
|
}
|
||||||
|
#progress-container {
|
||||||
|
background-color: #2d2d2d;
|
||||||
|
border-radius: 4px;
|
||||||
|
margin-bottom: 20px;
|
||||||
|
padding: 20px;
|
||||||
|
}
|
||||||
|
#progress-bar-container {
|
||||||
|
background-color: #444;
|
||||||
|
height: 20px;
|
||||||
|
border-radius: 10px;
|
||||||
|
overflow: hidden;
|
||||||
|
margin-bottom: 10px;
|
||||||
|
position: relative;
|
||||||
|
}
|
||||||
|
#progress-bar {
|
||||||
|
background-color: #4CAF50;
|
||||||
|
height: 100%;
|
||||||
|
width: 0;
|
||||||
|
transition: width 0.5s ease-in-out;
|
||||||
|
}
|
||||||
|
#progress-text {
|
||||||
|
position: absolute;
|
||||||
|
top: 50%;
|
||||||
|
left: 0;
|
||||||
|
right: 0;
|
||||||
|
transform: translateY(-50%);
|
||||||
|
text-align: center;
|
||||||
|
color: #fff;
|
||||||
|
font-weight: bold;
|
||||||
|
text-shadow: 1px 1px 2px rgba(0,0,0,0.5);
|
||||||
|
line-height: 20px;
|
||||||
|
}
|
||||||
|
#currentFile {
|
||||||
|
margin-top: 10px;
|
||||||
|
word-wrap: break-word;
|
||||||
|
}
|
||||||
|
#abort-button {
|
||||||
|
background-color: #f44336;
|
||||||
|
color: white;
|
||||||
|
border: none;
|
||||||
|
padding: 10px 15px;
|
||||||
|
margin-top: 10px;
|
||||||
|
border-radius: 4px;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
#abort-button:hover {
|
||||||
|
background-color: #d32f2f;
|
||||||
|
}
|
||||||
|
#pause-button, #resume-button, #toggle-console {
|
||||||
|
background-color: #4CAF50;
|
||||||
|
color: white;
|
||||||
|
border: none;
|
||||||
|
padding: 10px 15px;
|
||||||
|
margin-top: 10px;
|
||||||
|
border-radius: 4px;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
#pause-button:hover, #resume-button:hover, #toggle-console:hover {
|
||||||
|
background-color: #45a049;
|
||||||
|
}
|
||||||
|
#resume-button {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
#back-button {
|
||||||
|
background-color: #2196F3;
|
||||||
|
color: white;
|
||||||
|
border: none;
|
||||||
|
padding: 10px 15px;
|
||||||
|
margin-top: 10px;
|
||||||
|
border-radius: 4px;
|
||||||
|
cursor: pointer;
|
||||||
|
float: right;
|
||||||
|
}
|
||||||
|
#back-button:hover {
|
||||||
|
background-color: #1976D2;
|
||||||
|
}
|
||||||
|
#console {
|
||||||
|
display: none; /* Initially hidden */
|
||||||
|
background-color: black;
|
||||||
|
color: white;
|
||||||
|
height: 300px; /* Adjust height as needed */
|
||||||
|
overflow-y: scroll;
|
||||||
|
white-space: pre; /* Preserve whitespace */
|
||||||
|
font-family: monospace; /* Use monospace font */
|
||||||
|
margin-top: 10px;
|
||||||
|
border: 1px solid #ccc;
|
||||||
|
}
|
||||||
|
@media (max-width: 600px) {
|
||||||
|
body {
|
||||||
|
padding: 10px;
|
||||||
|
}
|
||||||
|
h1 {
|
||||||
|
font-size: 1.5em;
|
||||||
|
}
|
||||||
|
#progress-container {
|
||||||
|
padding: 10px;
|
||||||
|
}
|
||||||
|
#progress-bar-container {
|
||||||
|
height: 15px;
|
||||||
|
}
|
||||||
|
#progress-text {
|
||||||
|
font-size: 0.9em;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>Processing {{.Filename}}</h1>
|
||||||
|
<div id="progress-container">
|
||||||
|
<div id="progress-bar-container">
|
||||||
|
<div id="progress-bar"></div>
|
||||||
|
<div id="progress-text">0%</div>
|
||||||
|
</div>
|
||||||
|
<div id="currentFile"></div>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<button id="abort-button">Abort Download</button>
|
||||||
|
<button id="pause-button">Pause Download</button>
|
||||||
|
<button id="resume-button">Resume Download</button>
|
||||||
|
<button id="toggle-console">Toggle Console View</button>
|
||||||
|
<button id="back-button">Back to Index</button>
|
||||||
|
</div>
|
||||||
|
<div id="console"></div>
|
||||||
|
<script nonce="{{.Nonce}}">
|
||||||
|
let isPaused = false;
|
||||||
|
const filename = "{{.Filename}}";
|
||||||
|
const authToken = "{{.AuthToken}}";
|
||||||
|
|
||||||
|
function withToken(path) {
|
||||||
|
if (!authToken) {
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
|
const separator = path.includes('?') ? '&' : '?';
|
||||||
|
return `${path}${separator}token=${authToken}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
function updateProgress() {
|
||||||
|
fetch(withToken(`/progress?filename=${encodeURIComponent(filename)}`), {
|
||||||
|
headers: {
|
||||||
|
'Accept': 'application/json'
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.then(response => response.json())
|
||||||
|
.then(data => {
|
||||||
|
const progress = Math.round(data.Percentage);
|
||||||
|
document.getElementById('progress-bar').style.width = progress + '%';
|
||||||
|
document.getElementById('progress-text').innerText = progress + '%';
|
||||||
|
document.getElementById('currentFile').innerText = 'Current file: ' + (data.CurrentFile || 'None');
|
||||||
|
|
||||||
|
isPaused = data.Paused;
|
||||||
|
updatePauseResumeButtons();
|
||||||
|
|
||||||
|
if (progress < 100 && !isPaused) {
|
||||||
|
setTimeout(updateProgress, 1000);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function updatePauseResumeButtons() {
|
||||||
|
if (isPaused) {
|
||||||
|
document.getElementById('pause-button').style.display = 'none';
|
||||||
|
document.getElementById('resume-button').style.display = 'inline-block';
|
||||||
|
} else {
|
||||||
|
document.getElementById('pause-button').style.display = 'inline-block';
|
||||||
|
document.getElementById('resume-button').style.display = 'none';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function abortDownload() {
|
||||||
|
fetch(withToken(`/abort?filename=${encodeURIComponent(filename)}`), { method: 'POST' })
|
||||||
|
.then(response => {
|
||||||
|
if (response.ok) {
|
||||||
|
console.log('Abort signal sent. The download will stop soon.');
|
||||||
|
} else {
|
||||||
|
alert('Failed to abort the download.');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function pauseDownload() {
|
||||||
|
fetch(withToken(`/pause?filename=${encodeURIComponent(filename)}`), { method: 'POST' })
|
||||||
|
.then(response => {
|
||||||
|
if (response.ok) {
|
||||||
|
console.log('Pause signal sent. The download will pause soon.');
|
||||||
|
isPaused = true;
|
||||||
|
updatePauseResumeButtons();
|
||||||
|
} else {
|
||||||
|
alert('Failed to pause the download.');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function resumeDownload() {
|
||||||
|
fetch(withToken(`/resume?filename=${encodeURIComponent(filename)}`), { method: 'POST' })
|
||||||
|
.then(response => {
|
||||||
|
if (response.ok) {
|
||||||
|
console.log('Resume signal sent. The download will resume soon.');
|
||||||
|
isPaused = false;
|
||||||
|
updatePauseResumeButtons();
|
||||||
|
updateProgress();
|
||||||
|
} else {
|
||||||
|
alert('Failed to resume the download.');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const consoleDiv = document.getElementById('console');
|
||||||
|
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
|
||||||
|
const ws = new WebSocket(`${protocol}//${window.location.host}${withToken(`/ws?filename=${encodeURIComponent(filename)}`)}`);
|
||||||
|
|
||||||
|
ws.onmessage = function(event) {
|
||||||
|
consoleDiv.textContent += event.data;
|
||||||
|
consoleDiv.scrollTop = consoleDiv.scrollHeight;
|
||||||
|
};
|
||||||
|
|
||||||
|
ws.onclose = function() {
|
||||||
|
console.log('WebSocket connection closed');
|
||||||
|
};
|
||||||
|
|
||||||
|
ws.onerror = function(error) {
|
||||||
|
console.error('WebSocket error:', error);
|
||||||
|
};
|
||||||
|
|
||||||
|
document.addEventListener('DOMContentLoaded', function() {
|
||||||
|
document.getElementById('abort-button').addEventListener('click', abortDownload);
|
||||||
|
document.getElementById('pause-button').addEventListener('click', pauseDownload);
|
||||||
|
document.getElementById('resume-button').addEventListener('click', resumeDownload);
|
||||||
|
document.getElementById('back-button').addEventListener('click', function() {
|
||||||
|
window.location.href = withToken('/');
|
||||||
|
});
|
||||||
|
document.getElementById('toggle-console').addEventListener('click', function() {
|
||||||
|
consoleDiv.style.display = consoleDiv.style.display === "none" ? "block" : "none";
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
updateProgress();
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
130
src/templates/select
Normal file
130
src/templates/select
Normal file
@@ -0,0 +1,130 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>Select Items to Download</title>
|
||||||
|
<style nonce="{{.Nonce}}">
|
||||||
|
body {
|
||||||
|
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
|
||||||
|
background-color: #1e1e1e;
|
||||||
|
color: #d4d4d4;
|
||||||
|
line-height: 1.6;
|
||||||
|
padding: 20px;
|
||||||
|
max-width: 900px;
|
||||||
|
margin: 0 auto;
|
||||||
|
box-sizing: border-box;
|
||||||
|
}
|
||||||
|
h1 {
|
||||||
|
border-bottom: 1px solid #333;
|
||||||
|
padding-bottom: 10px;
|
||||||
|
}
|
||||||
|
.season {
|
||||||
|
margin-bottom: 20px;
|
||||||
|
background-color: #2d2d2d;
|
||||||
|
padding: 10px;
|
||||||
|
border-radius: 4px;
|
||||||
|
}
|
||||||
|
.season-title {
|
||||||
|
font-size: 1.2em;
|
||||||
|
font-weight: bold;
|
||||||
|
margin-bottom: 10px;
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
}
|
||||||
|
.season-checkbox {
|
||||||
|
margin-right: 10px;
|
||||||
|
}
|
||||||
|
.item {
|
||||||
|
margin-left: 20px;
|
||||||
|
}
|
||||||
|
button, input[type="submit"] {
|
||||||
|
background-color: #4CAF50;
|
||||||
|
color: white;
|
||||||
|
border: none;
|
||||||
|
padding: 10px 15px;
|
||||||
|
margin: 5px;
|
||||||
|
border-radius: 4px;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
button:hover, input[type="submit"]:hover {
|
||||||
|
background-color: #45a049;
|
||||||
|
}
|
||||||
|
#fix-order-button {
|
||||||
|
background-color: #2196F3;
|
||||||
|
color: white;
|
||||||
|
border: none;
|
||||||
|
padding: 10px 15px;
|
||||||
|
margin: 5px;
|
||||||
|
border-radius: 4px;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
#fix-order-button:hover {
|
||||||
|
background-color: #1976D2;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>Select Items to Download</h1>
|
||||||
|
<form action="{{if .AuthToken}}/process?token={{.AuthToken}}{{else}}/process{{end}}" method="post">
|
||||||
|
<input type="hidden" name="filenames" value="{{.Filenames}}">
|
||||||
|
{{range $filename, $fileItems := .AllItems}}
|
||||||
|
<h2>{{$filename}}</h2>
|
||||||
|
{{range $season, $items := $fileItems}}
|
||||||
|
<div class="season" id="season-{{$filename}}-{{$season}}">
|
||||||
|
<div class="season-title">
|
||||||
|
<input type="checkbox" class="season-checkbox" id="season-checkbox-{{$filename}}-{{$season}}" data-season-key="{{$filename}}-{{$season}}" checked>
|
||||||
|
<label for="season-checkbox-{{$filename}}-{{$season}}">{{$season}}</label>
|
||||||
|
</div>
|
||||||
|
<div class="season-items">
|
||||||
|
{{range $item := $items}}
|
||||||
|
<div class="item">
|
||||||
|
<label>
|
||||||
|
<input type="checkbox" name="items" value="{{$filename}}:{{$item.Filename}}" checked class="episode-{{$filename}}-{{$season}}">
|
||||||
|
{{$item.Filename}}
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
|
{{end}}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{{end}}
|
||||||
|
{{end}}
|
||||||
|
<div>
|
||||||
|
<button type="button" id="select-all">Select All</button>
|
||||||
|
<button type="button" id="select-none">Select None</button>
|
||||||
|
<input type="submit" value="Start Download">
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
<script nonce="{{.Nonce}}">
|
||||||
|
function selectAll(checked) {
|
||||||
|
var checkboxes = document.getElementsByName('items');
|
||||||
|
for (var i = 0; i < checkboxes.length; i++) {
|
||||||
|
checkboxes[i].checked = checked;
|
||||||
|
}
|
||||||
|
var seasonCheckboxes = document.getElementsByClassName('season-checkbox');
|
||||||
|
for (var i = 0; i < seasonCheckboxes.length; i++) {
|
||||||
|
seasonCheckboxes[i].checked = checked;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function toggleSeason(season) {
|
||||||
|
var seasonCheckbox = document.getElementById('season-checkbox-' + season);
|
||||||
|
var episodeCheckboxes = document.getElementsByClassName('episode-' + season);
|
||||||
|
for (var i = 0; i < episodeCheckboxes.length; i++) {
|
||||||
|
episodeCheckboxes[i].checked = seasonCheckbox.checked;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
document.addEventListener('DOMContentLoaded', function() {
|
||||||
|
document.getElementById('select-all').addEventListener('click', function() { selectAll(true); });
|
||||||
|
document.getElementById('select-none').addEventListener('click', function() { selectAll(false); });
|
||||||
|
var boxes = document.getElementsByClassName('season-checkbox');
|
||||||
|
for (var i = 0; i < boxes.length; i++) {
|
||||||
|
boxes[i].addEventListener('change', function() {
|
||||||
|
toggleSeason(this.dataset.seasonKey);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
452
src/utils.go
Normal file
452
src/utils.go
Normal file
@@ -0,0 +1,452 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/url"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
|
"regexp"
|
||||||
|
"sort"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"github.com/beevik/etree"
|
||||||
|
)
|
||||||
|
|
||||||
|
type JobInfo struct {
|
||||||
|
AbortChan chan struct{}
|
||||||
|
ResumeChan chan struct{}
|
||||||
|
Cmd *exec.Cmd
|
||||||
|
TempDir string
|
||||||
|
|
||||||
|
mu sync.RWMutex
|
||||||
|
paused bool
|
||||||
|
abortOnce sync.Once
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewJobInfo() *JobInfo {
|
||||||
|
return &JobInfo{
|
||||||
|
AbortChan: make(chan struct{}),
|
||||||
|
ResumeChan: make(chan struct{}, 1),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (j *JobInfo) SetPaused(value bool) {
|
||||||
|
j.mu.Lock()
|
||||||
|
defer j.mu.Unlock()
|
||||||
|
j.paused = value
|
||||||
|
}
|
||||||
|
|
||||||
|
func (j *JobInfo) IsPaused() bool {
|
||||||
|
j.mu.RLock()
|
||||||
|
defer j.mu.RUnlock()
|
||||||
|
return j.paused
|
||||||
|
}
|
||||||
|
|
||||||
|
func (j *JobInfo) SetCmd(cmd *exec.Cmd) {
|
||||||
|
j.mu.Lock()
|
||||||
|
defer j.mu.Unlock()
|
||||||
|
j.Cmd = cmd
|
||||||
|
}
|
||||||
|
|
||||||
|
func (j *JobInfo) SetTempDir(tempDir string) {
|
||||||
|
j.mu.Lock()
|
||||||
|
defer j.mu.Unlock()
|
||||||
|
j.TempDir = tempDir
|
||||||
|
}
|
||||||
|
|
||||||
|
func (j *JobInfo) GetTempDir() string {
|
||||||
|
j.mu.RLock()
|
||||||
|
defer j.mu.RUnlock()
|
||||||
|
return j.TempDir
|
||||||
|
}
|
||||||
|
|
||||||
|
func (j *JobInfo) KillProcess() {
|
||||||
|
j.mu.RLock()
|
||||||
|
cmd := j.Cmd
|
||||||
|
j.mu.RUnlock()
|
||||||
|
|
||||||
|
if cmd != nil && cmd.Process != nil {
|
||||||
|
_ = cmd.Process.Kill()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (j *JobInfo) SignalResume() {
|
||||||
|
select {
|
||||||
|
case j.ResumeChan <- struct{}{}:
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (j *JobInfo) Abort() {
|
||||||
|
j.abortOnce.Do(func() {
|
||||||
|
close(j.AbortChan)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (j *JobInfo) IsAborted() bool {
|
||||||
|
select {
|
||||||
|
case <-j.AbortChan:
|
||||||
|
return true
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrDownloadPaused = errors.New("download paused")
|
||||||
|
ErrDownloadAborted = errors.New("download aborted")
|
||||||
|
)
|
||||||
|
|
||||||
|
var sanitizeFilenameRegex = regexp.MustCompile(`[<>:"/\\|?*\x00-\x1f]`)
|
||||||
|
|
||||||
|
var windowsReservedNames = map[string]struct{}{
|
||||||
|
"CON": {}, "PRN": {}, "AUX": {}, "NUL": {},
|
||||||
|
"COM1": {}, "COM2": {}, "COM3": {}, "COM4": {}, "COM5": {}, "COM6": {}, "COM7": {}, "COM8": {}, "COM9": {},
|
||||||
|
"LPT1": {}, "LPT2": {}, "LPT3": {}, "LPT4": {}, "LPT5": {}, "LPT6": {}, "LPT7": {}, "LPT8": {}, "LPT9": {},
|
||||||
|
}
|
||||||
|
|
||||||
|
func sanitizeFilename(filename string) string {
|
||||||
|
filename = sanitizeFilenameRegex.ReplaceAllString(filename, "_")
|
||||||
|
filename = strings.Trim(filename, ". ")
|
||||||
|
|
||||||
|
base := filename
|
||||||
|
if idx := strings.LastIndex(filename, "."); idx > 0 {
|
||||||
|
base = filename[:idx]
|
||||||
|
}
|
||||||
|
if _, reserved := windowsReservedNames[strings.ToUpper(base)]; reserved {
|
||||||
|
filename = "_" + filename
|
||||||
|
}
|
||||||
|
|
||||||
|
if filename == "" {
|
||||||
|
filename = "_"
|
||||||
|
}
|
||||||
|
return filename
|
||||||
|
}
|
||||||
|
|
||||||
|
func safeUploadPath(filename string) (string, error) {
|
||||||
|
cleanName := strings.TrimSpace(filename)
|
||||||
|
if cleanName == "" {
|
||||||
|
return "", fmt.Errorf("filename is required")
|
||||||
|
}
|
||||||
|
|
||||||
|
baseName := filepath.Base(cleanName)
|
||||||
|
if baseName != cleanName {
|
||||||
|
return "", fmt.Errorf("invalid filename")
|
||||||
|
}
|
||||||
|
|
||||||
|
if strings.Contains(baseName, "..") {
|
||||||
|
return "", fmt.Errorf("invalid filename")
|
||||||
|
}
|
||||||
|
|
||||||
|
return filepath.Join(uploadDir, baseName), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func isValidURL(toTest string) bool {
|
||||||
|
_, err := url.ParseRequestURI(toTest)
|
||||||
|
return err == nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func fixGoPlay(mpdContent string) (string, error) {
|
||||||
|
doc := etree.NewDocument()
|
||||||
|
if err := doc.ReadFromString(mpdContent); err != nil {
|
||||||
|
return "", fmt.Errorf("error parsing MPD content: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
root := doc.Root()
|
||||||
|
|
||||||
|
// Remove ad periods
|
||||||
|
for _, period := range root.SelectElements("Period") {
|
||||||
|
if strings.Contains(period.SelectAttrValue("id", ""), "-ad-") {
|
||||||
|
root.RemoveChild(period)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Find highest bandwidth for video
|
||||||
|
highestBandwidth := 0
|
||||||
|
for _, adaptationSet := range root.FindElements("//AdaptationSet") {
|
||||||
|
if strings.Contains(adaptationSet.SelectAttrValue("mimeType", ""), "video") {
|
||||||
|
for _, representation := range adaptationSet.SelectElements("Representation") {
|
||||||
|
bandwidth, _ := strconv.Atoi(representation.SelectAttrValue("bandwidth", "0"))
|
||||||
|
if bandwidth > highestBandwidth {
|
||||||
|
highestBandwidth = bandwidth
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove lower bitrate representations
|
||||||
|
for _, adaptationSet := range root.FindElements("//AdaptationSet") {
|
||||||
|
if strings.Contains(adaptationSet.SelectAttrValue("mimeType", ""), "video") {
|
||||||
|
for _, representation := range adaptationSet.SelectElements("Representation") {
|
||||||
|
bandwidth, _ := strconv.Atoi(representation.SelectAttrValue("bandwidth", "0"))
|
||||||
|
if bandwidth != highestBandwidth {
|
||||||
|
adaptationSet.RemoveChild(representation)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Combine periods
|
||||||
|
periods := root.SelectElements("Period")
|
||||||
|
if len(periods) > 1 {
|
||||||
|
firstPeriod := periods[0]
|
||||||
|
var newVideoTimeline, newAudioTimeline *etree.Element
|
||||||
|
|
||||||
|
// Find or create SegmentTimeline elements
|
||||||
|
for _, adaptationSet := range firstPeriod.SelectElements("AdaptationSet") {
|
||||||
|
mimeType := adaptationSet.SelectAttrValue("mimeType", "")
|
||||||
|
if strings.Contains(mimeType, "video") && newVideoTimeline == nil {
|
||||||
|
newVideoTimeline = findOrCreateSegmentTimeline(adaptationSet)
|
||||||
|
} else if strings.Contains(mimeType, "audio") && newAudioTimeline == nil {
|
||||||
|
newAudioTimeline = findOrCreateSegmentTimeline(adaptationSet)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, period := range periods[1:] {
|
||||||
|
for _, adaptationSet := range period.SelectElements("AdaptationSet") {
|
||||||
|
mimeType := adaptationSet.SelectAttrValue("mimeType", "")
|
||||||
|
var timeline *etree.Element
|
||||||
|
if strings.Contains(mimeType, "video") {
|
||||||
|
timeline = newVideoTimeline
|
||||||
|
} else if strings.Contains(mimeType, "audio") {
|
||||||
|
timeline = newAudioTimeline
|
||||||
|
}
|
||||||
|
|
||||||
|
if timeline != nil {
|
||||||
|
segmentTimeline := findOrCreateSegmentTimeline(adaptationSet)
|
||||||
|
for _, s := range segmentTimeline.SelectElements("S") {
|
||||||
|
timeline.AddChild(s.Copy())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
root.RemoveChild(period)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return doc.WriteToString()
|
||||||
|
}
|
||||||
|
|
||||||
|
func findOrCreateSegmentTimeline(adaptationSet *etree.Element) *etree.Element {
|
||||||
|
for _, representation := range adaptationSet.SelectElements("Representation") {
|
||||||
|
for _, segmentTemplate := range representation.SelectElements("SegmentTemplate") {
|
||||||
|
timeline := segmentTemplate.SelectElement("SegmentTimeline")
|
||||||
|
if timeline != nil {
|
||||||
|
return timeline
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If no SegmentTimeline found, create one
|
||||||
|
representation := adaptationSet.CreateElement("Representation")
|
||||||
|
segmentTemplate := representation.CreateElement("SegmentTemplate")
|
||||||
|
return segmentTemplate.CreateElement("SegmentTimeline")
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseInputFile(filename string) ([]Item, error) {
|
||||||
|
fileInfo, err := os.Stat(filename)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if fileInfo.IsDir() {
|
||||||
|
return nil, fmt.Errorf("%s is a directory", filename)
|
||||||
|
}
|
||||||
|
|
||||||
|
file, err := os.Open(filename)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
byteValue, err := io.ReadAll(file)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
byteValue = removeBOM(byteValue)
|
||||||
|
|
||||||
|
var items Items
|
||||||
|
err = json.Unmarshal(byteValue, &items)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return items.Items, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func groupItemsBySeason(items []Item) map[string][]Item {
|
||||||
|
grouped := make(map[string][]Item)
|
||||||
|
for _, item := range items {
|
||||||
|
metadata := parseMetadata(item.Metadata)
|
||||||
|
if metadata.Type == "serie" {
|
||||||
|
key := fmt.Sprintf("%s - %s", metadata.Title, metadata.Season)
|
||||||
|
grouped[key] = append(grouped[key], item)
|
||||||
|
} else {
|
||||||
|
grouped["Movies"] = append(grouped["Movies"], item)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return grouped
|
||||||
|
}
|
||||||
|
|
||||||
|
func filterSelectedItems(items []Item, selectedItems []string) []Item {
|
||||||
|
set := make(map[string]struct{}, len(selectedItems))
|
||||||
|
for _, s := range selectedItems {
|
||||||
|
set[s] = struct{}{}
|
||||||
|
}
|
||||||
|
filtered := make([]Item, 0, len(selectedItems))
|
||||||
|
for _, item := range items {
|
||||||
|
if _, ok := set[item.Filename]; ok {
|
||||||
|
filtered = append(filtered, item)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return filtered
|
||||||
|
}
|
||||||
|
|
||||||
|
func sortItems(items []Item) {
|
||||||
|
sort.Slice(items, func(i, j int) bool {
|
||||||
|
iMeta := parseMetadata(items[i].Metadata)
|
||||||
|
jMeta := parseMetadata(items[j].Metadata)
|
||||||
|
|
||||||
|
if iMeta.Title != jMeta.Title {
|
||||||
|
return iMeta.Title < jMeta.Title
|
||||||
|
}
|
||||||
|
|
||||||
|
iSeason := extractNumber(iMeta.Season)
|
||||||
|
jSeason := extractNumber(jMeta.Season)
|
||||||
|
|
||||||
|
if iSeason != jSeason {
|
||||||
|
return iSeason < jSeason
|
||||||
|
}
|
||||||
|
|
||||||
|
iEpisode := extractEpisodeNumber(items[i].Filename)
|
||||||
|
jEpisode := extractEpisodeNumber(items[j].Filename)
|
||||||
|
|
||||||
|
return iEpisode < jEpisode
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func extractNumber(s string) int {
|
||||||
|
num, _ := strconv.Atoi(strings.TrimLeft(s, "S"))
|
||||||
|
return num
|
||||||
|
}
|
||||||
|
|
||||||
|
var episodeNumberRegex = regexp.MustCompile(`(?i)S\d+E(\d+)`)
|
||||||
|
|
||||||
|
func extractEpisodeNumber(filename string) int {
|
||||||
|
match := episodeNumberRegex.FindStringSubmatch(filename)
|
||||||
|
if len(match) < 2 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
num, _ := strconv.Atoi(match[1])
|
||||||
|
return num
|
||||||
|
}
|
||||||
|
|
||||||
|
func processItems(filename string, items []Item) error {
|
||||||
|
jobInfo := NewJobInfo()
|
||||||
|
setJob(filename, jobInfo)
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
removeJob(filename)
|
||||||
|
|
||||||
|
tempDir := jobInfo.GetTempDir()
|
||||||
|
if tempDir != "" {
|
||||||
|
_ = os.RemoveAll(tempDir)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
sortItems(items)
|
||||||
|
|
||||||
|
for i := 0; i < len(items); i++ {
|
||||||
|
select {
|
||||||
|
case <-jobInfo.AbortChan:
|
||||||
|
updateProgress(filename, 100, "Aborted", "aborted")
|
||||||
|
logger.LogJobState(filename, "aborted")
|
||||||
|
return ErrDownloadAborted
|
||||||
|
default:
|
||||||
|
if jobInfo.IsPaused() {
|
||||||
|
select {
|
||||||
|
case <-jobInfo.ResumeChan:
|
||||||
|
jobInfo.SetPaused(false)
|
||||||
|
logger.LogJobState(filename, "resumed")
|
||||||
|
case <-jobInfo.AbortChan:
|
||||||
|
updateProgress(filename, 100, "Aborted", "aborted")
|
||||||
|
logger.LogJobState(filename, "aborted")
|
||||||
|
return ErrDownloadAborted
|
||||||
|
}
|
||||||
|
}
|
||||||
|
updateProgress(filename, float64(i)/float64(len(items))*100, items[i].Filename, "running")
|
||||||
|
err := downloadFile(filename, items[i], jobInfo)
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, ErrDownloadPaused) {
|
||||||
|
logger.LogJobState(filename, "paused")
|
||||||
|
if remErr := removeCompletedEpisodes(filename, items[:i]); remErr != nil {
|
||||||
|
logger.LogError("Process Items", fmt.Sprintf("Error updating partial progress file: %v", remErr))
|
||||||
|
}
|
||||||
|
i--
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if errors.Is(err, ErrDownloadAborted) {
|
||||||
|
updateProgress(filename, 100, "Aborted", "aborted")
|
||||||
|
logger.LogJobState(filename, "aborted")
|
||||||
|
return ErrDownloadAborted
|
||||||
|
}
|
||||||
|
|
||||||
|
updateProgress(filename, float64(i)/float64(len(items))*100, items[i].Filename, "failed")
|
||||||
|
logger.LogError("Process Items", fmt.Sprintf("Error downloading item %s: %v", items[i].Filename, err))
|
||||||
|
return fmt.Errorf("error downloading %s: %w", items[i].Filename, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
updateProgress(filename, 100, "", "completed")
|
||||||
|
logger.LogJobState(filename, "completed successfully")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func removeCompletedEpisodes(filename string, completedItems []Item) error {
|
||||||
|
inputFile := filepath.Join(uploadDir, filename)
|
||||||
|
items, err := parseInputFile(inputFile)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error parsing input file: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
remainingItems := make([]Item, 0)
|
||||||
|
for _, item := range items {
|
||||||
|
if !isItemCompleted(item, completedItems) || isLastCompletedItem(item, completedItems) {
|
||||||
|
remainingItems = append(remainingItems, item)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
updatedItems := Items{Items: remainingItems}
|
||||||
|
jsonData, err := json.MarshalIndent(updatedItems, "", " ")
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error marshaling updated items: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = os.WriteFile(inputFile, jsonData, 0644)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error writing updated DRMD file: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func isItemCompleted(item Item, completedItems []Item) bool {
|
||||||
|
for _, completedItem := range completedItems {
|
||||||
|
if item.Filename == completedItem.Filename {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func isLastCompletedItem(item Item, completedItems []Item) bool {
|
||||||
|
if len(completedItems) == 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return item.Filename == completedItems[len(completedItems)-1].Filename
|
||||||
|
}
|
||||||
187
src/watcher.go
Normal file
187
src/watcher.go
Normal file
@@ -0,0 +1,187 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/fsnotify/fsnotify"
|
||||||
|
)
|
||||||
|
|
||||||
|
func watchFolder() {
|
||||||
|
if config.WatchFolder.UsePolling {
|
||||||
|
go pollFolder()
|
||||||
|
}
|
||||||
|
|
||||||
|
if config.WatchFolder.UseInotify {
|
||||||
|
go inotifyWatch()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var watchedProcessing = struct {
|
||||||
|
mu sync.Mutex
|
||||||
|
files map[string]bool
|
||||||
|
}{files: make(map[string]bool)}
|
||||||
|
|
||||||
|
func beginWatching(filePath string) bool {
|
||||||
|
watchedProcessing.mu.Lock()
|
||||||
|
defer watchedProcessing.mu.Unlock()
|
||||||
|
if watchedProcessing.files[filePath] {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
watchedProcessing.files[filePath] = true
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func doneWatching(filePath string) {
|
||||||
|
watchedProcessing.mu.Lock()
|
||||||
|
defer watchedProcessing.mu.Unlock()
|
||||||
|
delete(watchedProcessing.files, filePath)
|
||||||
|
}
|
||||||
|
|
||||||
|
func inotifyWatch() {
|
||||||
|
watcher, err := fsnotify.NewWatcher()
|
||||||
|
if err != nil {
|
||||||
|
logger.LogError("Watcher", fmt.Sprintf("Failed to create inotify watcher: %v", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer watcher.Close()
|
||||||
|
|
||||||
|
done := make(chan bool)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case event, ok := <-watcher.Events:
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if event.Op&fsnotify.Create == fsnotify.Create {
|
||||||
|
if strings.HasSuffix(event.Name, ".drmd") {
|
||||||
|
logger.LogInfo("Watcher", fmt.Sprintf("New .drmd detected: %s", event.Name))
|
||||||
|
go processWatchedFile(event.Name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case err, ok := <-watcher.Errors:
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
logger.LogError("Watcher", fmt.Sprintf("Inotify error: %v", err))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
err = watcher.Add(config.WatchFolder.Path)
|
||||||
|
if err != nil {
|
||||||
|
logger.LogError("Watcher", fmt.Sprintf("Failed to add watch folder %s: %v", config.WatchFolder.Path, err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
<-done
|
||||||
|
}
|
||||||
|
|
||||||
|
func pollFolder() {
|
||||||
|
ticker := time.NewTicker(time.Duration(config.WatchFolder.PollingInterval) * time.Second)
|
||||||
|
defer ticker.Stop()
|
||||||
|
|
||||||
|
for range ticker.C {
|
||||||
|
files, err := filepath.Glob(filepath.Join(config.WatchFolder.Path, "*.drmd"))
|
||||||
|
if err != nil {
|
||||||
|
logger.LogError("Watcher", fmt.Sprintf("Error polling folder: %v", err))
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, file := range files {
|
||||||
|
logger.LogInfo("Watcher", fmt.Sprintf("New .drmd detected via polling: %s", file))
|
||||||
|
go processWatchedFile(file)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func processWatchedFile(filePath string) {
|
||||||
|
if !beginWatching(filePath) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
releaseDedupe := true
|
||||||
|
defer func() {
|
||||||
|
if releaseDedupe {
|
||||||
|
doneWatching(filePath)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
for {
|
||||||
|
initialSize, err := getFileSize(filePath)
|
||||||
|
if err != nil {
|
||||||
|
logger.LogError("Watcher", fmt.Sprintf("Error getting file size: %v", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
|
||||||
|
currentSize, err := getFileSize(filePath)
|
||||||
|
if err != nil {
|
||||||
|
logger.LogError("Watcher", fmt.Sprintf("Error getting file size: %v", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if initialSize == currentSize {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
file, err := os.Open(filePath)
|
||||||
|
if err != nil {
|
||||||
|
logger.LogError("Watcher", fmt.Sprintf("Error opening file: %v", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
originalFilename := filepath.Base(filePath)
|
||||||
|
tempFile, err := os.CreateTemp(uploadDir, originalFilename)
|
||||||
|
if err != nil {
|
||||||
|
logger.LogError("Watcher", fmt.Sprintf("Error creating temporary file: %v", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := io.Copy(tempFile, file); err != nil {
|
||||||
|
_ = tempFile.Close()
|
||||||
|
_ = os.Remove(tempFile.Name())
|
||||||
|
logger.LogError("Watcher", fmt.Sprintf("Error copying file: %v", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err := tempFile.Close(); err != nil {
|
||||||
|
_ = os.Remove(tempFile.Name())
|
||||||
|
logger.LogError("Watcher", fmt.Sprintf("Error closing temp file: %v", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := os.Remove(filePath); err != nil {
|
||||||
|
logger.LogError("Watcher", fmt.Sprintf("Error deleting original file; keeping dedupe entry to avoid reprocessing: %v", err))
|
||||||
|
_ = os.Remove(tempFile.Name())
|
||||||
|
releaseDedupe = false
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
items, err := parseInputFile(tempFile.Name())
|
||||||
|
if err != nil {
|
||||||
|
logger.LogError("Watcher", fmt.Sprintf("Error parsing input file: %v", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
go func(targetFilename string, targetItems []Item) {
|
||||||
|
if err := processItems(targetFilename, targetItems); err != nil {
|
||||||
|
logger.LogError("Watcher", fmt.Sprintf("Error processing watched file %s: %v", targetFilename, err))
|
||||||
|
}
|
||||||
|
}(filepath.Base(tempFile.Name()), items)
|
||||||
|
}
|
||||||
|
|
||||||
|
func getFileSize(filePath string) (int64, error) {
|
||||||
|
fileInfo, err := os.Stat(filePath)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return fileInfo.Size(), nil
|
||||||
|
}
|
||||||
60
subtitles.go
60
subtitles.go
@@ -1,60 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"net/http"
|
|
||||||
"os"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/asticode/go-astisub"
|
|
||||||
)
|
|
||||||
|
|
||||||
func downloadAndConvertSubtitles(subtitlesURLs string) ([]string, error) {
|
|
||||||
var subtitlePaths []string
|
|
||||||
urls := strings.Split(subtitlesURLs, ",")
|
|
||||||
|
|
||||||
for _, url := range urls {
|
|
||||||
vttPath, err := downloadSubtitle(url)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("error downloading subtitle: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
srtPath, err := convertVTTtoSRT(vttPath)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("error converting subtitle: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
subtitlePaths = append(subtitlePaths, srtPath)
|
|
||||||
}
|
|
||||||
|
|
||||||
return subtitlePaths, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func downloadSubtitle(url string) (string, error) {
|
|
||||||
resp, err := http.Get(url)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
|
|
||||||
tempFile, err := os.CreateTemp("", "subtitle_*.vtt")
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
defer tempFile.Close()
|
|
||||||
|
|
||||||
_, err = io.Copy(tempFile, resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
return tempFile.Name(), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func convertVTTtoSRT(vttPath string) (string, error) {
|
|
||||||
srtPath := strings.TrimSuffix(vttPath, ".vtt") + ".srt"
|
|
||||||
s1, _ := astisub.OpenFile(vttPath)
|
|
||||||
s1.Write(srtPath)
|
|
||||||
return srtPath, nil
|
|
||||||
}
|
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
<!DOCTYPE html>
|
|
||||||
<html>
|
|
||||||
<body>
|
|
||||||
<h1>Simple Downloader</h1>
|
|
||||||
<form action="/upload" method="post" enctype="multipart/form-data">
|
|
||||||
<input type="file" name="file" accept=".drmd">
|
|
||||||
<input type="submit" value="Upload and Process">
|
|
||||||
</form>
|
|
||||||
<h2>Currently Running Jobs</h2>
|
|
||||||
<ul>
|
|
||||||
{{range $filename, $info := .Jobs}}
|
|
||||||
<li>
|
|
||||||
<a href="/progress?filename={{$filename}}">{{$filename}}</a>:
|
|
||||||
{{printf "%.2f%%" $info.Percentage}}
|
|
||||||
(Current file: {{$info.CurrentFile}})
|
|
||||||
</li>
|
|
||||||
{{else}}
|
|
||||||
<li>No active jobs</li>
|
|
||||||
{{end}}
|
|
||||||
</ul>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
@@ -1,27 +0,0 @@
|
|||||||
<!DOCTYPE html>
|
|
||||||
<html>
|
|
||||||
<body>
|
|
||||||
<h1>Processing {{.Filename}}</h1>
|
|
||||||
<div id="progress">0%</div>
|
|
||||||
<div id="currentFile"></div>
|
|
||||||
<script>
|
|
||||||
function updateProgress() {
|
|
||||||
fetch('/progress?filename={{.Filename}}', {
|
|
||||||
headers: {
|
|
||||||
'Accept': 'application/json'
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.then(response => response.json())
|
|
||||||
.then(data => {
|
|
||||||
const progress = Math.round(data.Percentage);
|
|
||||||
document.getElementById('progress').innerText = progress + '%';
|
|
||||||
document.getElementById('currentFile').innerText = 'Current file: ' + (data.CurrentFile || 'None');
|
|
||||||
if (progress < 100) {
|
|
||||||
setTimeout(updateProgress, 1000);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
updateProgress();
|
|
||||||
</script>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
120
utils.go
120
utils.go
@@ -1,120 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"net/url"
|
|
||||||
"regexp"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/beevik/etree"
|
|
||||||
)
|
|
||||||
|
|
||||||
func sanitizeFilename(filename string) string {
|
|
||||||
filename = regexp.MustCompile(`[<>:"/\\|?*]`).ReplaceAllString(filename, "_")
|
|
||||||
|
|
||||||
filename = strings.Trim(filename, ".")
|
|
||||||
|
|
||||||
return filename
|
|
||||||
}
|
|
||||||
|
|
||||||
func isValidURL(toTest string) bool {
|
|
||||||
_, err := url.ParseRequestURI(toTest)
|
|
||||||
return err == nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func fixGoPlay(mpdContent string) (string, error) {
|
|
||||||
doc := etree.NewDocument()
|
|
||||||
if err := doc.ReadFromString(mpdContent); err != nil {
|
|
||||||
return "", fmt.Errorf("error parsing MPD content: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
root := doc.Root()
|
|
||||||
|
|
||||||
// Remove ad periods
|
|
||||||
for _, period := range root.SelectElements("Period") {
|
|
||||||
if strings.Contains(period.SelectAttrValue("id", ""), "-ad-") {
|
|
||||||
root.RemoveChild(period)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Find highest bandwidth for video
|
|
||||||
highestBandwidth := 0
|
|
||||||
for _, adaptationSet := range root.FindElements("//AdaptationSet") {
|
|
||||||
if strings.Contains(adaptationSet.SelectAttrValue("mimeType", ""), "video") {
|
|
||||||
for _, representation := range adaptationSet.SelectElements("Representation") {
|
|
||||||
bandwidth, _ := strconv.Atoi(representation.SelectAttrValue("bandwidth", "0"))
|
|
||||||
if bandwidth > highestBandwidth {
|
|
||||||
highestBandwidth = bandwidth
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remove lower bitrate representations
|
|
||||||
for _, adaptationSet := range root.FindElements("//AdaptationSet") {
|
|
||||||
if strings.Contains(adaptationSet.SelectAttrValue("mimeType", ""), "video") {
|
|
||||||
for _, representation := range adaptationSet.SelectElements("Representation") {
|
|
||||||
bandwidth, _ := strconv.Atoi(representation.SelectAttrValue("bandwidth", "0"))
|
|
||||||
if bandwidth != highestBandwidth {
|
|
||||||
adaptationSet.RemoveChild(representation)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Combine periods
|
|
||||||
periods := root.SelectElements("Period")
|
|
||||||
if len(periods) > 1 {
|
|
||||||
firstPeriod := periods[0]
|
|
||||||
var newVideoTimeline, newAudioTimeline *etree.Element
|
|
||||||
|
|
||||||
// Find or create SegmentTimeline elements
|
|
||||||
for _, adaptationSet := range firstPeriod.SelectElements("AdaptationSet") {
|
|
||||||
mimeType := adaptationSet.SelectAttrValue("mimeType", "")
|
|
||||||
if strings.Contains(mimeType, "video") && newVideoTimeline == nil {
|
|
||||||
newVideoTimeline = findOrCreateSegmentTimeline(adaptationSet)
|
|
||||||
} else if strings.Contains(mimeType, "audio") && newAudioTimeline == nil {
|
|
||||||
newAudioTimeline = findOrCreateSegmentTimeline(adaptationSet)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, period := range periods[1:] {
|
|
||||||
for _, adaptationSet := range period.SelectElements("AdaptationSet") {
|
|
||||||
mimeType := adaptationSet.SelectAttrValue("mimeType", "")
|
|
||||||
var timeline *etree.Element
|
|
||||||
if strings.Contains(mimeType, "video") {
|
|
||||||
timeline = newVideoTimeline
|
|
||||||
} else if strings.Contains(mimeType, "audio") {
|
|
||||||
timeline = newAudioTimeline
|
|
||||||
}
|
|
||||||
|
|
||||||
if timeline != nil {
|
|
||||||
segmentTimeline := findOrCreateSegmentTimeline(adaptationSet)
|
|
||||||
for _, s := range segmentTimeline.SelectElements("S") {
|
|
||||||
timeline.AddChild(s.Copy())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
root.RemoveChild(period)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return doc.WriteToString()
|
|
||||||
}
|
|
||||||
|
|
||||||
func findOrCreateSegmentTimeline(adaptationSet *etree.Element) *etree.Element {
|
|
||||||
for _, representation := range adaptationSet.SelectElements("Representation") {
|
|
||||||
for _, segmentTemplate := range representation.SelectElements("SegmentTemplate") {
|
|
||||||
timeline := segmentTemplate.SelectElement("SegmentTimeline")
|
|
||||||
if timeline != nil {
|
|
||||||
return timeline
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// If no SegmentTimeline found, create one
|
|
||||||
representation := adaptationSet.CreateElement("Representation")
|
|
||||||
segmentTemplate := representation.CreateElement("SegmentTemplate")
|
|
||||||
return segmentTemplate.CreateElement("SegmentTimeline")
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user