Implement a live console of the downloader on the progress page #6
@@ -20,7 +20,7 @@ func removeBOM(input []byte) []byte {
 | 
			
		||||
	return input
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func downloadFile(item Item, jobInfo *JobInfo) error {
 | 
			
		||||
func downloadFile(drmdFilename string, 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,7 +131,9 @@ func downloadFile(item Item, jobInfo *JobInfo) error {
 | 
			
		||||
	go func() {
 | 
			
		||||
		for {
 | 
			
		||||
			if outputBuffer.Len() > 0 {
 | 
			
		||||
				broadcast(outputBuffer.Bytes())
 | 
			
		||||
				message := outputBuffer.Bytes()
 | 
			
		||||
				logger.LogInfo("Download File", fmt.Sprintf("Broadcasting message: %s", message))
 | 
			
		||||
				broadcast(drmdFilename, message)
 | 
			
		||||
				outputBuffer.Reset()
 | 
			
		||||
			}
 | 
			
		||||
			time.Sleep(1 * time.Second)
 | 
			
		||||
 
 | 
			
		||||
@@ -382,19 +382,31 @@ func updateProgress(filename string, value float64, currentFile string) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
var upgrader = websocket.Upgrader{}
 | 
			
		||||
var clients = make(map[*websocket.Conn]bool)
 | 
			
		||||
var clients = make(map[string]map[*websocket.Conn]bool)
 | 
			
		||||
var mu sync.Mutex
 | 
			
		||||
 | 
			
		||||
func handleWebSocket(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
	filename := r.URL.Query().Get("filename")
 | 
			
		||||
	fmt.Println("Filename:", filename)
 | 
			
		||||
	if filename == "" {
 | 
			
		||||
		http.Error(w, "Filename is required", http.StatusBadRequest)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	conn, err := upgrader.Upgrade(w, r, nil)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		fmt.Println("Error while upgrading connection:", err)
 | 
			
		||||
		logger.LogError("WebSocket", fmt.Sprintf("Error while upgrading connection: %v", err))
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	defer conn.Close()
 | 
			
		||||
 | 
			
		||||
	logger.LogInfo("WebSocket", fmt.Sprintf("WebSocket connection established for filename: %s", filename))
 | 
			
		||||
 | 
			
		||||
	mu.Lock()
 | 
			
		||||
	clients[conn] = true
 | 
			
		||||
	if clients[filename] == nil {
 | 
			
		||||
		clients[filename] = make(map[*websocket.Conn]bool)
 | 
			
		||||
	}
 | 
			
		||||
	clients[filename][conn] = true
 | 
			
		||||
	mu.Unlock()
 | 
			
		||||
 | 
			
		||||
	for {
 | 
			
		||||
@@ -404,18 +416,23 @@ func handleWebSocket(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	mu.Lock()
 | 
			
		||||
	delete(clients, conn)
 | 
			
		||||
	delete(clients[filename], conn)
 | 
			
		||||
	mu.Unlock()
 | 
			
		||||
 | 
			
		||||
	logger.LogInfo("WebSocket", fmt.Sprintf("WebSocket connection closed for filename: %s", filename))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func broadcast(message []byte) {
 | 
			
		||||
func broadcast(filename string, message []byte) {
 | 
			
		||||
	mu.Lock()
 | 
			
		||||
	defer mu.Unlock()
 | 
			
		||||
 | 
			
		||||
	for client := range clients {
 | 
			
		||||
	logger.LogInfo("Broadcast", fmt.Sprintf("Broadcasting message for filename: %s", filename))
 | 
			
		||||
 | 
			
		||||
	for client := range clients[filename] {
 | 
			
		||||
		if err := client.WriteMessage(websocket.TextMessage, message); err != nil {
 | 
			
		||||
			client.Close()
 | 
			
		||||
			delete(clients, client)
 | 
			
		||||
			delete(clients[filename], client)
 | 
			
		||||
			logger.LogError("Broadcast", fmt.Sprintf("Error writing message to client: %v", err))
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -145,9 +145,10 @@
 | 
			
		||||
    <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'
 | 
			
		||||
                }
 | 
			
		||||
@@ -179,7 +180,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.');
 | 
			
		||||
@@ -190,7 +191,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.');
 | 
			
		||||
@@ -203,7 +204,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.');
 | 
			
		||||
@@ -217,7 +218,7 @@
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        const consoleDiv = document.getElementById('console');
 | 
			
		||||
        const ws = new WebSocket(`ws://${window.location.host}/ws`);
 | 
			
		||||
        const ws = new WebSocket(`ws://${window.location.host}/ws?filename=${filename}`);
 | 
			
		||||
 | 
			
		||||
        ws.onmessage = function(event) {
 | 
			
		||||
            consoleDiv.textContent += event.data; 
 | 
			
		||||
 
 | 
			
		||||
@@ -259,21 +259,25 @@ 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(items[i], jobInfo)
 | 
			
		||||
			err := downloadFile(filename, items[i], jobInfo)
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				if err.Error() == "download paused" {
 | 
			
		||||
					logger.LogJobState(filename, "paused")
 | 
			
		||||
					removeCompletedEpisodes(filename, items[:i])
 | 
			
		||||
					i--
 | 
			
		||||
					continue
 | 
			
		||||
@@ -282,6 +286,7 @@ func processItems(filename string, items []Item) error {
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	updateProgress(filename, 100, "")
 | 
			
		||||
	logger.LogJobState(filename, "completed successfully")
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user