diff options
author | Louis Holbrook <nolash@users.noreply.github.com> | 2017-04-06 20:21:16 +0800 |
---|---|---|
committer | Felix Lange <fjl@users.noreply.github.com> | 2017-04-06 20:21:16 +0800 |
commit | 0ec1104ba92c226c279389bbeb88ca515208f030 (patch) | |
tree | de5d5d03371702e5f9bb248c4b9f1a1289cf1876 /swarm/api/client/client.go | |
parent | c76ad944920300be58446ddd1a50c8d693957774 (diff) | |
download | dexon-0ec1104ba92c226c279389bbeb88ca515208f030.tar dexon-0ec1104ba92c226c279389bbeb88ca515208f030.tar.gz dexon-0ec1104ba92c226c279389bbeb88ca515208f030.tar.bz2 dexon-0ec1104ba92c226c279389bbeb88ca515208f030.tar.lz dexon-0ec1104ba92c226c279389bbeb88ca515208f030.tar.xz dexon-0ec1104ba92c226c279389bbeb88ca515208f030.tar.zst dexon-0ec1104ba92c226c279389bbeb88ca515208f030.zip |
cmd/swarm: allow uploading from stdin (#3744)
- intended to be a swarm alternative to termbin.com
- added --stdin flag to swarm executable. if set, swarm will
read data from stdin and postRaw it.
Diffstat (limited to 'swarm/api/client/client.go')
-rw-r--r-- | swarm/api/client/client.go | 26 |
1 files changed, 25 insertions, 1 deletions
diff --git a/swarm/api/client/client.go b/swarm/api/client/client.go index 15e44f35d..ef5335be3 100644 --- a/swarm/api/client/client.go +++ b/swarm/api/client/client.go @@ -89,8 +89,32 @@ func (c *Client) UploadDirectory(dir string, defaultPath string) (string, error) return mhash, err } -func (c *Client) UploadFile(file string, fi os.FileInfo) (ManifestEntry, error) { +func (c *Client) UploadFile(file string, fi os.FileInfo, mimetype_hint string) (ManifestEntry, error) { + var mimetype string hash, err := c.uploadFileContent(file, fi) + if mimetype_hint != "" { + mimetype = mimetype_hint + log.Info("Mime type set by override", "mime", mimetype) + } else { + ext := filepath.Ext(file) + log.Info("Ext", "ext", ext, "file", file) + if ext != "" { + mimetype = mime.TypeByExtension(filepath.Ext(fi.Name())) + log.Info("Mime type set by fileextension", "mime", mimetype, "ext", filepath.Ext(file)) + } else { + f, err := os.Open(file) + if err == nil { + first512 := make([]byte, 512) + fread, _ := f.ReadAt(first512, 0) + if fread > 0 { + mimetype = http.DetectContentType(first512[:fread]) + log.Info("Mime type set by autodetection", "mime", mimetype) + } + } + f.Close() + } + + } m := ManifestEntry{ Hash: hash, ContentType: mime.TypeByExtension(filepath.Ext(fi.Name())), |