Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you access an attachment stored as MIME Part?

It seems to me there are two ways to store an attachment in a NotesDocument.

Either as a RichTextField or as a "MIME Part".

If they are stored as RichText you can do stuff like:

document.getAttachment(fileName)

That does not seem to work for an attachment stored as a MIME Part. See screenshot enter image description here

I have thousands of documents like this in the backend. This is NOT a UI issue where I need to use the file Download control of XPages.

Each document as only 1 attachment. An Image. A JPG file. I have 3 databases for different sizes. Original, Large, and Small. Originally I created everything from documents that had the attachment stored as RichText. But my code saved them as MIME Part. that's just what it did. Not really my intent.

What happened is I lost some of my "Small" pictures so I need to rebuild them from the Original pictures that are now stored as MIME Part. So my ultimate goal is to get it from the NotesDocument into a Java Buffered Image.

I think I have the code to do what I want but I just "simply" can't figure out how to get the attachment off the document and then into a Java Buffered Image.

Below is some rough code I'm working with. My goal is to pass in the document with the original picture. I already have the fileName because I stored that out in metaData. But I don't know how to get that from the document itself. And I'm passing in "Small" to create the Small image.

I think I just don't know how to work with attachments stored in this manner.

Any ideas/advice would be appreciated! Thanks!!!

public Document processImage(Document inputDoc, String fileName, String size) throws IOException {
    // fileName is the name of the attachment on the document
    // The goal is to return a NEW BLANK document with the image on it
    // The Calling code can then deal with keys and meta data.

// size is "Original", "Large" or "Small"
System.out.println("Processing Image,  Size = " + size);
//System.out.println("Filename = " + fileName);

boolean result = false;

Session session = Factory.getSession();
Database db = session.getCurrentDatabase();
session.setConvertMime(true);

BufferedImage img;
BufferedImage convertedImage = null;  // the output image

EmbeddedObject image = null;
InputStream imageStream = null;

int currentSize = 0;
int newWidth = 0;
String currentName = "";


try {
    // Get the Embedded Object
    image = inputDoc.getAttachment(fileName);
    System.out.println("Input Form : " + inputDoc.getItemValueString("form"));

    if (null == image) {
        System.out.println("ALERT - IMAGE IS NULL");
    }


    currentSize = image.getFileSize();
    currentName = image.getName();


    // Get a Stream of the Imahe
    imageStream = image.getInputStream();
    img = ImageIO.read(imageStream);  // this is the buffered image we'll work with
    imageStream.close();


    Document newDoc = db.createDocument();
    // Remember this is a BLANK document. The calling code needs to set the form


    if ("original".equalsIgnoreCase(size)) {
        this.attachImage(newDoc, img, fileName, "JPG");
        return newDoc;
    }

    if ("Large".equalsIgnoreCase(size)) {
        // Now we need to convert the LARGE image
        // We're assuming FIXED HEIGHT of 600px
        newWidth = this.getNewWidth(img.getHeight(), img.getWidth(), 600);
        convertedImage = this.getScaledInstance(img, newWidth, 600, false);
        this.attachImage(newDoc, img, fileName, "JPG");
        return newDoc;

    }

    if ("Small".equalsIgnoreCase(size)) {
        System.out.println("converting Small");
        newWidth = this.getNewWidth(img.getHeight(), img.getWidth(), 240);
        convertedImage = this.getScaledInstance(img, newWidth, 240, false);
        this.attachImage(newDoc, img, fileName, "JPG");
        System.out.println("End Converting Small");
        return newDoc;

    }

    return newDoc;




} catch (Exception e) {
    // HANDLE EXCEPTION HERE
    // SAMLPLE WRITE TO LOG.NSF
    System.out.println("****************");
    System.out.println("EXCEPTION IN processImage()");
    System.out.println("****************");
    System.out.println("picName: " + fileName);
    e.printStackTrace();
    return null;

} finally {
    if (null != imageStream) {
        imageStream.close();
    }
    if (null != image) {
        LibraryUtils.incinerate(image);
    }

}

}

like image 338
David Leedy Avatar asked Nov 21 '25 00:11

David Leedy


1 Answers

I believe it will be some variation of the following code snippet. You might have to change which mimeentity has the content so it might be in the parent or another child depending.

Stream stream = session.createStream();
doc.getMIMEEntity().getFirstChildEntity().getContentAsBytes(stream);
ByteArrayInputStream bais = new ByteArrayInputStream(stream.read());
return ImageIO.read(bais);

EDIT:

 session.setConvertMime(false);
  Stream stream = session.createStream();
  Item itm = doc.getFirstItem("ParentEntity");
  MIMEEntity me = itm.getMIMEEntity();
  MIMEEntity childEntity = me.getFirstChildEntity();
  childEntity.getContentAsBytes(stream);
  ByteArrayOutputStream bo = new ByteArrayOutputStream();
  stream.getContents(bo);
  byte[] mybytearray = bo.toByteArray();
  ByteArrayInputStream bais = new ByteArrayInputStream(mybytearray);
  return ImageIO.read(bais);
like image 163
Toby Samples Avatar answered Nov 23 '25 12:11

Toby Samples



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!