[ExtendScript / ID Server] Open a binary (e.g. image) file => parse unicode chars into byte array
Hello!
I am trying return byte arrays or binary buffers from Indesign Server so that I can hit the server requesting e.g. a certain JPG, and have that JPG returned in a buffer without any intermediary service.
So far, I have found that after exporting a file object, I can read it back into the server using e.g:
var file = new File('C:/Users/.../Desktop/file_example_JPG_100kB.jpg')
file.encoding = "Binary"
file.open("r");
var arr = [];
for (var i = 0; i < file.length; i+=4) {
file.seek(i, 0);
arr.push(file.read(4))
}
This will produce an array of unicode characters.
I am having trouble, however, turning those unicode characters into a bytestring which I can save out to a buffer, and eventually to a valid JPG on the other side.
I believe the issue has to do with this note on how the binary reader works
cc Jongware / https://estk.aenhancers.com/3%20-%20File%20System%20Access/file-object.html
"A special encoder, BINARY, is used to read binary files. It stores each byte of the file as one Unicode character regardless of any encoding. When writing, the lower byte of each Unicode character is treated as a single byte to write."
Has anyone been able to implement the removal of all but the lowest byte from each Unicode char that the Extendscript File reader reads in?
