Skip to content

Commit

Permalink
bug fix (issue 94): the previous version of AttachmentProxy assumed t…
Browse files Browse the repository at this point in the history
…hat the digest provided in the CBL attachment metadata could always be used to construct a BlobKey. In the issue's test case, the digest was an MD5 instead of a SHA1, so the BlobKey constructor rejected it. Fixed the problem by checking to see if the digest is a SHA1 first; if not, it uses the MessageDigest class to construct a SHA1 from the attachment contents
  • Loading branch information
pegli committed Feb 12, 2015
1 parent 1045aeb commit 4a82406
Showing 1 changed file with 50 additions and 5 deletions.
55 changes: 50 additions & 5 deletions mobile/android/src/com/obscure/titouchdb/AttachmentProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.KrollProxy;
Expand Down Expand Up @@ -35,19 +38,61 @@ public class AttachmentProxy extends KrollProxy {

private AbstractRevisionProxy revisionProxy;

public static BlobKey keyForBlobFromStream(InputStream in) {
MessageDigest md;
try {
md = MessageDigest.getInstance("SHA-1");
}
catch (NoSuchAlgorithmException e) {
Log.e(LCAT, "Error, SHA-1 digest is unavailable.");
return null;
}
byte[] sha1hash = new byte[40];

try {
byte[] buffer = new byte[65536];
int lenRead = in.read(buffer);
while (lenRead > 0) {
md.update(buffer, 0, lenRead);
lenRead = in.read(buffer);
}
}
catch (IOException e) {
Log.e(LCAT, "Error readin tmp file to compute key");
}

sha1hash = md.digest();
BlobKey result = new BlobKey(sha1hash);
return result;
}

public AttachmentProxy(AbstractRevisionProxy revisionProxy, Attachment attachment) {
assert revisionProxy != null;
assert attachment != null;

this.revisionProxy = revisionProxy;
this.attachment = attachment;

// if there is a digest in the attachment metadata, attempt to pre-load
// the blob.
blobStore = new BlobStore(attachment.getDocument().getDatabase().getAttachmentStorePath());
this.blobStore = new BlobStore(attachment.getDocument().getDatabase().getAttachmentStorePath());

// find the blob key for this attachment
BlobKey key = null;
String digest = (String) attachment.getMetadata().get("digest");
if (digest != null) {
blobFilePath = blobStore.pathForKey(new BlobKey(digest));
if (digest != null && digest.startsWith("sha1-")) {
key = new BlobKey(digest);
}
else {
try {
key = AttachmentProxy.keyForBlobFromStream(attachment.getContent());
}
catch (CouchbaseLiteException e) {
Log.e(LCAT, e.getMessage());
}
}

// attempt to preload the blob using the key
if (key != null) {
blobFilePath = blobStore.pathForKey(key);
if (blobFilePath != null) {
blob = TiBlob.blobFromFile(new TiFile(new File(blobFilePath), blobFilePath, false), attachment.getContentType());
}
Expand Down

0 comments on commit 4a82406

Please sign in to comment.