@@ -42,6 +42,7 @@ public enum PinType {all, direct, indirect, recursive}
4242 public final Update update = new Update ();
4343 public final DHT dht = new DHT ();
4444 public final File file = new File ();
45+ public final Files files = new Files ();
4546 public final Stats stats = new Stats ();
4647 public final Name name = new Name ();
4748 public final Pubsub pubsub = new Pubsub ();
@@ -477,8 +478,93 @@ public Map ls(Multihash path) throws IOException {
477478 }
478479 }
479480
480- // Network commands
481+ public class Files {
482+
483+ public String chcid () throws IOException {
484+ return retrieveString ("files/chcid" );
485+ }
486+
487+ public String chcid (String path ) throws IOException {
488+ String arg = URLEncoder .encode (path , "UTF-8" );
489+ return retrieveString ("files/chcid?args=" + arg );
490+ }
491+
492+ public String cp (String source , String dest , boolean parents ) throws IOException {
493+ return retrieveString ("files/cp?arg=" + URLEncoder .encode (source , "UTF-8" ) + "&arg=" + URLEncoder .encode (dest , "UTF-8" ) + "&parents=" + parents );
494+ }
495+
496+ public Map flush () throws IOException {
497+ return retrieveMap ("files/flush" );
498+ }
499+
500+ public Map flush (String path ) throws IOException {
501+ String arg = URLEncoder .encode (path , "UTF-8" );
502+ return retrieveMap ("files/flush?arg=" + arg );
503+ }
504+
505+ public Map ls () throws IOException {
506+ return retrieveMap ("files/ls" );
507+ }
508+
509+ public Map ls (String path ) throws IOException {
510+ String arg = URLEncoder .encode (path , "UTF-8" );
511+ return retrieveMap ("files/ls?arg=" + arg );
512+ }
481513
514+ public Map ls (String path , boolean longListing , boolean u ) throws IOException {
515+ String arg = URLEncoder .encode (path , "UTF-8" );
516+ return retrieveMap ("files/ls?arg=" + arg + "&long=" + longListing + "&U=" + u );
517+ }
518+
519+ public String mkdir (String path , boolean parents ) throws IOException {
520+ String arg = URLEncoder .encode (path , "UTF-8" );
521+ return retrieveString ("files/mkdir?arg=" + arg + "&parents=" + parents );
522+ }
523+
524+ public String mkdir (String path , boolean parents , int cidVersion , Multihash hash ) throws IOException {
525+ String arg = URLEncoder .encode (path , "UTF-8" );
526+ return retrieveString ("files/mkdir?arg=" + arg + "&parents=" + parents + "&cid-version=" + cidVersion + "&hash=" + hash );
527+ }
528+
529+ public String mv (String source , String dest ) throws IOException {
530+ return retrieveString ("files/mv?arg=" + URLEncoder .encode (source , "UTF-8" ) + "&arg=" + URLEncoder .encode (dest , "UTF-8" ));
531+ }
532+
533+ public byte [] read (String path ) throws IOException {
534+ String arg = URLEncoder .encode (path , "UTF-8" );
535+ return retrieve ("files/read?arg=" + arg );
536+ }
537+
538+ public byte [] read (String path , int offset , int count ) throws IOException {
539+ String arg = URLEncoder .encode (path , "UTF-8" );
540+ return retrieve ("files/read?arg=" + arg + "&offset=" + offset + "&count=" + count );
541+ }
542+
543+ public String rm (String path , boolean recursive , boolean force ) throws IOException {
544+ String arg = URLEncoder .encode (path , "UTF-8" );
545+ return retrieveString ("files/rm?arg=" + arg + "&recursive=" + recursive + "&force=" + force );
546+ }
547+
548+ public Map stat (String path ) throws IOException {
549+ String arg = URLEncoder .encode (path , "UTF-8" );
550+ return retrieveMap ("files/stat?arg=" + arg );
551+ }
552+
553+ public String write (String path , NamedStreamable uploadFile , boolean create , boolean parents ) throws IOException {
554+ String arg = URLEncoder .encode (path , "UTF-8" );
555+ String rpcParams = "files/write?arg=" + arg + "&create=" + create + "&parents=" + parents ;
556+ URL target = new URL (protocol ,host ,port ,version + rpcParams );
557+ Multipart m = new Multipart (target .toString (),"UTF-8" );
558+ if (uploadFile .isDirectory ()) {
559+ throw new IllegalArgumentException ("Input must be a file" );
560+ } else {
561+ m .addFilePart ("file" , Paths .get ("" ), uploadFile );
562+ }
563+ return m .finish ();
564+ }
565+ }
566+
567+ // Network commands
482568 public List <MultiAddress > bootstrap () throws IOException {
483569 return ((List <String >)retrieveMap ("bootstrap/" ).get ("Peers" ))
484570 .stream ()
@@ -693,6 +779,11 @@ private void retrieveAndParseStream(String path, Consumer<Object> results, Consu
693779 getObjectStream (retrieveStream (path ), d -> results .accept (JSONParser .parse (new String (d ))), err );
694780 }
695781
782+ private String retrieveString (String path ) throws IOException {
783+ URL target = new URL (protocol , host , port , version + path );
784+ return new String (IPFS .get (target , connectTimeoutMillis , readTimeoutMillis ));
785+ }
786+
696787 private byte [] retrieve (String path ) throws IOException {
697788 URL target = new URL (protocol , host , port , version + path );
698789 return IPFS .get (target , connectTimeoutMillis , readTimeoutMillis );
0 commit comments