Skip to content

Commit

Permalink
Merge pull request #739 from haiwen/fix_block_open_fail
Browse files Browse the repository at this point in the history
Fix block open fail
  • Loading branch information
freeplant authored Jun 9, 2018
2 parents 3a58116 + 561d9f2 commit ab9d751
Show file tree
Hide file tree
Showing 22 changed files with 258 additions and 202 deletions.
38 changes: 19 additions & 19 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -91,25 +91,25 @@ android {
}

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}"
compile "com.android.support:design:${rootProject.ext.supportLibVersion}"
compile 'com.github.JakeWharton:ViewPagerIndicator:2.4.1'
compile 'com.github.kevinsawicki:http-request:6.0'
compile 'commons-io:commons-io:2.4'
compile 'com.google.guava:guava:18.0'
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.3'
compile 'com.cocosw:bottomsheet:1.3.1'
compile 'us.feras.mdv:markdownview:1.1.0'
compile 'com.commit451:PhotoView:1.2.4'
compile 'com.joanzapata.iconify:android-iconify-material-community:2.2.1'
testCompile 'junit:junit:4.12'
testCompile 'org.robolectric:robolectric:3.0'
compile 'com.madgag.spongycastle:core:1.54.0.0'
compile 'com.madgag.spongycastle:prov:1.54.0.0'
compile 'com.shuyu:gsyVideoPlayer-java:2.1.0'
compile 'com.shuyu:gsyVideoPlayer-ex_so:2.1.0'
compile 'com.squareup.okhttp3:okhttp:3.9.1'
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}"
implementation "com.android.support:design:${rootProject.ext.supportLibVersion}"
implementation 'com.github.JakeWharton:ViewPagerIndicator:2.4.1'
implementation 'com.github.kevinsawicki:http-request:6.0'
implementation 'commons-io:commons-io:2.4'
implementation 'com.google.guava:guava:18.0'
implementation 'com.nostra13.universalimageloader:universal-image-loader:1.9.3'
implementation 'com.cocosw:bottomsheet:1.3.1'
implementation 'us.feras.mdv:markdownview:1.1.0'
implementation 'com.commit451:PhotoView:1.2.4'
implementation 'com.joanzapata.iconify:android-iconify-material-community:2.2.1'
testImplementation 'junit:junit:4.12'
testImplementation 'org.robolectric:robolectric:3.0'
implementation 'com.madgag.spongycastle:core:1.54.0.0'
implementation 'com.madgag.spongycastle:prov:1.54.0.0'
implementation 'com.shuyu:gsyVideoPlayer-java:2.1.0'
implementation 'com.shuyu:gsyVideoPlayer-ex_so:2.1.0'
implementation 'com.squareup.okhttp3:okhttp:3.9.1'
}

}
Expand Down
152 changes: 110 additions & 42 deletions app/src/main/java/com/seafile/seadroid2/SeafConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
import com.google.common.collect.Maps;
import com.seafile.seadroid2.account.Account;
import com.seafile.seadroid2.data.Block;
import com.seafile.seadroid2.data.BlockInfoBean;
import com.seafile.seadroid2.data.DataManager;
import com.seafile.seadroid2.data.FileBlocks;
import com.seafile.seadroid2.data.ProgressMonitor;
import com.seafile.seadroid2.httputils.RequestManager;
import com.seafile.seadroid2.ssl.SSLTrustManager;
import com.seafile.seadroid2.util.Utils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

Expand All @@ -33,6 +35,7 @@
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URLEncoder;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -545,9 +548,22 @@ public Pair<String, File> getBlock(String repoID,
}

public String uploadByBlocks(String repoID, String dir, String filePath, List<Block> blocks, boolean update, ProgressMonitor monitor) throws IOException, SeafException {
String url = getUploadLink(repoID, update, true);
Log.d(DEBUG_TAG, "UploadLink " + url);
return uploadBlocksCommon(url, repoID, dir, filePath, blocks, monitor, update);

try {
LinkedList<String> blkListId = new LinkedList<>();
for (Block block : blocks) {
blkListId.addLast(block.getBlockId());
}
String json = getBlockUploadLink(repoID, blkListId);
BlockInfoBean infoBean = BlockInfoBean.fromJson(json);
if (infoBean.blkIds.size() > 0) {
uploadBlocksCommon(infoBean.rawblksurl, infoBean.blkIds, dir, filePath, blocks, monitor, update);
}
commitUpload(infoBean.commiturl, blkListId, dir, filePath, update);
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}

private File getFileFromLink(String dlink, String path, String localPath,
Expand Down Expand Up @@ -752,29 +768,15 @@ public boolean setPassword(String repoID, String passwd) throws SeafException {
}

private String getUploadLink(String repoID, boolean update) throws SeafException {
return getUploadLink(repoID, update, false);
}

private String getUploadLink(String repoID, boolean update, boolean byblock) throws SeafException {
try {
String apiPath;
if (update) {
if (byblock) {
apiPath = "api2/repos/" + repoID + "/upload-";
} else {
apiPath = "api2/repos/" + repoID + "/update-";
}
apiPath = "api2/repos/" + repoID + "/update-link/";
} else {
apiPath = "api2/repos/" + repoID + "/upload-";
apiPath = "api2/repos/" + repoID + "/upload-link/";
}

if (byblock) {
apiPath = apiPath + "blks-link/";
} else {
apiPath = apiPath + "link/";
}

HttpRequest req = prepareApiGetRequest(apiPath);
HttpRequest req;
req = prepareApiGetRequest(apiPath);
checkRequestResponseStatus(req, HttpURLConnection.HTTP_OK);

String result = new String(req.bytes(), "UTF-8");
Expand All @@ -797,6 +799,64 @@ private String getUploadLink(String repoID, boolean update, boolean byblock) thr
}
}


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;
}
}


