Convert inline images to database

I have a little problem. Database documents contain richtextfield. The richtextfield contains a profile picture of a specific contact. The problem is that this content is not saved as mime, and therefore I cannot calculate the image URL.

I use pojo to retrieve data from a person profile and use this in my xpage control to display its contents. I need to create a conversion agent that takes richtextitem content and converts it to mime in order to be able to calculate a URL like

http://host/database.nsf/($users)/D40FE4181F2B86CCC12579AB0047BD22/Photo/M2?OpenElement 

Can someone help me with converting richtextitem content to mime? When I check inline objects in the rt field, they are not. When I get the contents of the field as a stream and save it in a new richtext field using the following code. But the new field is not created in any way.

 System.out.println("check if document contains a field with name "+fieldName); if(!doc.hasItem(fieldName)){ throw new PictureConvertException("Could not locate richtextitem with name"+fieldName); } RichTextItem pictureField = (RichTextItem) doc.getFirstItem(fieldName); System.out.println("Its a richtextfield.."); System.out.println("Copy field to backup field"); if(doc.hasItem("old_"+fieldName)){ doc.removeItem("old_"+fieldName); } pictureField.copyItemToDocument(doc, "old_"+fieldName); // Vector embeddedPictures = pictureField.getEmbeddedObjects(); // System.out.println(doc.hasEmbedded()); // System.out.println("Retrieved embedded objects"); // if(embeddedPictures.isEmpty()){ // throw new PictureConvertException("No embedded objects could be found."); // } // // EmbeddedObject photo = (EmbeddedObject) embeddedPictures.get(0); System.out.println("Create inputstream"); //s.setConvertMime(false); InputStream iStream = pictureField.getInputStream(); System.out.println("Create notesstream"); Stream nStream = s.createStream(); nStream.setContents(iStream); System.out.println("Create mime entity"); MIMEEntity mEntity = doc.createMIMEEntity("PictureTest"); MIMEHeader cdheader = mEntity.createHeader("Content-Disposition"); System.out.println("Set header withfilename picture.gif"); cdheader.setHeaderVal("attachment;filename=picture.gif"); System.out.println("Setcontent type header"); MIMEHeader cidheader = mEntity.createHeader("Content-ID"); cidheader.setHeaderVal("picture.gif"); System.out.println("Set content from stream"); mEntity.setContentFromBytes(nStream, "application/gif", mEntity.ENC_IDENTITY_BINARY); System.out.println("Save document.."); doc.save(); //s.setConvertMime(true); System.out.println("Done"); // Clean up if we are done.. //doc.removeItem(fieldName); 
+4
source share
3 answers

Now it was a bit, and I did not go the way of converting existing data to mime. I could not get it to work, and after several studies it seemed unnecessary. Since the problem is with displaying images bound to richtextbox, I did some research on how to calculate the URL for the image, and I came up with the following lines of code:

 function getImageURL(doc:NotesDocument, strRTItem,strFileType){ if(doc!=null && !"".equals(strRTItem)){ var rtItem = doc.getFirstItem(strRTItem); if(rtItem!=null){ var personelDB = doc.getParentDatabase(); var dbURL = getDBUrl(personelDB); var imageURL:java.lang.StringBuffer = new java.lang.StringBuffer(dbURL); if("file".equals(strFileType)){ var embeddedObjects:java.util.Vector = rtItem.getEmbeddedObjects(); if(!embeddedObjects.isEmpty()){ var file:NotesEmbeddedObject = embeddedObjects.get(0); imageURL.append("(lookupView)\\"); imageURL.append(doc.getUniversalID()); imageURL.append("\\$File\\"); imageURL.append(file.getName()); imageURL.append("?Open"); } }else{ imageURL.append(doc.getUniversalID()); imageURL.append("/"+strRTItem+"/"); if(rtItem instanceof lotus.domino.local.RichTextItem){ imageURL.append("0.C4?OpenElement"); }else{ imageURL.append("M2?OpenElement"); } } return imageURL.toString() } } } 

He will check for the presence of this RT field. If so, it takes a few things:

  • If there are any files in the rtfield file, the first file is the image to display
  • else it will create the specified url, if the element is of type Rt otherwhise, it will assume that it is a mime entity and will generate a different url.
+2
source

Not sure if this is the answer, but I cannot add comments yet. Have you confirmed that there is something in your thread?

if (stream.getBytes() != 0) {

0
source

The problem cannot be solved "perfectly" in Java.

1) if you convert to MIME, you will ruin the text of the original Notes. MIME allows only a rough approximation of the original content; it may or may not matter.

If that matters, you can convert a copy of the original field to MIME, used only for display purposes, or clear it with DXL and save it separately - however, this approach again means a synchronization problem every time someone changes the image in the original RT element.

2) the calculation of the URL in accordance with the OP code in the accepted self-answer is generally impossible, since the constant 0.C4 in this example refers to the image offset in the binary data of the RT element. Any other rich text field design makes sense, manually entered images created using another version of Notes - all this affects the offset.

3) The URL can only be correctly calculated using the C API, which allows you to examine binary data in an extended text element. This is not possible with Java. IMO (without creating JNI bridges, etc.)

0
source

Source: https://habr.com/ru/post/1400232/


All Articles