@@ -93,3 +93,40 @@ func UploadMediaFiles(c *gowebdav.Client, w io.Writer, files []string) {
|
||||
uploadFiles(c, w, files, "IMPORT_IMG", "Uploading media files")
|
||||
|
||||
}
|
||||
|
||||
// Removes a list of files.
|
||||
func BatchUnlink(w io.Writer, files []string) {
|
||||
|
||||
_, wImplementsHttpFlusher := interface{}(w).(http.Flusher)
|
||||
|
||||
maxConcTasks := min(10, runtime.NumCPU())
|
||||
|
||||
// Set a semaphore to restrict the number of concurrent upload tasks.
|
||||
semaphore := make(chan struct{}, maxConcTasks)
|
||||
wg := &sync.WaitGroup{}
|
||||
|
||||
for _, f := range(files) {
|
||||
|
||||
semaphore <- struct{}{} // acquire
|
||||
wg.Add(1)
|
||||
|
||||
go func() {
|
||||
|
||||
defer wg.Done()
|
||||
err := os.Remove(f)
|
||||
if err != nil {
|
||||
panic("Failed to delete file " + f)
|
||||
}
|
||||
fmt.Fprintf(w, "Delete file %s\n", f)
|
||||
<-semaphore // release
|
||||
|
||||
}()
|
||||
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
if wImplementsHttpFlusher == true {
|
||||
w.(http.Flusher).Flush()
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user