Skip to content

Commit a16037f

Browse files
Update ChunkedFileUploadRemoteOperation.java
Signed-off-by: Alexander-Ger-Reich <[email protected]>
1 parent 9e3c004 commit a16037f

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@
3636

3737
import androidx.annotation.VisibleForTesting;
3838

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

4045
public class ChunkedFileUploadRemoteOperation extends UploadFileRemoteOperation {
4146

@@ -216,6 +221,24 @@ protected RemoteOperationResult run(OwnCloudClient client) {
216221
moveMethod = new MoveMethod(originUri, destinationUri, true);
217222
moveMethod.addRequestHeader(OC_X_OC_MTIME_HEADER, String.valueOf(lastModificationTimestamp));
218223

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+
219242
if (creationTimestamp != null && creationTimestamp > 0) {
220243
moveMethod.addRequestHeader(OC_X_OC_CTIME_HEADER, String.valueOf(creationTimestamp));
221244
}
@@ -291,6 +314,22 @@ private RemoteOperationResult uploadChunk(OwnCloudClient client, Chunk chunk) th
291314
putMethod.addRequestHeader(E2E_TOKEN, token);
292315
}
293316

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+
294333
status = client.executeMethod(putMethod);
295334

296335
result = new RemoteOperationResult(isSuccess(status), putMethod);

0 commit comments

Comments
 (0)