Skip to content

Commit 20e4b85

Browse files
Update UploadFileRemoteOperation.java
Signed-off-by: Alexander-Ger-Reich <[email protected]>
1 parent a16037f commit 20e4b85

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

library/src/main/java/com/owncloud/android/lib/resources/files/UploadFileRemoteOperation.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.owncloud.android.lib.common.operations.OperationCancelledException;
1919
import com.owncloud.android.lib.common.operations.RemoteOperation;
2020
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
21+
import com.owncloud.android.lib.common.utils.Log_OC;
2122

2223
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
2324
import org.apache.commons.httpclient.Header;
@@ -32,6 +33,15 @@
3233
import java.util.Set;
3334
import java.util.concurrent.atomic.AtomicBoolean;
3435

36+
import java.io.FileInputStream;
37+
import java.security.DigestInputStream;
38+
import java.security.MessageDigest;
39+
import java.security.NoSuchAlgorithmException;
40+
import java.math.BigInteger;
41+
42+
43+
44+
3545
/**
3646
* Remote operation performing the upload of a remote file to the ownCloud server.
3747
*
@@ -211,6 +221,24 @@ protected RemoteOperationResult<String> uploadFile(OwnCloudClient client) throws
211221
}
212222

213223
putMethod.setRequestEntity(entity);
224+
225+
try {
226+
MessageDigest md = MessageDigest.getInstance("SHA-256");
227+
228+
try (FileInputStream fis = new FileInputStream(f);
229+
DigestInputStream dis = new DigestInputStream(fis, md)) {
230+
byte[] buffer = new byte[8192];
231+
while (dis.read(buffer) != -1) {
232+
// digest is updated by reading
233+
}
234+
}
235+
236+
String Hash = String.format("%064x", new BigInteger(1, md.digest()));
237+
putMethod.addRequestHeader("X-Content-Hash", Hash);
238+
} catch (Exception e) {
239+
Log_OC.w(this, "Could not compute chunk hash");
240+
}
241+
214242
status = client.executeMethod(putMethod);
215243

216244
result = new RemoteOperationResult<>(isSuccess(status), putMethod);

0 commit comments

Comments
 (0)