forked from ethersphere/swarm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd/swarm, swarm/api: Use common method to detect Content-Type (ether…
…sphere#527, ethersphere#944) Changed swarm/api.Upload: - when using WaitGroup no reason to use done counter. - f.Close() must be called in Defer - otherwise panic or future added early return will cause leak of file descriptors - use common DetectContentType method - one error was suppressed
- Loading branch information
1 parent
9c7f0d1
commit 33bf7d0
Showing
6 changed files
with
111 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package api | ||
|
||
import ( | ||
"mime" | ||
"net/http" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/ethereum/go-ethereum/log" | ||
) | ||
|
||
// detect first by file extension | ||
// otherwise detect by file content | ||
// if it cannot determine a more specific one, it | ||
// returns "application/octet-stream". | ||
func DetectContentType(file string) string { | ||
if ext := filepath.Ext(file); ext != "" { | ||
if mimeType := mime.TypeByExtension(ext); mimeType != "" { | ||
return mimeType | ||
} | ||
} | ||
|
||
f, err := os.Open(file) | ||
if err != nil { | ||
log.Warn("detectMimeType: can't open file", "file", file, "err", err) | ||
return "application/octet-stream" | ||
} | ||
defer f.Close() | ||
buf := make([]byte, 512) | ||
if n, _ := f.Read(buf); n > 0 { | ||
return http.DetectContentType(buf) | ||
} | ||
return "application/octet-stream" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package api | ||
|
||
import "testing" | ||
|
||
func TestDetectContentType(t *testing.T) { | ||
for _, v := range []struct { | ||
path string | ||
expectedContentType string | ||
}{ | ||
{ | ||
path: "./path/to/file.pdf", | ||
expectedContentType: "application/pdf", | ||
}, | ||
{ | ||
path: "./path/to/file.md", | ||
expectedContentType: "application/octet-stream", | ||
}, | ||
{ | ||
path: "", | ||
expectedContentType: "application/octet-stream", | ||
}, | ||
{ | ||
path: "noextension", | ||
expectedContentType: "application/octet-stream", | ||
}, | ||
{ | ||
path: "./path/to/noextension", | ||
expectedContentType: "application/octet-stream", | ||
}, | ||
{ | ||
path: "./1.css", | ||
expectedContentType: "text/css; charset=utf-8", | ||
}, | ||
} { | ||
detected := DetectContentType(v.path) | ||
if detected != v.expectedContentType { | ||
t.Fatalf("Expected mime type %s, got %s", v.expectedContentType, detected) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters