Platform Manual
Data Types
Attachment
with the attachment crdt you can associate very large amounts of binary data, such as an image, video, and so on, with a document and replicate across peers without conflict if you have very large amounts of binary data, such as a high resolution image, video, or some other file above 50 megapixels, or if you have a deeply embedded or a large document object, use an attachment instead of a regular document unlike documents, attachments store data outside of the ditto store running locally in the end user environment and must be explicitly fetched to replicate across distributed peers for more information about ditto documents, see the platform manual > docid\ uti2pcpnojh74whsurd9r syncing large documents can significantly impact sync performance caution is advised when handling very large binary data, a deeply embedded document, or a very large document carefully consider using attachment instead of storing the data directly within a document object ensuring uninterrupted fetching maintain a strong reference to attachmentfetcher for the entirety of the asynchronous fetch operation by following these guidelines preserve the attachmentfetcher as a globally accessible instance prevent the fetch operation from silently aborting creating and fetching attachments the following snippet demonstrates a use case for leveraging the attachment crdt, as well as the step by step process for creating and fetching the attachment if developing in swift, for a tutorial on how to work with an attachment in a chat app, see docid 1ovbhf0k7m0hxak hndbi the following snippet demonstrates the process for creating, associating, and fetching an attachment define a collection named 'foo' using base64 encoded image data and metadata, create an attachment object upsert a document with an attachment in the collection later, retrieve the document by id and fetch the attachment using an attachmentfetcher val testcontext = instrumentationregistry getinstrumentation() context val attachmentstream = testcontext assets open("attachment test png") val bitmapstream = testcontext assets open("attachment test png") val attachmentbitmap = bitmapfactory decodestream(bitmapstream) bitmapstream close() val metadata = mapof("name" to "my image png") val attachment = coll newattachment(attachmentstream, metadata) val docid = coll upsert(mapof("some" to "string", "my attachment" to attachment)) val doc = coll findbyid(docid) exec() val attachmenttoken = doc!!\["my attachment"] attachmenttoken val fetcher = coll fetchattachment(attachmenttoken!!) { when (it) { is completed > { val attbitmap bitmap = bitmapfactory decodestream(it attachment getinputstream()) } is progress > {} } }// define a collection named 'foo' in the ditto store const collection = ditto store collection('foo') // define an image in base64 format const myimagebase64 = 'ivborw0kggoaaaansuheugaaaqaaaaeaaqmaaabmvdolaaaaa1bmvew10nbjbbbqaaaah0leqvroge3baq0aaadcopdpbq43oaaaaaaaaaaavg0haaabmmdh1qaaaabjru5erkjggg==' // convert the base64 data to a uint8array const myimagebytes = uint8array from(myimagebase64, (character) => character charcodeat(0)) // define metadata const metadata = { name 'image png' } // create an attachment object using the image data and metadata // on node, you can also pass a file path (string) instead of image data // and the attachment will be created from that file const attachment = await collection newattachment(myimagebytes, metadata) const docid = await collection upsert({ some 'string', my attachment attachment }) // later, find the document by id and then fetch the attachment const doc = await collection findbyid(docid) const attachmenttoken = doc at('my attachment') attachmenttoken // fetch the attachment const attachmentfetcher = collection fetchattachment(attachmenttoken, async (attachmentfetchevent) => { switch (attachmentfetchevent type) { case 'completed' const fetchedattachment = attachmentfetchevent attachment // do something with attachment break default console log('unable to fetch attachment') break } }) // there is also a more convenient way of fetching the attachment // (attachmentfetcher implements the `promiselike` protocol) const fetchedattachment = await collection fetchattachment(attachmenttoken) // do something with attachmentstring attachmentpath = tempfile getpath(); map\<string, string> metadata = new hashmap<>(); metadata put("name", "my image png"); dittoattachment attachment = coll newattachment(attachmentpath, metadata); tempfile delete(); map\<string, object> content = new hashmap<>(); content put("some", "string"); content put("my attachment", attachment); dittodocumentid docid = coll upsert(content); dittodocument doc = coll findbyid(docid) exec(); dittoattachmenttoken attachmenttoken = doc get("my attachment") getattachmenttoken(); dittoattachmentfetcher fetcher = coll fetchattachment(attachmenttoken, event > { if (event gettype() == dittoattachmentfetcheventtype completed) { dittoattachment att = event ascompleted() getattachment(); bufferedimage attachmentimage; try { attachmentimage = imageio read(att getinputstream()); } catch (ioexception e) { e printstacktrace(); } } else if (event gettype() == dittoattachmentfetcheventtype progress) { // do nothing wait for `completed` or `deleted` } else { } });string attachmentimagepath = path combine(directory getcurrentdirectory(), "attachment test png"); var originalbytes = file readallbytes(attachmentimagepath); var metadata = new dictionary\<string, string> { { "name", "my image png" } }; var attachment = coll newattachment(attachmentimagepath, metadata); var docid = coll upsert(new dictionary\<string, object> { { "some", "string" }, { "my attachment", attachment } }); var doc = coll findbyid(docid) exec(); var attachmenttoken = doc\["my attachment"] attachmenttoken; using var fetcher = coll fetchattachment(attachmenttoken, ev => { switch (ev) { case dittoattachmentfetchevent completed e // do something with attachment break; default console writeline("unable to fetch attachment"); break; } });string attachmentimagepath = path combine(directory getcurrentdirectory(), "attachment test png"); var originalbytes = file readallbytes(attachmentimagepath); var metadata = new dictionary\<string, string> { { "name", "my image png" } }; var attachment = coll newattachment(attachmentimagepath, metadata); var docid = coll upsert(new dictionary\<string, object> { { "some", "string" }, { "my attachment", attachment } }); var doc = coll findbyid(docid) exec(); var attachmenttoken = doc\["my attachment"] attachmenttoken; using var fetcher = coll fetchattachment(attachmenttoken, ev => { switch (ev) { case dittoattachmentfetchevent completed e // do something with attachment break; default console writeline("unable to fetch attachment"); break; } });// to implement