Attachment Lookup by Name

35 views
Skip to first unread message

Jeff Kingyens

unread,
Jan 28, 2015, 12:54:45 PM1/28/15
to mobile-c...@googlegroups.com
Hello,

When I create file attachments for couchbase documents I can give them a name. Is there a way I can use the metadata stored in the couchbase document to build a view/index to lookup the truth path of the attachment on disk from its given name alone? The metadata looks like something like this:

    "_attachments" : {
      "image-BF835EEF-0A86-47C1-B596-8A83389A5089.jpg" : {
        "stub" : true,
        "length" : 179287,
        "digest" : "sha1-aVPI3e+uHklPwaICkX28ePAsWqQ=",
        "revpos" : 2,
        "content_type" : "image\/jpeg"
      }
    },

I want to create a view that emits the keys of the _attachments hash as the query key, and the value being the path on disk. However, I don't see the disk path anywhere. I read somewhere that the disk filename is the digest of the content. However, I can't find the relationship between "digest" and the disk filename (in this case: 6953C8DDEFAE1E494FC1A202917DBC78F02C5AA4.blob). I tried taking the hex value of "aVPI3e+uHklPwaICkX28ePAsWqQ=" but this isn't the same as the filename.

The greater context here is that I'm building a file provider extension where the goal is to abstract the storage details from other apps. I'd like to expose the attachments stored in couchbase as a flat list of files based on their given name.

Thanks,
Jeff

Jens Alfke

unread,
Jan 28, 2015, 1:43:09 PM1/28/15
to mobile-c...@googlegroups.com

On Jan 28, 2015, at 9:54 AM, Jeff Kingyens <jeff.k...@gmail.com> wrote:

I want to create a view that emits the keys of the _attachments hash as the query key, and the value being the path on disk.

There isn't a way to do that, and accessing attachments directly as files is discouraged. There are future changes to Couchbase Lite that would either make it never work (i.e. if your database is encrypted), or just sometimes not work (i.e. if we optimize by storing small attachments inline in the database, or if we download attachments from the server on demand.)

The only reason there's any API to get the path to an attachment file is because there are APIs in iOS/OS X — like AVFoundation — that only take paths as input, not NSData or NSStreams, so giving out the path was the only way to make attachments useable with those APIs.

—Jens

Jeff Kingyens

unread,
Jan 28, 2015, 1:45:52 PM1/28/15
to mobile-c...@googlegroups.com
Fair enough, I want to work in the most forward-compatible way. Accessing the data directly on disk is not a requirement, but I do to look up the data bytes (NSData?) based on the name I gave the attachment. Is there a way I can do this?

Jens Alfke

unread,
Jan 28, 2015, 5:13:34 PM1/28/15
to mobile-c...@googlegroups.com

On Jan 28, 2015, at 10:45 AM, Jeff Kingyens <jeff.k...@gmail.com> wrote:

Fair enough, I want to work in the most forward-compatible way. Accessing the data directly on disk is not a requirement, but I do to look up the data bytes (NSData?) based on the name I gave the attachment. Is there a way I can do this?

Reply all
Reply to author
Forward
0 new messages