Skip to content

Commit 69f214a

Browse files
committed
Add request headers to file submission
1 parent 78540e0 commit 69f214a

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

src/main/java/io/ipfs/api/IPFS.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ public List<MerkleNode> add(NamedStreamable file, boolean wrap, boolean hashOnly
9595
}
9696

9797
public List<MerkleNode> add(List<NamedStreamable> files, boolean wrap, boolean hashOnly) throws IOException {
98-
Multipart m = new Multipart(protocol + "://" + host + ":" + port + version + "add?stream-channels=true&w=" + wrap + "&n=" + hashOnly, "UTF-8");
98+
Multipart m = new Multipart(protocol + "://" + host + ":" + port + version + "add?stream-channels=true&w=" + wrap + "&n=" + hashOnly, "UTF-8", headers);
99+
99100
for (NamedStreamable file : files) {
100101
if (file.isDirectory()) {
101102
m.addSubtree(Paths.get(""), file);
@@ -306,7 +307,7 @@ public List<MerkleNode> put(List<byte[]> data, Optional<String> format) throws I
306307

307308
public MerkleNode put(byte[] data, Optional<String> format) throws IOException {
308309
String fmt = format.map(f -> "&format=" + f).orElse("");
309-
Multipart m = new Multipart(protocol + "://" + host + ":" + port + version + "block/put?stream-channels=true" + fmt, "UTF-8");
310+
Multipart m = new Multipart(protocol + "://" + host + ":" + port + version + "block/put?stream-channels=true" + fmt, "UTF-8", headers);
310311
try {
311312
m.addFilePart("file", Paths.get(""), new NamedStreamable.ByteArrayWrapper(data));
312313
String res = m.finish();
@@ -325,7 +326,7 @@ public Map stat(Multihash hash) throws IOException {
325326
*/
326327
public class IPFSObject {
327328
public List<MerkleNode> put(List<byte[]> data) throws IOException {
328-
Multipart m = new Multipart(protocol + "://" + host + ":" + port + version + "object/put?stream-channels=true", "UTF-8");
329+
Multipart m = new Multipart(protocol + "://" + host + ":" + port + version + "object/put?stream-channels=true", "UTF-8", headers);
329330
for (byte[] f : data)
330331
m.addFilePart("file", Paths.get(""), new NamedStreamable.ByteArrayWrapper(f));
331332
String res = m.finish();
@@ -335,7 +336,7 @@ public List<MerkleNode> put(List<byte[]> data) throws IOException {
335336
public List<MerkleNode> put(String encoding, List<byte[]> data) throws IOException {
336337
if (!"json".equals(encoding) && !"protobuf".equals(encoding))
337338
throw new IllegalArgumentException("Encoding must be json or protobuf");
338-
Multipart m = new Multipart(protocol + "://" + host + ":" + port + version + "object/put?stream-channels=true&encoding=" + encoding, "UTF-8");
339+
Multipart m = new Multipart(protocol + "://" + host + ":" + port + version + "object/put?stream-channels=true&encoding=" + encoding, "UTF-8", headers);
339340
for (byte[] f : data)
340341
m.addFilePart("file", Paths.get(""), new NamedStreamable.ByteArrayWrapper(f));
341342
String res = m.finish();
@@ -389,7 +390,7 @@ public MerkleNode patch(Multihash base, String command, Optional<byte[]> data, O
389390
case "append-data":
390391
if (!data.isPresent())
391392
throw new IllegalStateException("set-data requires data!");
392-
Multipart m = new Multipart(protocol + "://" + host + ":" + port + version + "object/patch/" + command + "?arg=" + base.toBase58() + "&stream-channels=true", "UTF-8");
393+
Multipart m = new Multipart(protocol + "://" + host + ":" + port + version + "object/patch/" + command + "?arg=" + base.toBase58() + "&stream-channels=true", "UTF-8", headers);
393394
m.addFilePart("file", Paths.get(""), new NamedStreamable.ByteArrayWrapper(data.get()));
394395
String res = m.finish();
395396
return MerkleNode.fromJSON(JSONParser.parse(res));
@@ -534,7 +535,7 @@ public MerkleNode put(byte[] object, String outputFormat) throws IOException {
534535

535536
public MerkleNode put(String inputFormat, byte[] object, String outputFormat) throws IOException {
536537
String prefix = protocol + "://" + host + ":" + port + version;
537-
Multipart m = new Multipart(prefix + "dag/put/?stream-channels=true&input-enc=" + inputFormat + "&f=" + outputFormat, "UTF-8");
538+
Multipart m = new Multipart(prefix + "dag/put/?stream-channels=true&input-enc=" + inputFormat + "&f=" + outputFormat, "UTF-8", headers);
538539
m.addFilePart("file", Paths.get(""), new NamedStreamable.ByteArrayWrapper(object));
539540
String res = m.finish();
540541
return MerkleNode.fromJSON(JSONParser.parse(res));
@@ -589,7 +590,7 @@ public Map show() throws IOException {
589590
}
590591

591592
public void replace(NamedStreamable file) throws IOException {
592-
Multipart m = new Multipart(protocol + "://" + host + ":" + port + version + "config/replace?stream-channels=true", "UTF-8");
593+
Multipart m = new Multipart(protocol + "://" + host + ":" + port + version + "config/replace?stream-channels=true", "UTF-8", headers);
593594
m.addFilePart("file", Paths.get(""), file);
594595
String res = m.finish();
595596
}
@@ -748,7 +749,7 @@ private static byte[] post(URL target, byte[] body, Map<String, String> headers)
748749
return readFully(in);
749750
}
750751

751-
private static void addHeaders(HttpURLConnection conn, Map<String, String> headers) {
752+
public static void addHeaders(HttpURLConnection conn, Map<String, String> headers) {
752753
// add headers
753754
for (Map.Entry<String, String> header : headers.entrySet()) {
754755
conn.setRequestProperty(header.getKey(), header.getValue());

src/main/java/io/ipfs/api/Multipart.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ public class Multipart {
1111
private HttpURLConnection httpConn;
1212
private String charset;
1313
private OutputStream out;
14+
private Map<String, String> headers;
1415

15-
public Multipart(String requestURL, String charset) {
16+
public Multipart(String requestURL, String charset, Map<String, String> headers) {
1617
this.charset = charset;
18+
this.headers = headers;
1719

1820
boundary = createBoundary();
1921

@@ -26,6 +28,7 @@ public Multipart(String requestURL, String charset) {
2628
httpConn.setRequestProperty("Expect", "100-continue");
2729
httpConn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
2830
httpConn.setRequestProperty("User-Agent", "Java IPFS CLient");
31+
IPFS.addHeaders(httpConn, headers);
2932
out = httpConn.getOutputStream();
3033
} catch (IOException e) {
3134
throw new RuntimeException(e.getMessage(), e);
@@ -36,11 +39,11 @@ public static String createBoundary() {
3639
Random r = new Random();
3740
String allowed = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
3841
StringBuilder b = new StringBuilder();
39-
for (int i=0; i < 32; i++)
42+
for (int i = 0; i < 32; i++)
4043
b.append(allowed.charAt(r.nextInt(allowed.length())));
4144
return b.toString();
4245
}
43-
46+
4447
private Multipart append(String value) throws IOException {
4548
out.write(value.getBytes(charset));
4649
return this;
@@ -59,7 +62,7 @@ public void addFormField(String name, String value) throws IOException {
5962
public void addSubtree(Path parentPath, NamedStreamable dir) throws IOException {
6063
Path dirPath = parentPath.resolve(dir.getName().get());
6164
addDirectoryPart(dirPath);
62-
for (NamedStreamable f: dir.getChildren()) {
65+
for (NamedStreamable f : dir.getChildren()) {
6366
if (f.isDirectory())
6467
addSubtree(dirPath, f);
6568
else
@@ -86,7 +89,7 @@ private static String encode(String in) {
8689
}
8790

8891
public void addFilePart(String fieldName, Path parent, NamedStreamable uploadFile) throws IOException {
89-
Optional<String> fileName = uploadFile.getName().map(n -> encode(parent.resolve(n).toString().replace('\\','/')));
92+
Optional<String> fileName = uploadFile.getName().map(n -> encode(parent.resolve(n).toString().replace('\\', '/')));
9093
append("--").append(boundary).append(LINE_FEED);
9194
if (!fileName.isPresent())
9295
append("Content-Disposition: file; name=\"").append(fieldName).append("\";").append(LINE_FEED);

0 commit comments

Comments
 (0)