package com.everbox4j;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import org.apache.commons.codec.binary.Base64;
import org.json.JSONObject;
import org.junit.BeforeClass;
import org.junit.Test;
import org.nutz.json.Json;
import org.nutz.lang.Streams;
import com.everbox4j.bean.EverBoxObject;
import com.snda.everbox.sdk.account.Account;
import com.snda.everbox.sdk.fs.FS;
import com.snda.everbox.sdk.proxy.EverBoxJsonProxy;
import com.snda.everbox.utils.Digest;
public class BaseTest {
@BeforeClass
public static void init() throws Throwable {
EverboxAPI.login();
}
// @Test
public void test_ping() throws Throwable {
assertEquals(200, Account.ping());
}
// @Test
public void test_listHome() throws Throwable {
JSONObject obj = new JSONObject();
// obj.put("path", "/home");
StringBuilder sb = new StringBuilder();
// int rc = FS.get(obj, sb);
// assertEquals(200, rc);
// System.out.println(sb.toString());
// EverBoxObject home = Json.fromJson(EverBoxObject.class, sb.toString());
//// home = list(home);
// System.out.println("------------------------------------------------------------");
// System.out.println(Json.toJson(home));
//
// assertNotNull(home.getEntries());
// assertTrue(home.getEditTime() > 0);
// assertEquals("/home", home.getPath());
// obj = new JSONObject();
// obj.put("path", "/home");
// FS.get(obj, sb);
// System.out.println(sb);
obj = new JSONObject();
obj.put("path", "/home/ABC.txt");
obj.put("aimType", 0);
// obj.put("showType", 3);
obj.put("base", "");
obj.put("list", 1);
sb = new StringBuilder();
FS.thumbnail(obj, sb);
System.out.println(sb);
JSONObject thumbnailInfo = new JSONObject(sb.toString());
System.out.println(thumbnailInfo.get("url"));
byte[] data = new EverBoxJsonProxy().CallGetBytes(thumbnailInfo.getString("url"));
System.out.println(data.length);
FileOutputStream fos = new FileOutputStream("download.data");
fos.write(data);
fos.flush();
fos.close();
}
public EverBoxObject list(EverBoxObject parent) throws Throwable {
if (parent.getEntries() == null || parent.getEntries().size() == 0)
return parent;
for (EverBoxObject ebo : parent.getEntries()) {
if(ebo.getType() == 2) {
JSONObject obj = new JSONObject();
obj.put("path", ebo.getPath());
StringBuilder sb = new StringBuilder();
int rc = FS.get(obj, sb);
assertEquals(200, rc);
EverBoxObject me = Json.fromJson(EverBoxObject.class, sb.toString());
me = list(me);
ebo.setEntries(me.getEntries());
}
}
return parent;
}
@Test
public void test_upload() throws Throwable {
EverBoxObject ebo = new EverBoxObject();
ebo.setPath("/home/ta.rtf");
EverboxAPI.upload(ebo);
// File f = new File("everbox/ta.rtf");
// byte[] sha = Digest.SHA1(new FileInputStream(f), (int)f.length());
//// System.out.println(new String(sha));
// String key = Base64.encodeBase64String(sha).replace('/', '_');
// key.substring(0, key.lastIndexOf('='));
}
public static void main(String[] args) throws Throwable{
File f = new File("G:\\EverBox\\360safe_cq.exe");
byte[] sha = Digest.SHA1(new FileInputStream(f), (int)f.length());
String key = Base64.encodeBase64String(sha).replace('/', '_');
key = key.substring(0, key.lastIndexOf('=')+1);
System.out.println("---"+key+"--------");
System.out.println("MDAyZTRmOTg0MzJkNGNjOA==");
}
}