Ejemplo n.º 1
0
        /// <summary>
        /// Goes through an _attachments dictionary and replaces any values that are Attachment objects
        /// with proper JSON metadata dicts.
        /// </summary>
        /// <remarks>
        /// Goes through an _attachments dictionary and replaces any values that are Attachment objects
        /// with proper JSON metadata dicts. It registers the attachment bodies with the blob store and sets
        /// the metadata 'digest' and 'follows' properties accordingly.
        /// </remarks>
        internal static IDictionary <string, object> InstallAttachmentBodies(IDictionary <String, Object> attachments, Database database)
        {
            var updatedAttachments = new Dictionary <string, object>();

            foreach (string name in attachments.Keys)
            {
                object value;
                attachments.TryGetValue(name, out value);

                if (value is Attachment)
                {
                    var attachment      = (Attachment)value;
                    var metadataMutable = new AttachmentMetadataDictionary(attachment.Metadata);
                    var body            = attachment.Body;
                    if (body != null)
                    {
                        // Copy attachment body into the database's blob store:
                        var writer = BlobStoreWriterForBody(body, database);
                        metadataMutable[AttachmentMetadataDictionaryKeys.Length]  = (long)writer.GetLength();
                        metadataMutable[AttachmentMetadataDictionaryKeys.Digest]  = writer.SHA1DigestString();
                        metadataMutable[AttachmentMetadataDictionaryKeys.Follows] = true;
                        var errMsg = metadataMutable.Validate();
                        if (errMsg != null)
                        {
                            throw Misc.CreateExceptionAndLog(Log.To.Database, StatusCode.BadAttachment, Tag,
                                                             "Error installing attachment body ({0})", errMsg);
                        }

                        database.RememberAttachmentWriter(writer, writer.SHA1DigestString());
                    }

                    attachment.Dispose();
                    updatedAttachments[name] = metadataMutable;
                }
                else if (value is AttachmentInternal)
                {
                    throw Misc.CreateExceptionAndLog(Log.To.Database, StatusCode.BadParam, Tag,
                                                     "AttachmentInternal objects not expected here.  Could indicate a bug");
                }
                else
                {
                    if (value != null)
                    {
                        updatedAttachments[name] = value;
                    }
                }
            }

            return(updatedAttachments);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Goes through an _attachments dictionary and replaces any values that are Attachment objects
        /// with proper JSON metadata dicts.
        /// </summary>
        /// <remarks>
        /// Goes through an _attachments dictionary and replaces any values that are Attachment objects
        /// with proper JSON metadata dicts. It registers the attachment bodies with the blob store and sets
        /// the metadata 'digest' and 'follows' properties accordingly.
        /// </remarks>
        internal static IDictionary<string, object> InstallAttachmentBodies(IDictionary<String, Object> attachments, Database database)
        {
            var updatedAttachments = new Dictionary<string, object>();
            foreach (string name in attachments.Keys)
            {
                object value;
                attachments.TryGetValue(name, out value);

                if (value is Attachment)
                {
                    var attachment = (Attachment)value;
                    var metadataMutable = new AttachmentMetadataDictionary(attachment.Metadata);
                    var body = attachment.Body;
                    if (body != null)
                    {
                        // Copy attachment body into the database's blob store:
                        var writer = BlobStoreWriterForBody(body, database);
                        metadataMutable[AttachmentMetadataDictionaryKeys.Length] = (long)writer.GetLength();
                        metadataMutable[AttachmentMetadataDictionaryKeys.Digest] = writer.SHA1DigestString();
                        metadataMutable[AttachmentMetadataDictionaryKeys.Follows] = true;
                        var errMsg = metadataMutable.Validate();
                        if (errMsg != null) {
                            throw Misc.CreateExceptionAndLog(Log.To.Database, StatusCode.BadAttachment, Tag,
                                "Error installing attachment body ({0})", errMsg);
                        }

                        database.RememberAttachmentWriter(writer, writer.SHA1DigestString());
                    }

                    attachment.Dispose();
                    updatedAttachments[name] = metadataMutable;
                } else if (value is AttachmentInternal) {
                    throw Misc.CreateExceptionAndLog(Log.To.Database, StatusCode.BadParam, Tag,
                        "AttachmentInternal objects not expected here.  Could indicate a bug");
                } else {
                    if (value != null) {
                        updatedAttachments[name] = value;
                    }
                }
            }

            return updatedAttachments;
        }
        /// <summary>
        /// Goes through an _attachments dictionary and replaces any values that are Attachment objects
        /// with proper JSON metadata dicts.
        /// </summary>
        /// <remarks>
        /// Goes through an _attachments dictionary and replaces any values that are Attachment objects
        /// with proper JSON metadata dicts. It registers the attachment bodies with the blob store and sets
        /// the metadata 'digest' and 'follows' properties accordingly.
        /// </remarks>
        internal static IDictionary<string, object> InstallAttachmentBodies(IDictionary<String, Object> attachments, Database database)
        {
            var updatedAttachments = new Dictionary<string, object>();
            foreach (string name in attachments.Keys)
            {
                object value;
                attachments.TryGetValue(name, out value);

                if (value is Attachment)
                {
                    var attachment = (Attachment)value;
                    var metadataMutable = new AttachmentMetadataDictionary(attachment.Metadata);
                    var body = attachment.Body;
                    if (body != null)
                    {
                        // Copy attachment body into the database's blob store:
                        var writer = BlobStoreWriterForBody(body, database);
                        metadataMutable[AttachmentMetadataDictionary.LENGTH] = (long)writer.GetLength();
                        metadataMutable[AttachmentMetadataDictionary.DIGEST] = writer.SHA1DigestString();
                        metadataMutable[AttachmentMetadataDictionary.FOLLOWS] = true;
                        var errMsg = metadataMutable.Validate();
                        if (errMsg != null) {
                            throw new CouchbaseLiteException("Error installing attachment body ({0})", errMsg) { Code = StatusCode.BadAttachment };
                        }

                        database.RememberAttachmentWriter(writer);
                    }

                    attachment.Dispose();
                    updatedAttachments[name] = metadataMutable;
                }
                else if (value is AttachmentInternal)
                {
                    throw new ArgumentException("AttachmentInternal objects not expected here.  Could indicate a bug");
                }
                else 
                {
                    if (value != null)
                        updatedAttachments[name] = value;
                }
            }
            return updatedAttachments;
        }