Skip to content

Commit

Permalink
commands: Pass FileInfo between the cli and daemon with a MIME header
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: W. Trevor King <wking@tremily.us>
  • Loading branch information
wking committed Jun 24, 2015
1 parent c789d9a commit a71c07a
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 4 deletions.
54 changes: 51 additions & 3 deletions commands/files/multipartfile.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package files

import (
"fmt"
"mime"
"mime/multipart"
"net/http"
"net/url"
"os"
"strconv"
"time"
)

const (
Expand Down Expand Up @@ -40,17 +43,62 @@ func NewFileFromPart(part *multipart.Part) (File, error) {
return nil, err
}

name := f.FileName()
if f.IsDirectory() {
boundary, found := params["boundary"]
if !found {
return nil, http.ErrMissingBoundary
}

f.Reader = multipart.NewReader(part, boundary)
f.stat = NewDummyDirectoryFileInfo(name)
}

name := f.FileName()
fileInfoString := part.Header.Get("File-Info")
if fileInfoString == "" {
if f.IsDirectory() {
f.stat = NewDummyDirectoryFileInfo(name)
} else {
f.stat = NewDummyRegularFileInfo(name)
}
} else {
f.stat = NewDummyRegularFileInfo(name)
version, params, err := mime.ParseMediaType(fileInfoString)
if version != "ipfs/v1" {
return nil, fmt.Errorf(
"unrecognized File-Info version: %s (%s)", version, fileInfoString)
}
sizeString, ok := params["size"]
if !ok {
return nil, fmt.Errorf(
"File-Info missing \"size\" parameter: %s", fileInfoString)
}
size, err := strconv.ParseInt(sizeString, 0, 64)
if err != nil {
return nil, err
}
modeString, ok := params["mode"]
if !ok {
return nil, fmt.Errorf(
"File-Info missing \"mode\" parameter: %s", fileInfoString)
}
mode, err := strconv.ParseUint(modeString, 0, 32)
if err != nil {
return nil, err
}
modTimeString, ok := params["mod-time"]
if !ok {
return nil, fmt.Errorf(
"File-Info missing \"mod-time\" parameter: %s", fileInfoString)
}
modTime, err := time.Parse(time.RFC3339Nano, modTimeString)
if err != nil {
return nil, err
}
f.stat = &fileInfo{
name: name,
size: size,
mode: os.FileMode(mode),
modTime: modTime,
}
}

return f, nil
Expand Down
15 changes: 14 additions & 1 deletion commands/http/multifilereader.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"bytes"
"fmt"
"io"
"mime"
"mime/multipart"
"net/textproto"
"net/url"
"sync"
"time"

files "github.com/ipfs/go-ipfs/commands/files"
)
Expand Down Expand Up @@ -90,7 +92,18 @@ func (mfr *MultiFileReader) Read(buf []byte) (written int, err error) {
header.Set("Content-Type", "application/octet-stream")
}

_, err := mfr.mpWriter.CreatePart(header)
stat, err := file.Stat()
if err != nil {
return 0, err
}

params := map[string]string{
"mode": fmt.Sprintf("%#o", stat.Mode()),
"size": fmt.Sprintf("%d", stat.Size()),
"mod-time": stat.ModTime().Format(time.RFC3339Nano),
}
header.Set("File-Info", mime.FormatMediaType("ipfs/v1", params))
_, err = mfr.mpWriter.CreatePart(header)
if err != nil {
return 0, err
}
Expand Down

0 comments on commit a71c07a

Please sign in to comment.