Add option to upload files in webui

This commit is contained in:
2025-03-03 19:42:53 +01:00
parent a8a6292d2b
commit dcc46f679b
9 changed files with 366 additions and 31 deletions

View File

@@ -4,6 +4,8 @@ import (
"fmt"
"path/filepath"
"os"
"io"
"net/http"
"runtime"
"sync"
"sync/atomic"
@@ -21,7 +23,10 @@ func SetImportConfigToTheRemote(c *gowebdav.Client, config configloader.MDWebDav
}
// Uploads a list of files to the target folder.
func uploadFiles(c *gowebdav.Client, files []string, remoteTarget string, outputContext string) {
func uploadFiles(c *gowebdav.Client, w io.Writer, files []string, remoteTarget string, outputContext string) {
// Check if the io.Writer is a http writer
_, wImplementsHttpFlusher := interface{}(w).(http.Flusher)
total := len(files)
var counter atomic.Uint64
@@ -34,7 +39,10 @@ func uploadFiles(c *gowebdav.Client, files []string, remoteTarget string, output
semaphore := make(chan struct{}, maxConcTasks)
wg := &sync.WaitGroup{}
fmt.Printf("Will upload %v files. Processing %v tasks at a time.\n", total, maxConcTasks)
fmt.Fprintf(w, "Will upload %v files. Processing %v tasks at a time.\n", total, maxConcTasks)
if wImplementsHttpFlusher == true {
w.(http.Flusher).Flush()
}
for _, f := range(files) {
@@ -56,7 +64,10 @@ func uploadFiles(c *gowebdav.Client, files []string, remoteTarget string, output
c.WriteStream("./" + remoteTarget + "/" + basename, file, 0644)
counter.Add(1)
fmt.Printf(outputContext + ". File %v of %v. Done. (File: %v)\n", counter, total, basename)
fmt.Fprintf(w, "Uploading %d of %d - File: %s (%s)\n", counter, total, basename, outputContext)
if wImplementsHttpFlusher == true {
w.(http.Flusher).Flush()
}
<-semaphore // release
@@ -70,15 +81,15 @@ func uploadFiles(c *gowebdav.Client, files []string, remoteTarget string, output
}
// Uploads the selected metadata files.
func UploadMetadataFiles(c *gowebdav.Client, files []string) {
func UploadMetadataFiles(c *gowebdav.Client, w io.Writer, files []string) {
uploadFiles(c, files, "IMPORT_XML", "Uploading metadata files")
uploadFiles(c, w, files, "IMPORT_XML", "Uploading metadata files")
}
// Uploads the selected media files.
func UploadMediaFiles(c *gowebdav.Client, files []string) {
func UploadMediaFiles(c *gowebdav.Client, w io.Writer, files []string) {
uploadFiles(c, files, "IMPORT_IMG", "Uploading media files")
uploadFiles(c, w, files, "IMPORT_IMG", "Uploading media files")
}