diff options
author | Alexey Sharov <www.pismeco@gmail.com> | 2018-10-01 19:39:39 +0800 |
---|---|---|
committer | Anton Evangelatov <anton.evangelatov@gmail.com> | 2018-10-01 19:39:39 +0800 |
commit | dc5d643bb59812cda578fac941c2f1da316bc9d7 (patch) | |
tree | 7405f387672f0548eb4734a93581780c96cee7a9 /swarm/api/filesystem.go | |
parent | b69942befeb9f1af55cad0f91953bdaea2ea3efb (diff) | |
download | go-tangerine-dc5d643bb59812cda578fac941c2f1da316bc9d7.tar go-tangerine-dc5d643bb59812cda578fac941c2f1da316bc9d7.tar.gz go-tangerine-dc5d643bb59812cda578fac941c2f1da316bc9d7.tar.bz2 go-tangerine-dc5d643bb59812cda578fac941c2f1da316bc9d7.tar.lz go-tangerine-dc5d643bb59812cda578fac941c2f1da316bc9d7.tar.xz go-tangerine-dc5d643bb59812cda578fac941c2f1da316bc9d7.tar.zst go-tangerine-dc5d643bb59812cda578fac941c2f1da316bc9d7.zip |
cmd/swarm, swarm: cross-platform Content-Type detection (#17782)
- Mime types generator (Standard "mime" package rely on system-settings, see mime.osInitMime)
- Changed swarm/api.Upload:
- simplify I/O throttling by semaphore primitive and use file name where possible
- f.Close() must be called in Defer - otherwise panic or future added early return will cause leak of file descriptors
- one error was suppressed
Diffstat (limited to 'swarm/api/filesystem.go')
-rw-r--r-- | swarm/api/filesystem.go | 81 |
1 files changed, 39 insertions, 42 deletions
diff --git a/swarm/api/filesystem.go b/swarm/api/filesystem.go index 8251ebc4d..43695efc1 100644 --- a/swarm/api/filesystem.go +++ b/swarm/api/filesystem.go @@ -21,7 +21,6 @@ import ( "context" "fmt" "io" - "net/http" "os" "path" "path/filepath" @@ -97,51 +96,50 @@ func (fs *FileSystem) Upload(lpath, index string, toEncrypt bool) (string, error list = append(list, entry) } - cnt := len(list) - errors := make([]error, cnt) - done := make(chan bool, maxParallelFiles) - dcnt := 0 - awg := &sync.WaitGroup{} + errors := make([]error, len(list)) + sem := make(chan bool, maxParallelFiles) + defer close(sem) for i, entry := range list { - if i >= dcnt+maxParallelFiles { - <-done - dcnt++ - } - awg.Add(1) - go func(i int, entry *manifestTrieEntry, done chan bool) { + sem <- true + go func(i int, entry *manifestTrieEntry) { + defer func() { <-sem }() + f, err := os.Open(entry.Path) - if err == nil { - stat, _ := f.Stat() - var hash storage.Address - var wait func(context.Context) error - ctx := context.TODO() - hash, wait, err = fs.api.fileStore.Store(ctx, f, stat.Size(), toEncrypt) - if hash != nil { - list[i].Hash = hash.Hex() - } - err = wait(ctx) - awg.Done() - if err == nil { - first512 := make([]byte, 512) - fread, _ := f.ReadAt(first512, 0) - if fread > 0 { - mimeType := http.DetectContentType(first512[:fread]) - if filepath.Ext(entry.Path) == ".css" { - mimeType = "text/css" - } - list[i].ContentType = mimeType - } - } - f.Close() + if err != nil { + errors[i] = err + return + } + defer f.Close() + + stat, err := f.Stat() + if err != nil { + errors[i] = err + return + } + + var hash storage.Address + var wait func(context.Context) error + ctx := context.TODO() + hash, wait, err = fs.api.fileStore.Store(ctx, f, stat.Size(), toEncrypt) + if hash != nil { + list[i].Hash = hash.Hex() } - errors[i] = err - done <- true - }(i, entry, done) + if err := wait(ctx); err != nil { + errors[i] = err + return + } + + list[i].ContentType, err = DetectContentType(f.Name(), f) + if err != nil { + errors[i] = err + return + } + + }(i, entry) } - for dcnt < cnt { - <-done - dcnt++ + for i := 0; i < cap(sem); i++ { + sem <- true } trie := &manifestTrie{ @@ -168,7 +166,6 @@ func (fs *FileSystem) Upload(lpath, index string, toEncrypt bool) (string, error if err2 == nil { hs = trie.ref.Hex() } - awg.Wait() return hs, err2 } |