Skip to content

Commit

Permalink
added minio interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Orbiter committed Aug 24, 2020
1 parent 19864c9 commit d6ea5ac
Show file tree
Hide file tree
Showing 7 changed files with 790 additions and 1 deletion.
1 change: 0 additions & 1 deletion .settings/org.eclipse.buildship.core.prefs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
build.commands=org.eclipse.jdt.core.javabuilder
connection.arguments=
connection.gradle.distribution=GRADLE_DISTRIBUTION(WRAPPER)
connection.java.home=null
connection.jvm.arguments=
connection.project.dir=
Expand Down
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ dependencies {
compile 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.0'
compile group: 'javax.xml.bind', name: 'jaxb-api', version: '2.4.+'
compile group: 'org.jsoup', name: 'jsoup', version: '1.+'
compile group: 'io.minio', name: 'minio', version: '7.1.0'
}

tasks.withType(Javadoc) {
Expand Down
181 changes: 181 additions & 0 deletions src/org/loklak/tools/storage/AbstractStorage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
/**
* AbstractStorage
* Copyright 21.08.2020 by Michael Peter Christen, @0rb1t3r
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program in the file lgpl21.txt
* If not, see <http://www.gnu.org/licenses/>.
*/

package org.loklak.tools.storage;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.List;

import org.json.JSONObject;
import org.json.JSONTokener;

public abstract class AbstractStorage implements Storage {


@Override
public String getString(String bucket, String object, Charset encoding, String dflt) throws IOException {
byte[] b = getBytes(bucket, object, null);
if (b == null) return dflt;
return new String(b, encoding);
}

@Override
public byte[] getBytes(String bucket, String object, byte[] dflt) throws IOException {
InputStream is;
try {
is = this.get(bucket, object);
} catch (IOException e) {
return dflt;
}
if (is == null) return dflt;
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int i;
byte[] data = new byte[2048];
while ((i = is.read(data, 0, data.length)) > 0) {
buffer.write(data, 0, i);
}
byte[] b = buffer.toByteArray();
return b;
}

@Override
public Boolean getBoolean(String bucket, String object, Boolean dflt) throws IOException {
byte[] b = getBytes(bucket, object, null);
if (b == null) return dflt;
return Boolean.parseBoolean(new String(b, StandardCharsets.UTF_8));
}

@Override
public Double getDouble(String bucket, String object, Double dflt) throws IOException {
byte[] b = getBytes(bucket, object, null);
if (b == null) return dflt;
return Double.parseDouble(new String(b, StandardCharsets.UTF_8));
}

@Override
public Float getFloat(String bucket, String object, Float dflt) throws IOException {
byte[] b = getBytes(bucket, object, null);
if (b == null) return dflt;
return Float.parseFloat(new String(b, StandardCharsets.UTF_8));
}

@Override
public Integer getInt(String bucket, String object, Integer dflt) throws IOException {
byte[] b = getBytes(bucket, object, null);
if (b == null) return dflt;
return Integer.parseInt(new String(b, StandardCharsets.UTF_8));
}

@Override
public Long getLong(String bucket, String object, Long dflt) throws IOException {
byte[] b = getBytes(bucket, object, null);
if (b == null) return dflt;
return Long.parseLong(new String(b, StandardCharsets.UTF_8));
}

@Override
public JSONObject getJSON(String bucket, String object, JSONObject dflt) throws IOException {
byte[] b = getBytes(bucket, object, null);
if (b == null) return dflt;
return new JSONObject(new JSONTokener(new String(b, StandardCharsets.UTF_8)));
}

@Override
public Storage put(String bucket, String object, String value, Charset encoding) throws IOException {
return this.put(bucket, object, value.getBytes(encoding));
}

@Override
public Storage put(String bucket, String object, boolean value) throws IOException {
return this.put(bucket, object, Boolean.toString(value).getBytes(StandardCharsets.UTF_8));
}

@Override
public Storage put(String bucket, String object, double value) throws IOException {
return this.put(bucket, object, Double.toString(value).getBytes(StandardCharsets.UTF_8));
}

@Override
public Storage put(String bucket, String object, float value) throws IOException {
return this.put(bucket, object, Float.toString(value).getBytes(StandardCharsets.UTF_8));
}

@Override
public Storage put(String bucket, String object, int value) throws IOException {
return this.put(bucket, object, Integer.toString(value).getBytes(StandardCharsets.UTF_8));
}

@Override
public Storage put(String bucket, String object, long value) throws IOException {
return this.put(bucket, object, Long.toString(value).getBytes(StandardCharsets.UTF_8));
}

@Override
public Storage put(String bucket, String object, File f) throws IOException {
return this.put(bucket, object, new FileInputStream(f));
}

@Override
public Storage put(String bucket, String object, byte[] b) throws IOException {
return this.put(bucket, object, new ByteArrayInputStream(b));
}

@Override
public Storage put(String bucket, String object, JSONObject value) throws IOException {
return this.put(bucket, object, value.toString(0).getBytes(StandardCharsets.UTF_8));
}

public static void main(String[] args) {
try {
Storage s = new FileStorage(new File("/tmp/storagetest"));

s
.make("test1")
.put("test2", "p1", "hello", StandardCharsets.UTF_8)
.put("test2", "p2", " world", StandardCharsets.UTF_8)
.append("test2", "p3", "test2", "p1", "p2");

List<String> l = s.list();
System.out.println("buckets: " + l.toString());
for (String bucket: l) {
System.out.println("files in bucket " + bucket + ": " + s.list(bucket, ""));
}

System.out.println("p3: " + s.getString("test2", "p3", StandardCharsets.UTF_8, ""));

System.out.println("filter1 in test2: " + s.list("test2", ""));
System.out.println("filter2 in test2: " + s.list("test2", "p"));
System.out.println("filter3 in test2: " + s.list("test2", "p1"));

s.delete("test2", "p1", "p2", "p3");
s.delete("test1");
s.delete("test2");
} catch (IOException e) {
e.printStackTrace();
}

}
}
Loading

0 comments on commit d6ea5ac

Please sign in to comment.