/**
* commit blocks to server
*/
private String commitUpload(String link, List<String> blkIds, String dir, String filePath, boolean update)
throws SeafException, IOException {
File file = new File(filePath);
if (!file.exists()) {
throw new SeafException(SeafException.OTHER_EXCEPTION, "File not exists");
}

MultipartBody.Builder builder = new MultipartBody.Builder();
//set type
builder.setType(MultipartBody.FORM);
//set header ,to replace file
if (update) {
builder.addFormDataPart("replace", "1");
} else {
builder.addFormDataPart("replace", "0");
}
builder.addFormDataPart("parent_dir", dir);
builder.addFormDataPart("file_size", file.length() + "");
builder.addFormDataPart("file_name", file.getName());
JSONArray jsonArray = new JSONArray(blkIds);
builder.addFormDataPart("blockids", jsonArray.toString());
RequestBody body = builder.build();
Request request = new Request.Builder().url(link).post(body).build();
Response response = RequestManager.getInstance().getClient().newCall(request).execute();
if (response.isSuccessful()) {
return response.body().string();
}
throw new SeafException(SeafException.OTHER_EXCEPTION, "File upload failed");
}


/**
* Upload or update a file
*
Expand Down Expand Up @@ -829,12 +889,9 @@ private String uploadFileCommon(String link, String repoID, String dir,
//set type
builder.setType(MultipartBody.FORM);
// "target_file" "parent_dir" must be "/" end off
if (!TextUtils.equals("/", dir)) {
dir = dir + "/";
}

if (update) {
builder.addFormDataPart("target_file", dir);
String targetFilePath = Utils.pathJoin(dir, file.getName());
builder.addFormDataPart("target_file", targetFilePath);
} else {
builder.addFormDataPart("parent_dir", dir);
}
Expand All @@ -844,15 +901,22 @@ private String uploadFileCommon(String link, String repoID, String dir,
RequestBody body = builder.build();
//create Request
final Request request = new Request.Builder().url(link).post(body).header("Authorization", "Token " + account.token).build();
Response execute = RequestManager.getInstance().getClient().newCall(request).execute();
return execute.body().string();
Response response = RequestManager.getInstance().getClient().newCall(request).execute();
if (response.isSuccessful()) {
String str = response.body().string();
if (!TextUtils.isEmpty(str)) {
return str.replace("\"", "");
}
}
throw new SeafException(SeafException.OTHER_EXCEPTION, "File upload failed");
}

/**
* Upload file blocks to server
*/
private String uploadBlocksCommon(String link, String repoID, String dir, String filePath, List<Block> blocks, ProgressMonitor
monitor, boolean update)
private String uploadBlocksCommon(String link, List<String> needUploadId, String dir, String filePath, List<Block> blocks,
ProgressMonitor
monitor, boolean update)
throws SeafException, IOException {
File file = new File(filePath);
if (!file.exists()) {
Expand All @@ -861,22 +925,26 @@ private String uploadBlocksCommon(String link, String repoID, String dir, String
MultipartBody.Builder builder = new MultipartBody.Builder();
//set type
builder.setType(MultipartBody.FORM);
//set header ,to replace file
if (update) {
builder.addFormDataPart("replace", "1");
}
builder.addFormDataPart("parent_dir", dir);
builder.addFormDataPart("file_name", file.getName());
builder.addFormDataPart("file_size", file.length() + "");
for (Block block : blocks) {
File blk = new File(block.path);
builder.addFormDataPart("file", blk.getName(), RequestManager.getInstance().createProgressRequestBody(monitor, blk));
builder.addFormDataPart("filename", file.getName());

for (int i = 0; i < blocks.size(); i++) {
Block block = blocks.get(i);
for (String s : needUploadId) {
if (s.equals(block.getBlockId())) {
File blk = new File(block.path);
builder.addFormDataPart("file", blk.getName(), RequestManager.getInstance().createProgressRequestBody(monitor, blk));
break;
}
}
}

RequestBody body = builder.build();
final Request request = new Request.Builder().url(link).post(body).header("Authorization", "Token " + account.token).build();
Response execute = RequestManager.getInstance().getClient().newCall(request).execute();
return execute.body().string();
Response response = RequestManager.getInstance().getClient().newCall(request).execute();
if (response.isSuccessful()) {
return response.body().string();
}
throw new SeafException(SeafException.OTHER_EXCEPTION, "File upload failed");
}

public void createNewRepo(String repoName, String description, String password) throws SeafException {
Expand Down
33 changes: 33 additions & 0 deletions app/src/main/java/com/seafile/seadroid2/data/BlockInfoBean.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.seafile.seadroid2.data;

import com.seafile.seadroid2.util.Utils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;


public class BlockInfoBean {
public List<String> blkIds;
public String rawblksurl;
public String commiturl;

public static BlockInfoBean fromJson(String json) throws JSONException {
BlockInfoBean bean = new BlockInfoBean();
JSONObject obj = Utils.parseJsonObject(json);
bean.rawblksurl = obj.optString("rawblksurl");
bean.commiturl = obj.optString("commiturl");
JSONArray jsonArray = obj.optJSONArray("blklist");
bean.blkIds = new ArrayList<>();
for (int i = 0; i < jsonArray.length(); i++) {
bean.blkIds.add(jsonArray.getString(i));
}
return bean;
}

public BlockInfoBean() {
}
}
10 changes: 5 additions & 5 deletions app/src/main/java/com/seafile/seadroid2/data/DataManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ public synchronized File getFile(String repoName, String repoID, String path,
}
}

public synchronized File getFileByBlocks(String repoName, String repoID, String path, int version, long fileSize,
public synchronized File getFileByBlocks(String repoName, String repoID, String path, long fileSize,
ProgressMonitor monitor) throws SeafException, IOException, JSONException, NoSuchAlgorithmException {

String cachedFileID = null;
Expand Down Expand Up @@ -992,7 +992,7 @@ public String getRepoPassword(String repoID) {
return null;
}

final SeafRepo seafRepo = getCachedRepoByID(repoID);
final SeafRepoEncrypt seafRepo = getCachedRepoEncryptByID(repoID);
if (seafRepo != null && seafRepo.canLocalDecrypt()) {
final Pair<String, String> pair = dbHelper.getEnckey(repoID);
if (pair == null)
Expand Down Expand Up @@ -1192,12 +1192,12 @@ private FileBlocks chunkFile(String encKey, String enkIv, String filePath) throw

public void uploadByBlocks(String repoName, String repoId, String dir,
String filePath, ProgressMonitor monitor,
boolean isUpdate, boolean isCopyToLocal, int version) throws NoSuchAlgorithmException, IOException, SeafException {
uploadByBlocksCommon(repoName, repoId, dir, filePath, monitor, isUpdate, isCopyToLocal, version);
boolean isUpdate, boolean isCopyToLocal) throws NoSuchAlgorithmException, IOException, SeafException {
uploadByBlocksCommon(repoName, repoId, dir, filePath, monitor, isUpdate, isCopyToLocal);
}

private void uploadByBlocksCommon(String repoName, String repoID, String dir, String filePath,
ProgressMonitor monitor, boolean isUpdate, boolean isCopyToLocal, int version) throws NoSuchAlgorithmException, IOException, SeafException {
ProgressMonitor monitor, boolean isUpdate, boolean isCopyToLocal) throws NoSuchAlgorithmException, IOException, SeafException {


final Pair<String, String> pair = getRepoEncKey(repoID);
Expand Down
2 changes: 0 additions & 2 deletions app/src/main/java/com/seafile/seadroid2/data/SeafRepo.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public class SeafRepo implements SeafItem {
public String permission;
public String magic;
public String encKey;
public int encVersion;
public long size;
public String root; // the id of root directory

Expand All @@ -47,7 +46,6 @@ static SeafRepo fromJson(JSONObject obj) throws JSONException{
repo.isSharedRepo = obj.getString("type").equals("srepo");
repo.magic = obj.optString("magic");
repo.encKey = obj.optString("random_key");
repo.encVersion = obj.optInt("enc_version");
return repo;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ public class SeafRepoEncrypt {
public int encVersion;
public long size;
public String root; // the id of root directory
private String modifierContactEmail;
private String modifierName;
private String modifierEmail;
private int fileCount;

static SeafRepoEncrypt fromJson(JSONObject obj) throws JSONException {
SeafRepoEncrypt repo = new SeafRepoEncrypt();
Expand All @@ -36,14 +32,10 @@ static SeafRepoEncrypt fromJson(JSONObject obj) throws JSONException {
repo.encVersion = obj.optInt("enc_version");
repo.mtime = obj.getLong("mtime");
repo.owner = obj.getString("owner");
repo.modifierContactEmail = obj.getString("modifier_contact_email");
repo.id = obj.getString("id");
repo.modifierName = obj.getString("modifier_name");
repo.size = obj.getLong("size");
repo.modifierEmail = obj.getString("modifier_email");
repo.name = obj.getString("name");
repo.root = obj.getString("root");
repo.fileCount = obj.optInt("file_count");
repo.encKey = obj.optString("random_key");
repo.isGroupRepo = obj.getString("type").equals("grepo");
repo.isPersonalRepo = obj.getString("type").equals("repo");
Expand Down
Loading

0 comments on commit ab9d751

Please sign in to comment.