speedLimiter #10

Merged
Joren merged 9 commits from speedLimiter into main 2024-12-30 16:47:46 +01:00
5 changed files with 124 additions and 4 deletions
Showing only changes of commit 83cd0b722b - Show all commits

View File

@ -1,5 +1,5 @@
[General]
BaseDir = "/mnt/media"
BaseDir = "/home/joren/dev/DRMDTool/media"
Format = "mkv"
TempBaseDir = "/tmp/nre"
EnableConsole = true
@ -8,7 +8,7 @@ EnableConsole = true
Path = "/mnt/watched"
PollingInterval = 10
UsePolling = false
UseInotify = true
UseInotify = false
[N_m3u8DLRE]
Path = "nre"
Path = "nre"

View File

@ -195,6 +195,10 @@ func getDownloadCommand(item Item, mpdPath string, tempDir string) string {
command += fmt.Sprintf(" --tmp-dir \"%s\"", tempDir)
if globalSpeedLimit != "" {
command += fmt.Sprintf(" -R %s", globalSpeedLimit)
}
fmt.Println(command)
return command

View File

@ -48,7 +48,11 @@ func handleRoot(w http.ResponseWriter, r *http.Request) {
CurrentFile string
Paused bool
}
}{jobsInfo})
GlobalSpeedLimit string
}{
Jobs: jobsInfo,
GlobalSpeedLimit: globalSpeedLimit,
})
if err != nil {
logger.LogError("Handle Root", fmt.Sprintf("Error executing template: %v", err))
http.Error(w, err.Error(), http.StatusInternalServerError)
@ -443,3 +447,32 @@ func broadcast(filename string, message []byte) {
}
}
}
func handleSetSpeedLimit(w http.ResponseWriter, r *http.Request) {
logger.LogInfo("Set Speed Limit", "Received request to set speed limit")
if r.Method != http.MethodPost {
logger.LogError("Set Speed Limit", "Invalid method")
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
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
}
if requestData.SpeedLimit == "unlimited" {
globalSpeedLimit = ""
} else {
globalSpeedLimit = requestData.SpeedLimit
}
logger.LogInfo("Set Speed Limit", fmt.Sprintf("Global speed limit set to: %s", globalSpeedLimit))
w.WriteHeader(http.StatusOK)
}

View File

@ -44,6 +44,8 @@ var templates *template.Template
//go:embed templates
var templateFS embed.FS
var globalSpeedLimit string
func init() {
if err := os.MkdirAll(uploadDir, 0755); err != nil {
fmt.Printf("Error creating upload directory: %v\n", err)
@ -70,6 +72,8 @@ func main() {
}
processItems(*inputFile, items)
}
http.HandleFunc("/set-speed-limit", handleSetSpeedLimit)
}
func startWebServer() {
@ -83,6 +87,7 @@ func startWebServer() {
http.HandleFunc("/resume", handleResume)
http.HandleFunc("/clear-completed", handleClearCompleted)
http.HandleFunc("/ws", handleWebSocket)
http.HandleFunc("/set-speed-limit", handleSetSpeedLimit)
logger.LogInfo("Main", "Starting web server on http://0.0.0.0:8080")
http.ListenAndServe(":8080", nil)

View File

@ -77,6 +77,11 @@
.paused {
color: #ffa500;
}
.speed-limit {
font-size: 1em;
color: #a0a0a0;
margin-top: 10px;
}
@media (max-width: 600px) {
body {
padding: 10px;
@ -107,6 +112,39 @@
#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;
}
</style>
</head>
<body>
@ -135,6 +173,24 @@
{{end}}
</ul>
<button id="clear-completed" onclick="clearCompleted()">Clear Completed Jobs</button>
<div class="settings-section">
<h2>Settings</h2>
<form id="speed-limit-form" class="speed-limit-form" onsubmit="updateSpeedLimit(event)">
<div class="form-group">
<label for="speedLimitValue">Global 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 type="button" onclick="updateSpeedLimit(event)">Set Speed Limit</button>
</div>
<p class="speed-limit">Current Speed Limit: <span id="currentSpeedLimit">{{if .GlobalSpeedLimit}}{{.GlobalSpeedLimit}}{{else}}unlimited{{end}}</span></p>
</form>
</div>
<script>
function clearCompleted() {
fetch('/clear-completed', { method: 'POST' })
@ -147,6 +203,28 @@
}
});
}
function updateSpeedLimit(event) {
event.preventDefault();
const speedLimitValue = document.getElementById('speedLimitValue').value;
const speedLimitUnit = document.getElementById('speedLimitUnit').value;
const speedLimit = speedLimitValue === "0" ? "unlimited" : speedLimitValue + speedLimitUnit;
fetch('/set-speed-limit', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ speedLimit }),
}).then(response => {
if (response.ok) {
alert('Speed limit updated successfully');
document.getElementById('currentSpeedLimit').textContent = speedLimit;
} else {
alert('Failed to update speed limit');
}
});
}
</script>
</body>
</html>