Implement a live console of the downloader on the progress page #6
@@ -1,6 +1,7 @@
 | 
				
			|||||||
BaseDir = "/mnt/media"
 | 
					BaseDir = "/mnt/media"
 | 
				
			||||||
Format = "mkv" 
 | 
					Format = "mkv" 
 | 
				
			||||||
TempBaseDir = "/tmp/nre"
 | 
					TempBaseDir = "/tmp/nre"
 | 
				
			||||||
 | 
					EnableConsole = true
 | 
				
			||||||
 | 
					
 | 
				
			||||||
[N_m3u8DLRE]
 | 
					[N_m3u8DLRE]
 | 
				
			||||||
Path = "nre"
 | 
					Path = "nre"
 | 
				
			||||||
@@ -15,6 +15,7 @@ type Config struct {
 | 
				
			|||||||
	N_m3u8DLRE  struct {
 | 
						N_m3u8DLRE  struct {
 | 
				
			||||||
		Path string
 | 
							Path string
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						EnableConsole bool
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var config Config
 | 
					var config Config
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -132,8 +132,9 @@ func downloadFile(drmdFilename string, item Item, jobInfo *JobInfo) error {
 | 
				
			|||||||
		for {
 | 
							for {
 | 
				
			||||||
			if outputBuffer.Len() > 0 {
 | 
								if outputBuffer.Len() > 0 {
 | 
				
			||||||
				message := outputBuffer.Bytes()
 | 
									message := outputBuffer.Bytes()
 | 
				
			||||||
				logger.LogInfo("Download File", fmt.Sprintf("Broadcasting message: %s", message))
 | 
									if config.EnableConsole {
 | 
				
			||||||
					broadcast(drmdFilename, message)
 | 
										broadcast(drmdFilename, message)
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
				outputBuffer.Reset()
 | 
									outputBuffer.Reset()
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			time.Sleep(1 * time.Second)
 | 
								time.Sleep(1 * time.Second)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -386,8 +386,13 @@ var clients = make(map[string]map[*websocket.Conn]bool)
 | 
				
			|||||||
var mu sync.Mutex
 | 
					var mu sync.Mutex
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func handleWebSocket(w http.ResponseWriter, r *http.Request) {
 | 
					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")
 | 
						filename := r.URL.Query().Get("filename")
 | 
				
			||||||
	fmt.Println("Filename:", filename)
 | 
					 | 
				
			||||||
	if filename == "" {
 | 
						if filename == "" {
 | 
				
			||||||
		http.Error(w, "Filename is required", http.StatusBadRequest)
 | 
							http.Error(w, "Filename is required", http.StatusBadRequest)
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
@@ -423,11 +428,13 @@ func handleWebSocket(w http.ResponseWriter, r *http.Request) {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func broadcast(filename string, message []byte) {
 | 
					func broadcast(filename string, message []byte) {
 | 
				
			||||||
 | 
						if !config.EnableConsole {
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	mu.Lock()
 | 
						mu.Lock()
 | 
				
			||||||
	defer mu.Unlock()
 | 
						defer mu.Unlock()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	logger.LogInfo("Broadcast", fmt.Sprintf("Broadcasting message for filename: %s", filename))
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	for client := range clients[filename] {
 | 
						for client := range clients[filename] {
 | 
				
			||||||
		if err := client.WriteMessage(websocket.TextMessage, message); err != nil {
 | 
							if err := client.WriteMessage(websocket.TextMessage, message); err != nil {
 | 
				
			||||||
			client.Close()
 | 
								client.Close()
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user