-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.go
108 lines (92 loc) · 2.52 KB
/
upload.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"sync"
"time"
dac "github.com/xinsnake/go-http-digest-auth-client"
)
func main() {
outDir := "./out"
var wg sync.WaitGroup
concurrencyLimit := 15
sem := make(chan struct{}, concurrencyLimit)
counter := 0
counterMutex := sync.Mutex{}
// Ticker to refresh the counter every 500ms
ticker := time.NewTicker(500 * time.Millisecond)
go func() {
for range ticker.C {
counterMutex.Lock()
fmt.Printf("Files uploaded: %d\n", counter)
counterMutex.Unlock()
}
}()
err := filepath.Walk(outDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
fmt.Printf("Error accessing file: %v\n", err)
return nil
}
if !info.IsDir() {
wg.Add(1)
sem <- struct{}{} // Acquire a slot in the semaphore
go func(path string) {
defer wg.Done()
defer func() { <-sem }() // Release the slot in the semaphore
fileContents, err := ioutil.ReadFile(path)
if err != nil {
fmt.Printf("Error reading file %s: %v\n", path, err)
return
}
url := "http://localhost:8890/sparql-graph-crud-auth/?graph-uri=https://synbiohub.org/public"
t := dac.NewTransport("dba", "dba")
req, err := http.NewRequest("POST", url, bytes.NewBuffer(fileContents))
if err != nil {
fmt.Printf("Error creating request for file %s: %v\n", path, err)
return
}
req.Header.Set("Content-Type", "application/rdf+xml")
resp, err := t.RoundTrip(req)
if err != nil {
fmt.Printf("Error sending request for file %s: %v\n", path, err)
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
if resp.StatusCode == http.StatusCreated {
fmt.Printf("Received status code %d, retrying in 1 second...\n", resp.StatusCode)
time.Sleep(1 * time.Second)
resp, err = t.RoundTrip(req)
if err != nil {
fmt.Printf("Error sending request for file %s: %v\n", path, err)
return
} else {
counterMutex.Lock()
counter++
counterMutex.Unlock()
}
defer resp.Body.Close()
} else {
fmt.Printf("Failed to upload file %s: received status code %d\n", path, resp.StatusCode)
return
}
} else {
counterMutex.Lock()
counter++
counterMutex.Unlock()
}
}(path)
}
return nil
})
if err != nil {
fmt.Printf("Error walking directory: %v\n", err)
}
wg.Wait()
ticker.Stop() // Stop the ticker when all uploads are done
fmt.Printf("Final count of files uploaded: %d\n", counter)
}