Ejemplo n.º 1
0
        private bool UploadMultipartRevision(RevisionInternal revision)
        {
            MultipartEntity multiPart             = null;
            IDictionary <string, object> revProps = revision.GetProperties();

            revProps.Put("_revisions", db.GetRevisionHistoryDict(revision));
            // TODO: refactor this to
            IDictionary <string, object> attachments = (IDictionary <string, object>)revProps.Get
                                                           ("_attachments");

            foreach (string attachmentKey in attachments.Keys)
            {
                IDictionary <string, object> attachment = (IDictionary <string, object>)attachments
                                                          .Get(attachmentKey);
                if (attachment.ContainsKey("follows"))
                {
                    if (multiPart == null)
                    {
                        multiPart = new MultipartEntity();
                        try
                        {
                            string   json        = Manager.GetObjectMapper().WriteValueAsString(revProps);
                            Encoding utf8charset = Sharpen.Extensions.GetEncoding("UTF-8");
                            multiPart.AddPart("param1", new StringBody(json, "application/json", utf8charset)
                                              );
                        }
                        catch (IOException e)
                        {
                            throw new ArgumentException(e);
                        }
                    }
                    BlobStore   blobStore    = this.db.GetAttachments();
                    string      base64Digest = (string)attachment.Get("digest");
                    BlobKey     blobKey      = new BlobKey(base64Digest);
                    InputStream inputStream  = blobStore.BlobStreamForKey(blobKey);
                    if (inputStream == null)
                    {
                        Log.W(Database.Tag, "Unable to find blob file for blobKey: " + blobKey + " - Skipping upload of multipart revision."
                              );
                        multiPart = null;
                    }
                    else
                    {
                        string contentType = null;
                        if (attachment.ContainsKey("content_type"))
                        {
                            contentType = (string)attachment.Get("content_type");
                        }
                        else
                        {
                            if (attachment.ContainsKey("content-type"))
                            {
                                string message = string.Format("Found attachment that uses content-type" + " field name instead of content_type (see couchbase-lite-android"
                                                               + " issue #80): " + attachment);
                                Log.W(Database.Tag, message);
                            }
                        }
                        multiPart.AddPart(attachmentKey, new InputStreamBody(inputStream, contentType, attachmentKey
                                                                             ));
                    }
                }
            }
            if (multiPart == null)
            {
                return(false);
            }
            string path = string.Format("/%s?new_edits=false", revision.GetDocId());

            // TODO: need to throttle these requests
            Log.D(Database.Tag, "Uploadeding multipart request.  Revision: " + revision);
            Log.D(Database.Tag, this + "|" + Sharpen.Thread.CurrentThread() + ": uploadMultipartRevision() calling asyncTaskStarted()"
                  );
            AsyncTaskStarted();
            SendAsyncMultipartRequest("PUT", path, multiPart, new _RemoteRequestCompletionBlock_411
                                          (this));
            // TODO:
            return(true);
        }
Ejemplo n.º 2
0
        private bool UploadMultipartRevision(RevisionInternal revision)
        {
            MultipartEntity multiPart             = null;
            IDictionary <string, object> revProps = revision.GetProperties();
            // TODO: refactor this to
            IDictionary <string, object> attachments = (IDictionary <string, object>)revProps.Get
                                                           ("_attachments");

            foreach (string attachmentKey in attachments.Keys)
            {
                IDictionary <string, object> attachment = (IDictionary <string, object>)attachments
                                                          .Get(attachmentKey);
                if (attachment.ContainsKey("follows"))
                {
                    if (multiPart == null)
                    {
                        multiPart = new MultipartEntity();
                        try
                        {
                            string   json        = Manager.GetObjectMapper().WriteValueAsString(revProps);
                            Encoding utf8charset = Sharpen.Extensions.GetEncoding("UTF-8");
                            multiPart.AddPart("param1", new StringBody(json, "application/json", utf8charset)
                                              );
                        }
                        catch (IOException e)
                        {
                            throw new ArgumentException(e);
                        }
                    }
                    BlobStore   blobStore    = this.db.GetAttachments();
                    string      base64Digest = (string)attachment.Get("digest");
                    BlobKey     blobKey      = new BlobKey(base64Digest);
                    InputStream inputStream  = blobStore.BlobStreamForKey(blobKey);
                    if (inputStream == null)
                    {
                        Log.W(Log.TagSync, "Unable to find blob file for blobKey: %s - Skipping upload of multipart revision."
                              , blobKey);
                        multiPart = null;
                    }
                    else
                    {
                        string contentType = null;
                        if (attachment.ContainsKey("content_type"))
                        {
                            contentType = (string)attachment.Get("content_type");
                        }
                        else
                        {
                            if (attachment.ContainsKey("content-type"))
                            {
                                Log.W(Log.TagSync, "Found attachment that uses content-type" + " field name instead of content_type (see couchbase-lite-android"
                                      + " issue #80): %s", attachment);
                            }
                        }
                        multiPart.AddPart(attachmentKey, new InputStreamBody(inputStream, contentType, attachmentKey
                                                                             ));
                    }
                }
            }
            if (multiPart == null)
            {
                return(false);
            }
            string path = string.Format("/%s?new_edits=false", revision.GetDocId());

            Log.D(Log.TagSync, "Uploading multipart request.  Revision: %s", revision);
            AddToChangesCount(1);
            Log.V(Log.TagSync, "%s | %s: uploadMultipartRevision() calling asyncTaskStarted()"
                  , this, Sharpen.Thread.CurrentThread());
            AsyncTaskStarted();
            SendAsyncMultipartRequest("PUT", path, multiPart, new _RemoteRequestCompletionBlock_542
                                          (this, revision));
            // Server doesn't like multipart, eh? Fall back to JSON.
            //status 415 = "bad_content_type"
            return(true);
        }