7 Replies Latest reply on Sep 15, 2017 9:51 AM by sinan.oezdemir

    How to add an attachment to an existing Document? (using JS API)

    Robert Hanson

      I have a Jive app and have a need to manipulate attachments on a Document using the JS API.  I can create a new document with an attachment, but I can't figure out how to add a new attachment.

       

      Any help would be appreciated.


      Thanks.

        • Re: How to add or delete attachment from existing Document?
          Robert Hanson

          I have made some progress, but still having an issue.  If I use the JS API to get an existing document and add an attachment it throws an error about an existing attachment.

           

          // fetch existing known Document
          osapi.jive.corev3.contents.get({'entityDescriptor':'102,1001'})
          .execute(function(res){
          
              // get the Document object from the results
              var doc = res.list[0];
              if (doc) {
          
                  // add a new attachment to the Document
                  doc.attachments.push({
                      doUpload: true,
                      url: "http://worldonline.media.clients.ellingtoncms.com/img/photos/2012/10/04/zombie_beer_t640.jpg",
                      contentType: "image/jpeg"
                  });
          
                  // update the document ... THIS FAILS      
                  doc.update()
                  .execute(function(res){
                      console.log(JSON.stringify(res, null, 4));
                  });
              }
          
          });
          
          
          

           

          Here is the response from the update().

          {
              "status": 400,
              "error": {
                  "message": "There was an error fetching file http://localhost:8080/api/core/v3/attachments/1122/data. HTTP error code: 401",
                  "code": "attachmentFetchFailed"
              }
          }
          
          
          

           

          The error is on the attachment /api/core/v3/attachments/1122/data, but this is an existing attachment and when inspecting the doc object, doUpload for this attachment is set to false.

           

          If I instead replace the attachments array instead of adding to it, Jive won't throw an error but it will remove all of the old attachments before it adds the new one.

           

          Any ideas on how to to get update() to work so that it adds the new attachment while leaving the other ones in tact?

            • Re: Re: How to add or delete attachment from existing Document?
              Robert Hanson

              Ryan Rutan, this smells like a bug to me.

               

              Per, Jive JavaScript API v3.7 → osapi.jive.corev3.contents.Attachment entity

              doUpload Boolean required

              An indicator of whether an attachment is new and should be uploaded.

               

              This flag doesn't appear to work as documented.  Even when the flag is set to false it treats it as if it were true and attempts to upload the file into Jive.  The problem is that since this file already exists in Jive it ends up getting HTTP 401 errors when trying to fetch it.  I assume that this is because Jive is trying to fetch it using no authentication and the URL isn't publicly available.

               

              To reproduce:

              Use the example I posted in the previous reply and change the entityDescriptor in the first lines to a document in your Jive instance.  The first time you run the script it will attach the image to the document as long as there are no other attachments on the document.  Run it a second time to add the image as a second attachment and it will throw the error.

               

              If you have any ideas on a workaround it would be helpful.

                • Re: Re: Re: How to add or delete attachment from existing Document?
                  Robert Hanson

                  Ryan Rutan, the bug appears to be in class com.jivesoftware.api.core.v3.providers.content.AttachmentProvider.

                   

                  Here is a summary of the issue with comments added to highlight the issue:

                  public List<Attachment> validateAttachmentsFromJSON(JSONObject object) {
                      // ...
                  
                      // Loops through the attachments and validates the fields on each.
                      for (int n = 0; n < jsonAttachments.length(); n++) {
                          // ...
                  
                          // If any attachments have doUpload==true set anyNewAttachments=true
                          if (attachment.getBoolean("doUpload")) {
                              anyNewAttachments = true;
                          }
                      }
                  
                      // If no new attachments then return
                      if (!anyNewAttachments) {
                          return null;
                      }
                  
                      // ...
                  
                      // Loop through the attachmnents and upload each of them.
                      // BUG IS HERE: All attachments are uploaded, it doesn't check the doUpload flag.
                      for (int n = 0; n < jsonAttachments.length(); n++) {
                          // ...
                          SimpleDataSource attachmentSource = fetchFile(url);
                          // ...
                      }
                  
                      // ...
                  
                  
                      return attachments;
                  }
                  
                  
                  

                   

                  Once this method returns it will attempt to update the document and attachments, which ends up calling this method in class com.jivesoftware.api.core.v3.providers.AbstractObjectProvider.

                  protected List<Attachment> replaceAttachments(AttachmentContentResource content, List<Attachment> files)
                  {
                      if (files != null) {
                          deleteAttachments(content);
                          // ...
                      }
                      // ...
                  }
                  
                  

                   

                  To summarize the issues:

                   

                  1. If there are any new attachments, it attempt to upload them all.  The correct behavior should be to only upload files where doUpload is true.

                  2. When it replaces the attachments it deletes all existing ones and uses the new ones.  The correct behavior should be to add new attachments, remove attachments that have been removed, and ignore existing attachments where doUpload is false.

                   

                  The attachments are already being stored in an Attachment object, so the fix is likely to add a field to the Attachment class to indicate if the file is a new one (with the content set in the object), or an existing one (without the content set in the object).

                   

                  Anyway, if you can come up with a workaround for this it would be appreciated.

              • Re: How to add an attachment to an existing Document? (using JS API)
                Nils Drews

                As you have realized the problem is that 401 - not authorized here. The easiest way around this is to use Binary Data Storage Usage -> section 3.3 "Downloading a Binary Attachment". With the name of the attachment and the ref attribute set to the document itself you get a URL that can be passed back to the update functionality. As these are temporarily accessible now by this URL we prevent the issue with the 401. They get cleaned up after the specified amount of time again and we are good again.

                 

                That should do the trick here nicely, a colleague implemented it this way. I hope I described it properly so you can follow along and I did not miss anything.

                1 person found this helpful