|
| 1 | +// |
| 2 | +// Blob.hh |
| 3 | +// |
| 4 | +// Copyright (c) 2019 Couchbase, Inc All rights reserved. |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | + |
| 19 | +#pragma once |
| 20 | +#include "cbl++/Document.hh" |
| 21 | +#include "cbl/CBLBlob.h" |
| 22 | +#include "fleece/Mutable.hh" |
| 23 | +#include <string> |
| 24 | + |
| 25 | +// VOLATILE API: Couchbase Lite C++ API is not finalized, and may change in |
| 26 | +// future releases. |
| 27 | + |
| 28 | +CBL_ASSUME_NONNULL_BEGIN |
| 29 | + |
| 30 | +namespace cbl { |
| 31 | + class BlobReadStream; |
| 32 | + class BlobWriteStream; |
| 33 | + |
| 34 | + /** A reference to a binary data blob associated with a document. |
| 35 | + A blob's persistent form is a special dictionary in the document properties. |
| 36 | + To work with a blob, you construct a Blob object with that dictionary. */ |
| 37 | + class Blob : protected RefCounted { |
| 38 | + public: |
| 39 | + /** Returns true if a dictionary in a document is a blob reference. |
| 40 | + @note This method tests whether the dictionary has a `@type` property, |
| 41 | + whose value is `"blob"`. */ |
| 42 | + static bool isBlob(fleece::Dict d) {return FLDict_IsBlob(d);} |
| 43 | + |
| 44 | + /** Creates a new blob, given its contents as a single block of data. |
| 45 | + @note The memory pointed to by `contents` is no longer needed after this call completes |
| 46 | + (it will have been written to the database.) |
| 47 | + @param contentType The MIME type (optional). |
| 48 | + @param contents The data's address and length. */ |
| 49 | + Blob(slice contentType, slice contents) { |
| 50 | + _ref = (CBLRefCounted*) CBLBlob_CreateWithData(contentType, contents); |
| 51 | + } |
| 52 | + |
| 53 | + /** Creates a new blob from the data written to a \ref CBLBlobWriteStream. |
| 54 | + @param contentType The MIME type (optional). |
| 55 | + @param writer The blob-writing stream the data was written to. */ |
| 56 | + inline Blob(slice contentType, BlobWriteStream& writer); |
| 57 | + |
| 58 | + /** Creates a Blob instance on an existing blob reference in a document or query result. |
| 59 | + @note If the dict argument is not actually a blob reference, this Blob object will be |
| 60 | + invalid; you can check that by calling its `valid` method or testing it with its |
| 61 | + `operator bool`. */ |
| 62 | + Blob(fleece::Dict d) |
| 63 | + :RefCounted((CBLRefCounted*) FLDict_GetBlob(d)) |
| 64 | + { } |
| 65 | + |
| 66 | + /** Returns the length in bytes of a blob's content (from its `length` property). */ |
| 67 | + uint64_t length() const {return CBLBlob_Length(ref());} |
| 68 | + |
| 69 | + /** Returns a blob's MIME type, if its metadata has a `content_type` property. */ |
| 70 | + std::string contentType() const {return asString(CBLBlob_ContentType(ref()));} |
| 71 | + |
| 72 | + /** Returns the cryptographic digest of a blob's content (from its `digest` property). */ |
| 73 | + std::string digest() const {return asString(CBLBlob_Digest(ref()));} |
| 74 | + |
| 75 | + /** Returns a blob's metadata. This includes the `digest`, `length`, `content_type`, |
| 76 | + and `@type` properties, as well as any custom ones that may have been added. */ |
| 77 | + fleece::Dict properties() const {return CBLBlob_Properties(ref());} |
| 78 | + |
| 79 | + // Allows Blob to be assigned to mutable Dict/Array item, e.g. `dict["foo"] = blob` |
| 80 | + operator fleece::Dict() const {return properties();} |
| 81 | + |
| 82 | + /** Reads the blob's content into memory and returns them. */ |
| 83 | + alloc_slice loadContent() { |
| 84 | + CBLError error; |
| 85 | + fleece::alloc_slice content = CBLBlob_Content(ref(), &error); |
| 86 | + check(content.buf, error); |
| 87 | + return content; |
| 88 | + } |
| 89 | + |
| 90 | + /** Opens a stream for reading a blob's content. */ |
| 91 | + inline BlobReadStream* openContentStream(); |
| 92 | + |
| 93 | + protected: |
| 94 | + Blob(CBLRefCounted* r) :RefCounted(r) { } |
| 95 | + |
| 96 | + CBL_REFCOUNTED_BOILERPLATE(Blob, RefCounted, CBLBlob) |
| 97 | + }; |
| 98 | + |
| 99 | + /** A stream for writing a new blob to the database. */ |
| 100 | + class BlobReadStream { |
| 101 | + public: |
| 102 | + /** Opens a stream for reading a blob's content. */ |
| 103 | + BlobReadStream(Blob *blob) { |
| 104 | + CBLError error; |
| 105 | + _stream = CBLBlob_OpenContentStream(blob->ref(), &error); |
| 106 | + if (!_stream) throw error; |
| 107 | + } |
| 108 | + |
| 109 | + ~BlobReadStream() { |
| 110 | + CBLBlobReader_Close(_stream); |
| 111 | + } |
| 112 | + |
| 113 | + /** Reads data from a blob. |
| 114 | + @param dst The address to copy the read data to. |
| 115 | + @param maxLength The maximum number of bytes to read. |
| 116 | + @return The actual number of bytes read; 0 if at EOF. */ |
| 117 | + size_t read(void *dst, size_t maxLength) { |
| 118 | + CBLError error; |
| 119 | + int bytesRead = CBLBlobReader_Read(_stream, dst, maxLength, &error); |
| 120 | + if (bytesRead < 0) |
| 121 | + throw error; |
| 122 | + return size_t(bytesRead); |
| 123 | + } |
| 124 | + |
| 125 | + private: |
| 126 | + CBLBlobReadStream* _cbl_nullable _stream {nullptr}; |
| 127 | + }; |
| 128 | + |
| 129 | + BlobReadStream* Blob::openContentStream() { |
| 130 | + return new BlobReadStream(this); |
| 131 | + } |
| 132 | + |
| 133 | + /** A stream for writing a new blob to the database. */ |
| 134 | + class BlobWriteStream { |
| 135 | + public: |
| 136 | + /** Create a stream to write a new blob to the database. */ |
| 137 | + BlobWriteStream(Database db) { |
| 138 | + CBLError error; |
| 139 | + _writer = CBLBlobWriter_Create(db.ref(), &error); |
| 140 | + if (!_writer) throw error; |
| 141 | + } |
| 142 | + |
| 143 | + ~BlobWriteStream() { |
| 144 | + CBLBlobWriter_Close(_writer); |
| 145 | + } |
| 146 | + |
| 147 | + /** Writes data to a new blob. |
| 148 | + @param data The data to write. */ |
| 149 | + void write(fleece::slice data) { |
| 150 | + write(data.buf, data.size); |
| 151 | + } |
| 152 | + |
| 153 | + /** Writes data to a new blob. |
| 154 | + @param src The address of the data to write. |
| 155 | + @param length The length of the data to write. */ |
| 156 | + void write(const void *src, size_t length) { |
| 157 | + CBLError error; |
| 158 | + if (!CBLBlobWriter_Write(_writer, src, length, &error)) |
| 159 | + throw error; |
| 160 | + } |
| 161 | + |
| 162 | + private: |
| 163 | + friend class Blob; |
| 164 | + CBLBlobWriteStream* _cbl_nullable _writer {nullptr}; |
| 165 | + }; |
| 166 | + |
| 167 | + inline Blob::Blob(slice contentType, BlobWriteStream& writer) { |
| 168 | + _ref = (CBLRefCounted*) CBLBlob_CreateWithStream(contentType, writer._writer); |
| 169 | + writer._writer = nullptr; |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +CBL_ASSUME_NONNULL_END |
0 commit comments