Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-dms3
Commits
51bfe06f
Commit
51bfe06f
authored
Nov 16, 2014
by
Matt Bell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
commands/http: Support recursive multipart in MultiFileReader
parent
9d2ee4f1
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
39 additions
and
6 deletions
+39
-6
commands/http/client.go
commands/http/client.go
+1
-1
commands/http/multifilereader.go
commands/http/multifilereader.go
+38
-5
No files found.
commands/http/client.go
View file @
51bfe06f
...
...
@@ -50,7 +50,7 @@ func (c *client) Send(req cmds.Request) (cmds.Response, error) {
var
fileReader
*
MultiFileReader
if
req
.
Files
()
!=
nil
{
fileReader
=
NewMultiFileReader
(
req
.
Files
())
fileReader
=
NewMultiFileReader
(
req
.
Files
()
,
true
)
}
path
:=
strings
.
Join
(
req
.
Path
(),
"/"
)
...
...
commands/http/multifilereader.go
View file @
51bfe06f
...
...
@@ -2,8 +2,10 @@ package http
import
(
"bytes"
"fmt"
"io"
"mime/multipart"
"net/textproto"
cmds
"github.com/jbenet/go-ipfs/commands"
)
...
...
@@ -12,15 +14,20 @@ type MultiFileReader struct {
io
.
Reader
files
cmds
.
File
currentFile
cmds
.
File
currentFile
io
.
Reader
buf
bytes
.
Buffer
mpWriter
*
multipart
.
Writer
closed
bool
// if true, the data will be type 'multipart/form-data'
// if false, the data will be type 'multipart/mixed'
form
bool
}
func
NewMultiFileReader
(
file
cmds
.
File
)
*
MultiFileReader
{
func
NewMultiFileReader
(
file
cmds
.
File
,
form
bool
)
*
MultiFileReader
{
mfr
:=
&
MultiFileReader
{
files
:
file
,
form
:
form
,
}
mfr
.
mpWriter
=
multipart
.
NewWriter
(
&
mfr
.
buf
)
...
...
@@ -35,16 +42,42 @@ func (mfr *MultiFileReader) Read(buf []byte) (written int, err error) {
// if the current file isn't set, advance to the next file
if
mfr
.
currentFile
==
nil
{
mfr
.
currentF
ile
,
err
=
mfr
.
files
.
NextFile
()
if
err
==
io
.
EOF
||
(
err
==
nil
&&
mfr
.
currentF
ile
==
nil
)
{
f
ile
,
err
:
=
mfr
.
files
.
NextFile
()
if
err
==
io
.
EOF
||
(
err
==
nil
&&
f
ile
==
nil
)
{
mfr
.
mpWriter
.
Close
()
mfr
.
closed
=
true
}
else
if
err
!=
nil
{
return
0
,
err
}
// handle starting a new file part
if
!
mfr
.
closed
{
_
,
err
:=
mfr
.
mpWriter
.
CreateFormFile
(
"file"
,
mfr
.
currentFile
.
FileName
())
if
file
.
IsDirectory
()
{
// if file is a directory, create a multifilereader from it
// (using 'multipart/mixed')
mfr
.
currentFile
=
NewMultiFileReader
(
file
,
false
)
}
else
{
// otherwise, use the file as a reader to read its contents
mfr
.
currentFile
=
file
}
// write the boundary and headers
header
:=
make
(
textproto
.
MIMEHeader
)
if
mfr
.
form
{
contentDisposition
:=
fmt
.
Sprintf
(
"form-data; name=
\"
file
\"
; filename=
\"
%s
\"
"
,
file
.
FileName
())
header
.
Set
(
"Content-Disposition"
,
contentDisposition
)
}
else
{
header
.
Set
(
"Content-Disposition"
,
fmt
.
Sprintf
(
"file; filename=
\"
%s
\"
"
,
file
.
FileName
()))
}
if
file
.
IsDirectory
()
{
boundary
:=
mfr
.
currentFile
.
(
*
MultiFileReader
)
.
Boundary
()
header
.
Set
(
"Content-Type"
,
fmt
.
Sprintf
(
"multipart/mixed; boundary=%s"
,
boundary
))
}
else
{
header
.
Set
(
"Content-Type"
,
"application/octet-stream"
)
}
_
,
err
:=
mfr
.
mpWriter
.
CreatePart
(
header
)
if
err
!=
nil
{
return
0
,
err
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment