CBLAttachment digest to blob file name

11 views
Skip to first unread message

Brendan Duddridge

unread,
Mar 31, 2016, 4:26:56 AM3/31/16
to Couchbase Mobile
Hi,

I'm writing an export routine which generates a big JSON file. I need to extract file attachments from the database now and store them into a folder along side the JSON file. Is there a way to get access to the following method?

+ (bool) digest: (NSString*)digest toBlobKey: (CBLBlobKey*)outKey;


I'm assuming that is will take the digest as provided by the metadata from the CBLAttachment and give me a filename that I can read from? I don't want to store the files with the original filename because I might have file attachments from different documents with the same attachment name, but are actually different files.

So I thought I would do something similar to what CBL already does with naming attachments. But since the data I have in the JSON file is whatever the CBLAttachment metadata is, I would like to figure out how to name the attachments correctly so that when I import the JSON file I'm able to determine from the digest what attachment file to read in.

Here's an example of what my attachments JSON looks like:

"_attachments": {
"icon": {
"stub": true,
"length": 6411,
"digest": "sha1-/wxjDAVSpOaf7qN7SWuqNZ4IM78=",
"revpos": 1,
"content_type": "image/png"
}
}

"icon" is just the name I give an icon that I associate with one of my model objects. But it could be a regular file name such as "hunger-games.jpg" or "terminator.png", etc.

Thanks,

Brendan

Brendan Duddridge

unread,
Mar 31, 2016, 4:30:40 AM3/31/16
to Couchbase Mobile
I do believe that the "CBLDatabase+Attachments.h" where the digest:toBlobKey method resides is private.

And so is CBLBlobKey I believe.

Jens Alfke

unread,
Mar 31, 2016, 10:31:47 AM3/31/16
to mobile-c...@googlegroups.com
I think what you want is CBLAttachment.contentURL. This returns a file: URL containing the path to the attachment file. The file will have a unique name based on a SHA-1 digest of the data, so you don’t need to worry about collisions.

—Jens

Brendan Duddridge

unread,
Mar 31, 2016, 1:38:07 PM3/31/16
to Couchbase Mobile
Won't that be nil for encrypted attachments though?

Jens Alfke

unread,
Mar 31, 2016, 2:01:38 PM3/31/16
to mobile-c...@googlegroups.com

On Mar 31, 2016, at 1:38 PM, Brendan Duddridge <bren...@gmail.com> wrote:

Won't that be nil for encrypted attachments though?

Yeah, you’re right. Oh well! In that case you can read the attachment and copy it into a new file. To ensure a unique name you can do what I do and base the name on a SHA-1 digest.

—Jens
Reply all
Reply to author
Forward
0 new messages