|
36 | 36 |
|
37 | 37 | import androidx.annotation.VisibleForTesting;
|
38 | 38 |
|
| 39 | +import java.io.FileInputStream; |
| 40 | +import java.security.DigestInputStream; |
| 41 | +import java.security.MessageDigest; |
| 42 | +import java.nio.ByteBuffer; |
| 43 | +import java.math.BigInteger; |
39 | 44 |
|
40 | 45 | public class ChunkedFileUploadRemoteOperation extends UploadFileRemoteOperation {
|
41 | 46 |
|
@@ -216,6 +221,24 @@ protected RemoteOperationResult run(OwnCloudClient client) {
|
216 | 221 | moveMethod = new MoveMethod(originUri, destinationUri, true);
|
217 | 222 | moveMethod.addRequestHeader(OC_X_OC_MTIME_HEADER, String.valueOf(lastModificationTimestamp));
|
218 | 223 |
|
| 224 | + try { |
| 225 | + File file_hash = new File(localPath); |
| 226 | + MessageDigest md = MessageDigest.getInstance("SHA-256"); |
| 227 | + |
| 228 | + try (FileInputStream fis = new FileInputStream(file_hash); |
| 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 | + moveMethod.addRequestHeader("X-Content-Hash", Hash); |
| 238 | + } catch (Exception e) { |
| 239 | + Log_OC.w(TAG, "Could not compute chunk hash"); |
| 240 | + } |
| 241 | + |
219 | 242 | if (creationTimestamp != null && creationTimestamp > 0) {
|
220 | 243 | moveMethod.addRequestHeader(OC_X_OC_CTIME_HEADER, String.valueOf(creationTimestamp));
|
221 | 244 | }
|
@@ -291,6 +314,22 @@ private RemoteOperationResult uploadChunk(OwnCloudClient client, Chunk chunk) th
|
291 | 314 | putMethod.addRequestHeader(E2E_TOKEN, token);
|
292 | 315 | }
|
293 | 316 |
|
| 317 | + try (RandomAccessFile hashRaf = new RandomAccessFile(file, "r")){ |
| 318 | + MessageDigest md = MessageDigest.getInstance("SHA-256"); |
| 319 | + |
| 320 | + FileChannel hashChannel = hashRaf.getChannel(); |
| 321 | + ByteBuffer buf = ByteBuffer.allocate((int)chunk.getLength()); |
| 322 | + hashChannel.position(chunk.getStart()); |
| 323 | + hashChannel.read(buf); |
| 324 | + md.update(buf.array()); |
| 325 | + |
| 326 | + String chunkHash = String.format("%064x", new BigInteger(1, md.digest())); |
| 327 | + |
| 328 | + putMethod.addRequestHeader("X-Content-Hash", chunkHash); |
| 329 | + } catch (Exception e) { |
| 330 | + Log_OC.w(TAG, "Could not compute chunk hash"); |
| 331 | + } |
| 332 | + |
294 | 333 | status = client.executeMethod(putMethod);
|
295 | 334 |
|
296 | 335 | result = new RemoteOperationResult(isSuccess(status), putMethod);
|
|
0 commit comments