2024-09-06 13:28:12 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
2024-09-14 02:02:35 +02:00
|
|
|
"net/url"
|
2024-09-06 13:28:12 +02:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2024-09-14 02:02:35 +02:00
|
|
|
"strings"
|
2024-09-06 13:28:12 +02:00
|
|
|
)
|
|
|
|
|
2024-09-14 00:53:55 +02:00
|
|
|
type ProgressInfo struct {
|
|
|
|
Percentage float64
|
|
|
|
CurrentFile string
|
|
|
|
Paused bool
|
|
|
|
}
|
2024-09-06 13:28:12 +02:00
|
|
|
|
2024-09-14 00:53:55 +02:00
|
|
|
func handleRoot(w http.ResponseWriter, r *http.Request) {
|
2024-09-06 13:28:12 +02:00
|
|
|
progressMutex.Lock()
|
2024-09-14 00:53:55 +02:00
|
|
|
defer progressMutex.Unlock()
|
|
|
|
|
|
|
|
jobsInfo := make(map[string]struct {
|
|
|
|
Percentage float64
|
|
|
|
CurrentFile string
|
|
|
|
Paused bool
|
|
|
|
})
|
|
|
|
|
|
|
|
for filename, info := range progress {
|
|
|
|
jobsInfo[filename] = struct {
|
|
|
|
Percentage float64
|
|
|
|
CurrentFile string
|
|
|
|
Paused bool
|
|
|
|
}{
|
|
|
|
Percentage: info.Percentage,
|
|
|
|
CurrentFile: info.CurrentFile,
|
|
|
|
Paused: info.Paused,
|
|
|
|
}
|
2024-09-06 13:28:12 +02:00
|
|
|
}
|
|
|
|
|
2024-09-14 00:53:55 +02:00
|
|
|
err := templates.ExecuteTemplate(w, "index", struct {
|
|
|
|
Jobs map[string]struct {
|
|
|
|
Percentage float64
|
|
|
|
CurrentFile string
|
|
|
|
Paused bool
|
|
|
|
}
|
|
|
|
}{jobsInfo})
|
2024-09-06 13:28:12 +02:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleUpload(w http.ResponseWriter, r *http.Request) {
|
2024-09-14 02:02:35 +02:00
|
|
|
err := r.ParseMultipartForm(32 << 20)
|
2024-09-06 13:28:12 +02:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-09-14 02:02:35 +02:00
|
|
|
files := r.MultipartForm.File["files"]
|
|
|
|
if len(files) == 0 {
|
|
|
|
http.Error(w, "No files uploaded", http.StatusBadRequest)
|
2024-09-06 13:28:12 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-09-14 02:02:35 +02:00
|
|
|
uploadedFiles := []string{}
|
|
|
|
|
|
|
|
for _, fileHeader := range files {
|
|
|
|
file, err := fileHeader.Open()
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
2024-09-14 03:39:15 +02:00
|
|
|
tempFile, err := os.CreateTemp(uploadDir, fileHeader.Filename)
|
2024-09-14 02:02:35 +02:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2024-09-14 03:39:15 +02:00
|
|
|
defer tempFile.Close()
|
2024-09-14 02:02:35 +02:00
|
|
|
|
2024-09-14 03:39:15 +02:00
|
|
|
_, err = io.Copy(tempFile, file)
|
2024-09-14 02:02:35 +02:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-09-14 03:39:15 +02:00
|
|
|
uploadedFiles = append(uploadedFiles, filepath.Base(tempFile.Name()))
|
2024-09-14 02:02:35 +02:00
|
|
|
|
2024-09-14 03:39:15 +02:00
|
|
|
_, err = parseInputFile(tempFile.Name())
|
2024-09-14 02:02:35 +02:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2024-09-06 13:28:12 +02:00
|
|
|
}
|
|
|
|
|
2024-09-14 02:02:35 +02:00
|
|
|
validFiles := []string{}
|
|
|
|
for _, file := range uploadedFiles {
|
|
|
|
if file != "" {
|
|
|
|
validFiles = append(validFiles, file)
|
|
|
|
}
|
|
|
|
}
|
2024-09-06 19:05:57 +02:00
|
|
|
|
2024-09-14 02:02:35 +02:00
|
|
|
if len(validFiles) == 0 {
|
|
|
|
http.Error(w, "No valid files were uploaded", http.StatusBadRequest)
|
2024-09-13 22:00:44 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-09-14 02:02:35 +02:00
|
|
|
http.Redirect(w, r, "/select?files="+url.QueryEscape(strings.Join(validFiles, ",")), http.StatusSeeOther)
|
2024-09-13 22:00:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func handleSelect(w http.ResponseWriter, r *http.Request) {
|
2024-09-14 02:02:35 +02:00
|
|
|
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 := filepath.Join(uploadDir, filename)
|
|
|
|
|
|
|
|
if _, err := os.Stat(fullPath); os.IsNotExist(err) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
items, err := parseInputFile(fullPath)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-09-15 04:29:48 +02:00
|
|
|
sortItems(items)
|
|
|
|
|
2024-09-14 02:02:35 +02:00
|
|
|
groupedItems := groupItemsBySeason(items)
|
|
|
|
allItems[filename] = groupedItems
|
2024-09-13 22:00:44 +02:00
|
|
|
}
|
|
|
|
|
2024-09-14 02:02:35 +02:00
|
|
|
if len(allItems) == 0 {
|
|
|
|
http.Error(w, "No valid files were processed", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2024-09-13 22:00:44 +02:00
|
|
|
|
2024-09-14 02:02:35 +02:00
|
|
|
err := templates.ExecuteTemplate(w, "select", struct {
|
|
|
|
Filenames string
|
|
|
|
AllItems map[string]map[string][]Item
|
2024-09-13 22:00:44 +02:00
|
|
|
}{
|
2024-09-14 02:02:35 +02:00
|
|
|
Filenames: filesParam,
|
|
|
|
AllItems: allItems,
|
2024-09-13 22:00:44 +02:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleProcess(w http.ResponseWriter, r *http.Request) {
|
2024-09-14 02:02:35 +02:00
|
|
|
if err := r.ParseForm(); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2024-09-13 22:00:44 +02:00
|
|
|
|
2024-09-14 02:02:35 +02:00
|
|
|
selectedItems := r.Form["items"]
|
|
|
|
if len(selectedItems) == 0 {
|
|
|
|
http.Error(w, "No items selected", http.StatusBadRequest)
|
2024-09-13 22:00:44 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-09-14 02:02:35 +02:00
|
|
|
itemsByFile := make(map[string][]string)
|
|
|
|
for _, item := range selectedItems {
|
|
|
|
parts := strings.SplitN(item, ":", 2)
|
|
|
|
if len(parts) != 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
filename, itemName := parts[0], parts[1]
|
|
|
|
itemsByFile[filename] = append(itemsByFile[filename], itemName)
|
|
|
|
}
|
2024-09-13 22:00:44 +02:00
|
|
|
|
2024-09-14 02:02:35 +02:00
|
|
|
for filename, items := range itemsByFile {
|
|
|
|
fullPath := filepath.Join(uploadDir, filename)
|
|
|
|
allItems, err := parseInputFile(fullPath)
|
2024-09-06 13:28:12 +02:00
|
|
|
if err != nil {
|
2024-09-14 02:02:35 +02:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
2024-09-06 13:28:12 +02:00
|
|
|
}
|
|
|
|
|
2024-09-14 02:02:35 +02:00
|
|
|
selectedItems := filterSelectedItems(allItems, items)
|
2024-09-15 04:29:48 +02:00
|
|
|
sortItems(selectedItems)
|
2024-09-14 02:02:35 +02:00
|
|
|
go processItems(filename, selectedItems)
|
|
|
|
}
|
2024-09-06 13:28:12 +02:00
|
|
|
|
2024-09-14 02:02:35 +02:00
|
|
|
http.Redirect(w, r, "/", http.StatusSeeOther)
|
2024-09-06 13:28:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func handleProgress(w http.ResponseWriter, r *http.Request) {
|
|
|
|
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 }{filename})
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|
2024-09-13 22:29:03 +02:00
|
|
|
|
|
|
|
func handlePause(w http.ResponseWriter, r *http.Request) {
|
|
|
|
filename := r.URL.Query().Get("filename")
|
|
|
|
if filename == "" {
|
|
|
|
http.Error(w, "Filename is required", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jobsMutex.Lock()
|
|
|
|
jobInfo, exists := jobs[filename]
|
|
|
|
jobsMutex.Unlock()
|
|
|
|
|
|
|
|
if !exists {
|
|
|
|
http.Error(w, "Job not found", http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jobInfo.Paused = true
|
|
|
|
if jobInfo.Cmd != nil && jobInfo.Cmd.Process != nil {
|
|
|
|
jobInfo.Cmd.Process.Kill()
|
|
|
|
}
|
|
|
|
|
2024-09-14 00:53:55 +02:00
|
|
|
progressMutex.Lock()
|
|
|
|
if progressInfo, ok := progress[filename]; ok {
|
|
|
|
progressInfo.Paused = true
|
|
|
|
}
|
|
|
|
progressMutex.Unlock()
|
|
|
|
|
2024-09-13 22:29:03 +02:00
|
|
|
fmt.Fprintf(w, "Pause signal sent for %s", filename)
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleResume(w http.ResponseWriter, r *http.Request) {
|
|
|
|
filename := r.URL.Query().Get("filename")
|
|
|
|
if filename == "" {
|
|
|
|
http.Error(w, "Filename is required", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jobsMutex.Lock()
|
|
|
|
jobInfo, exists := jobs[filename]
|
|
|
|
jobsMutex.Unlock()
|
|
|
|
|
|
|
|
if !exists {
|
|
|
|
http.Error(w, "Job not found", http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jobInfo.Paused = false
|
|
|
|
jobInfo.ResumeChan <- struct{}{}
|
|
|
|
|
2024-09-14 00:53:55 +02:00
|
|
|
progressMutex.Lock()
|
|
|
|
if progressInfo, ok := progress[filename]; ok {
|
|
|
|
progressInfo.Paused = false
|
|
|
|
}
|
|
|
|
progressMutex.Unlock()
|
|
|
|
|
2024-09-13 22:29:03 +02:00
|
|
|
fmt.Fprintf(w, "Resume signal sent for %s", filename)
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleAbort(w http.ResponseWriter, r *http.Request) {
|
|
|
|
filename := r.URL.Query().Get("filename")
|
|
|
|
if filename == "" {
|
|
|
|
http.Error(w, "Filename is required", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jobsMutex.Lock()
|
|
|
|
jobInfo, exists := jobs[filename]
|
|
|
|
jobsMutex.Unlock()
|
|
|
|
|
|
|
|
if !exists {
|
|
|
|
http.Error(w, "Job not found", http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
close(jobInfo.AbortChan)
|
|
|
|
if jobInfo.Cmd != nil && jobInfo.Cmd.Process != nil {
|
|
|
|
jobInfo.Cmd.Process.Kill()
|
|
|
|
}
|
|
|
|
|
2024-09-13 22:43:22 +02:00
|
|
|
if jobInfo.TempDir != "" {
|
|
|
|
os.RemoveAll(jobInfo.TempDir)
|
|
|
|
}
|
|
|
|
|
2024-09-13 22:29:03 +02:00
|
|
|
fmt.Fprintf(w, "Abort signal sent for %s", filename)
|
|
|
|
}
|
2024-09-13 22:57:55 +02:00
|
|
|
|
|
|
|
func handleClearCompleted(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method != http.MethodPost {
|
|
|
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
|
|
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 {
|
|
|
|
delete(progress, filename)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-09-14 00:53:55 +02:00
|
|
|
|
|
|
|
func updateProgress(filename string, value float64, currentFile string) {
|
|
|
|
progressMutex.Lock()
|
|
|
|
defer progressMutex.Unlock()
|
|
|
|
|
|
|
|
jobsMutex.Lock()
|
|
|
|
jobInfo, exists := jobs[filename]
|
|
|
|
jobsMutex.Unlock()
|
|
|
|
|
|
|
|
paused := false
|
|
|
|
if exists {
|
|
|
|
paused = jobInfo.Paused
|
|
|
|
}
|
|
|
|
|
|
|
|
if existingProgress, ok := progress[filename]; ok {
|
|
|
|
existingProgress.Percentage = value
|
|
|
|
existingProgress.CurrentFile = currentFile
|
|
|
|
existingProgress.Paused = paused
|
|
|
|
} else {
|
|
|
|
progress[filename] = &ProgressInfo{
|
|
|
|
Percentage: value,
|
|
|
|
CurrentFile: currentFile,
|
|
|
|
Paused: paused,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|