use of com.github.kevinsawicki.http.HttpRequest in project seadroid by haiwen.
the class SeafConnection method getBlockFromLink.
private File getBlockFromLink(String dlink, FileBlocks fileBlocks, String blkId, String localPath, long fileSize, ProgressMonitor monitor) throws SeafException {
if (dlink == null)
return null;
try {
HttpRequest req = prepareApiFileGetRequest(dlink);
checkRequestResponseStatus(req, HttpURLConnection.HTTP_OK);
if (monitor != null) {
/*if (req.header(HttpRequest.HEADER_CONTENT_LENGTH) == null) {
throw SeafException.illFormatException;
}
Long size = Long.parseLong(req.header(HttpRequest.HEADER_CONTENT_LENGTH));*/
if (req.contentLength() > 0) {
monitor.onProgressNotify(fileSize, true);
}
}
File block = new File(localPath);
// Log.d(DEBUG_TAG, "write to " + block.getAbsolutePath());
if (monitor == null) {
req.receive(block);
} else {
req.bufferSize(DataManager.BUFFER_SIZE);
req.receive(new MonitoredFileOutputStream(fileBlocks, blkId, block, monitor));
}
return block;
} catch (SeafException e) {
throw e;
} catch (UnsupportedEncodingException e) {
throw SeafException.encodingException;
} catch (IOException e) {
e.printStackTrace();
throw SeafException.networkException;
} catch (HttpRequestException e) {
if (e.getCause() instanceof MonitorCancelledException) {
// Log.d(DEBUG_TAG, "download is cancelled");
throw SeafException.userCancelledException;
} else {
throw getSeafExceptionFromHttpRequestException(e);
}
}
}
use of com.github.kevinsawicki.http.HttpRequest in project seadroid by haiwen.
the class SeafConnection method getBlockUploadLink.
private String getBlockUploadLink(String repoID, List<String> blocksId) throws SeafException {
try {
String apiPath;
apiPath = "api2/repos/" + repoID + "/upload-blks-link/";
HttpRequest req = prepareApiPostRequest(apiPath, true, null);
String ids = blocksId.toString().replace("[", "").replace("]", "").replace(" ", "");
req.form("blklist", ids);
checkRequestResponseStatus(req, HttpURLConnection.HTTP_OK);
return new String(req.bytes(), "UTF-8");
} catch (SeafException e) {
Log.d(DEBUG_TAG, e.getCode() + e.getMessage());
throw e;
} catch (Exception e) {
String msg = e.getMessage();
if (msg != null)
Log.d(DEBUG_TAG, msg);
else
Log.d(DEBUG_TAG, "get upload link error", e);
throw SeafException.unknownException;
}
}
use of com.github.kevinsawicki.http.HttpRequest in project seadroid by haiwen.
the class SeafConnection method getUploadLink.
private String getUploadLink(String repoID, boolean update, String dir) throws SeafException {
try {
String apiPath;
if (update) {
apiPath = "api2/repos/" + repoID + "/update-link/";
} else {
apiPath = "api2/repos/" + repoID + "/upload-link/?p=" + Utils.toURLEncoded(dir);
}
HttpRequest req;
req = prepareApiGetRequest(apiPath);
checkRequestResponseStatus(req, HttpURLConnection.HTTP_OK);
String result = new String(req.bytes(), "UTF-8");
// should return "\"http://gonggeng.org:8082/...\"" or "\"https://gonggeng.org:8082/...\"
if (result.startsWith("\"http")) {
// remove the starting and trailing quote
return result.substring(1, result.length() - 1);
} else
throw SeafException.unknownException;
} catch (SeafException e) {
Log.d(DEBUG_TAG, e.getCode() + e.getMessage());
throw e;
} catch (Exception e) {
String msg = e.getMessage();
if (msg != null)
Log.d(DEBUG_TAG, msg);
else
Log.d(DEBUG_TAG, "get upload link error", e);
throw SeafException.unknownException;
}
}
use of com.github.kevinsawicki.http.HttpRequest in project seadroid by haiwen.
the class SeafConnection method getFileFromLink.
private File getFileFromLink(String dlink, String path, String localPath, String oid, ProgressMonitor monitor) throws SeafException {
if (dlink == null)
return null;
File file = new File(localPath);
try {
int i = dlink.lastIndexOf('/');
String quoted = dlink.substring(0, i) + "/" + URLEncoder.encode(dlink.substring(i + 1), "UTF-8");
HttpRequest req = prepareApiFileGetRequest(quoted);
checkRequestResponseStatus(req, HttpURLConnection.HTTP_OK);
if (monitor != null) {
Long size = -1L;
if (req.contentLength() > 0) {
size = Long.valueOf(req.contentLength());
} else {
// The req.contentLength() returns an int, which has a max value of
// 2GB. So if a file size exceeds 2GB, request.contentLength() would
// return -1. In such case, we parse the content length from the raw
// header string directly.
//
// See https://github.com/kevinsawicki/http-request/blob/http-request-5.6/lib/src/main/java/com/github/kevinsawicki/http/HttpRequest.java#L2519-L2521
String contentLengthheader = req.header(HttpRequest.HEADER_CONTENT_LENGTH);
// response, e.g. when the server is using chunked transfer encoding.
if (contentLengthheader != null) {
size = Long.parseLong(contentLengthheader);
}
}
if (size > 0) {
monitor.onProgressNotify(size, false);
}
}
File tmp = DataManager.createTempFile();
// Log.d(DEBUG_TAG, "write to " + tmp.getAbsolutePath());
if (monitor == null) {
req.receive(tmp);
} else {
req.bufferSize(MonitoredFileOutputStream.BUFFER_SIZE);
req.receive(new MonitoredFileOutputStream(tmp, monitor));
}
if (!tmp.renameTo(file)) {
Log.w(DEBUG_TAG, "Rename file error");
return null;
}
return file;
} catch (SeafException e) {
throw e;
} catch (UnsupportedEncodingException e) {
throw SeafException.encodingException;
} catch (IOException e) {
e.printStackTrace();
throw SeafException.networkException;
} catch (HttpRequestException e) {
if (e.getCause() instanceof MonitorCancelledException) {
// Log.d(DEBUG_TAG, "download is cancelled");
throw SeafException.userCancelledException;
} else {
throw getSeafExceptionFromHttpRequestException(e);
}
}
}
use of com.github.kevinsawicki.http.HttpRequest in project seadroid by haiwen.
the class SeafConnection method getShareLink.
public String getShareLink(String repoID, String path) throws SeafException {
try {
Map<String, Object> params = Maps.newHashMap();
params.put("repo_id", repoID);
params.put("path", path);
HttpRequest req = prepareApiGetRequest("api/v2.1/share-links", params);
checkRequestResponseStatus(req, HttpURLConnection.HTTP_OK);
String result = new String(req.bytes(), "UTF-8");
return result;
} catch (SeafException e) {
throw e;
} catch (HttpRequestException e) {
throw getSeafExceptionFromHttpRequestException(e);
} catch (IOException e) {
throw SeafException.networkException;
}
}
Aggregations