Compare commits

..

No commits in common. "7445627f7e582ffbc82a93a85aeab5d7fcedcd6d" and "ace79838fe2bfcd9a35535ace855b903890dcb29" have entirely different histories.

6 changed files with 16 additions and 51 deletions

View File

@ -1,7 +1,6 @@
BaseDir = "/mnt/media"
Format = "mkv"
TempBaseDir = "/tmp/nre"
EnableConsole = true
[N_m3u8DLRE]
Path = "nre"
Path = "nre"

View File

@ -15,7 +15,6 @@ type Config struct {
N_m3u8DLRE struct {
Path string
}
EnableConsole bool
}
var config Config

View File

@ -20,7 +20,7 @@ func removeBOM(input []byte) []byte {
return input
}
func downloadFile(drmdFilename string, item Item, jobInfo *JobInfo) error {
func downloadFile(item Item, jobInfo *JobInfo) error {
logger.LogInfo("Download File", fmt.Sprintf("Starting download for: %s", item.Filename))
tempDir := filepath.Join(config.TempBaseDir, sanitizeFilename(item.Filename))
@ -131,10 +131,7 @@ func downloadFile(drmdFilename string, item Item, jobInfo *JobInfo) error {
go func() {
for {
if outputBuffer.Len() > 0 {
message := outputBuffer.Bytes()
if config.EnableConsole {
broadcast(drmdFilename, message)
}
broadcast(outputBuffer.Bytes())
outputBuffer.Reset()
}
time.Sleep(1 * time.Second)

View File

@ -382,36 +382,19 @@ func updateProgress(filename string, value float64, currentFile string) {
}
var upgrader = websocket.Upgrader{}
var clients = make(map[string]map[*websocket.Conn]bool)
var clients = make(map[*websocket.Conn]bool)
var mu sync.Mutex
func handleWebSocket(w http.ResponseWriter, r *http.Request) {
fmt.Println(config.EnableConsole)
if !config.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))
fmt.Println("Error while upgrading connection:", err)
return
}
defer conn.Close()
logger.LogInfo("WebSocket", fmt.Sprintf("WebSocket connection established for filename: %s", filename))
mu.Lock()
if clients[filename] == nil {
clients[filename] = make(map[*websocket.Conn]bool)
}
clients[filename][conn] = true
clients[conn] = true
mu.Unlock()
for {
@ -421,25 +404,18 @@ func handleWebSocket(w http.ResponseWriter, r *http.Request) {
}
mu.Lock()
delete(clients[filename], conn)
delete(clients, conn)
mu.Unlock()
logger.LogInfo("WebSocket", fmt.Sprintf("WebSocket connection closed for filename: %s", filename))
}
func broadcast(filename string, message []byte) {
if !config.EnableConsole {
return
}
func broadcast(message []byte) {
mu.Lock()
defer mu.Unlock()
for client := range clients[filename] {
for client := range clients {
if err := client.WriteMessage(websocket.TextMessage, message); err != nil {
client.Close()
delete(clients[filename], client)
logger.LogError("Broadcast", fmt.Sprintf("Error writing message to client: %v", err))
delete(clients, client)
}
}
}

View File

@ -145,10 +145,9 @@
<div style="display: none;" id="console"></div>
<script>
let isPaused = false;
const filename = "{{.Filename}}";
function updateProgress() {
fetch(`/progress?filename=${filename}`, {
fetch('/progress?filename={{.Filename}}', {
headers: {
'Accept': 'application/json'
}
@ -180,7 +179,7 @@
}
function abortDownload() {
fetch(`/abort?filename=${filename}`, { method: 'POST' })
fetch('/abort?filename={{.Filename}}', { method: 'POST' })
.then(response => {
if (response.ok) {
console.log('Abort signal sent. The download will stop soon.');
@ -191,7 +190,7 @@
}
function pauseDownload() {
fetch(`/pause?filename=${filename}`, { method: 'POST' })
fetch('/pause?filename={{.Filename}}', { method: 'POST' })
.then(response => {
if (response.ok) {
console.log('Pause signal sent. The download will pause soon.');
@ -204,7 +203,7 @@
}
function resumeDownload() {
fetch(`/resume?filename=${filename}`, { method: 'POST' })
fetch('/resume?filename={{.Filename}}', { method: 'POST' })
.then(response => {
if (response.ok) {
console.log('Resume signal sent. The download will resume soon.');
@ -218,7 +217,7 @@
}
const consoleDiv = document.getElementById('console');
const ws = new WebSocket(`ws://${window.location.host}/ws?filename=${filename}`);
const ws = new WebSocket(`ws://${window.location.host}/ws`);
ws.onmessage = function(event) {
consoleDiv.textContent += event.data;

View File

@ -259,25 +259,21 @@ func processItems(filename string, items []Item) error {
select {
case <-jobInfo.AbortChan:
updateProgress(filename, 100, "Aborted")
logger.LogJobState(filename, "aborted")
return fmt.Errorf("download aborted")
default:
if jobInfo.Paused {
select {
case <-jobInfo.ResumeChan:
jobInfo.Paused = false
logger.LogJobState(filename, "resumed")
case <-jobInfo.AbortChan:
updateProgress(filename, 100, "Aborted")
logger.LogJobState(filename, "aborted")
return fmt.Errorf("download aborted")
}
}
updateProgress(filename, float64(i)/float64(len(items))*100, items[i].Filename)
err := downloadFile(filename, items[i], jobInfo)
err := downloadFile(items[i], jobInfo)
if err != nil {
if err.Error() == "download paused" {
logger.LogJobState(filename, "paused")
removeCompletedEpisodes(filename, items[:i])
i--
continue
@ -286,7 +282,6 @@ func processItems(filename string, items []Item) error {
}
}
updateProgress(filename, 100, "")
logger.LogJobState(filename, "completed successfully")
return nil
}