Search code examples
file-uploadlotus-dominohcldomino-appdev-pack

HCL Domino AppDevPack - Problem with write attachments other than .txt


I use this code (suppose the connection to the database has already been made) :

const fs = require('fs');

let docUnid = "F09C0DB42276F208C1258513005722D1";
let sFilename = fileSamplePdf.pdf; // !!! Does not work for reading after attachment !!!
// let sFilename = fileSampleTxt.txt; !!! Work for reading after attachment !!!

let buffer = fs.readFileSync("/tmp/" + sFilename);
const writable = await database.bulkCreateAttachmentStream({});
writable.on('error', e => {
  console.error("Error on write File", e);
});
writable.on('response', response => {
  console.log("File " + sFilename + " saved to doc " + docUnid);
});
let offset = 0;
const writeRemaining = () => {
  if (error) {
    return;
  }
  let draining = true;
  while (offset < buffer.length && draining) {
    const remainingBytes = buffer.length - offset;
    let chunkSize = 16 * 1024;
    if (remainingBytes < chunkSize) {
      chunkSize = remainingBytes;
    }
    const chunk = new Uint8Array(
      buffer.slice(offset, offset + chunkSize),
    );
    draining = writable.write(chunk);
    //draining = writable.write(buffer.slice(offset, offset + chunkSize)); !!! Does not work, generate a GrpcError !!!
    offset += chunkSize;
  }
  if (offset < buffer.length) {
    writable.once('drain', writeRemaining);
  } else {
    writable.end();
  }
};
writable.file({
  unid: docUnid,
  fileName: sFilename,
});
writeRemaining();

It works perfectly whatever the size of the file (since version 1.0.3), and the file is attached to the document in the "$File" field (size file is correct) :

Field Name: $FILE
Data Type: Attached Object
Data Length: 57 bytes
Seq Num: 100
Dup Item ID: 0
Field Flags: ATTACH SIGN SEAL SUMMARY 

Object Type: File
Object ID: 00000266
Object Length: 486481
File Name: fileSamplePdf.pdf
Flags: 
Host: UNKNOWN
Compression Type: NONE 
Encoding Type: 
File Attributes: RW PUBLIC 
File Size: 486481
File Created: 15/04/2020 16:56:08
File Modified: 15/04/2020 16:56:08

But after the attachment, if the type of attached file is other than txt (ex: jpg, pdf, ...), it cannot then be read, even from the Notes client, this generates a read error.

I compared the properties of the $File field of one file of type txt and another and there are no differences other than the properties of the file.

I'm using a Windows system with IBM Domino (r) Server (64 Bit), Release 10.0.1FP3 and AppDevPack 1.0.3.

Thank you in advance for your help


Solution

  • Would you be able to share some code? And maybe upgrade to FP4 and use the latest release (1.0.4)?

    One issue you may have if you haven't upgraded your domino-db client correctly is the use of Buffers vs U8Int arrays.
    In 1.0.3, I put in support to use buffers to avoid the issue on write, but there's a second param on read to grab the buffer.