Render thumbnail in Angular4

时间:2017-06-15 10:30:08

标签: java node.js image angular openkm

I am trying to render an image which I got from a Java service as InputStream, re-send it through NodeJS Express server and finally render it in Angular4

Here's what I do:

Java Jersey service:

@GET
@Path("thumbnail")
@ApiOperation(
        value = "Gets document preview",
        notes = "Gets document preview"
)
@ApiResponses(value = {
        @ApiResponse(code = 200, message = "Preview of the document")
})
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces("image/png")
public Response getDocThumbnail(
        @ApiParam(value = "Entity UUID", required = true) @FormDataParam("uuid") String uuid
) throws RepositoryException, UnknowException, WebserviceException, PathNotFoundException, DatabaseException, AutomationException, AccessDeniedException, ConversionException, IOException {
    RawDocument rawDocument = docCtrl.getDocThumbnail(uuid);
    return Response
            .ok(rawDocument.getInputStream(), "image/png")
            .header("Content-Disposition", "attachment; filename=\" " + rawDocument.getName() + "\"")
            .build();
}

the controller looks like:

public RawDocument getDocThumbnail(String uuid) throws IOException, AccessDeniedException, PathNotFoundException, WebserviceException, RepositoryException, DatabaseException, ConversionException, AutomationException, UnknowException {
    return new RawDocument(
            okmWebSrv.getOkmService().getThumbnail(uuid, ThumbnailType.THUMBNAIL_LIGHTBOX),
            "whatever"
    );
}

Basically it's call to OpenKM SDK to retreive document's thumbnail

This Java endpoint is called from NodeJS Express 4.15 that is pre-processing some requests for this Java backend. Here's what I do:

...compose request options...    
const vedica_res = await rp(options);

let buffered = new Buffer(vedica_res, 'binary');
res.writeHead(200, {
    'Content-Type': 'image/png',
    'Content-disposition': 'attachment;filename=' + 'thumb.png',
    'Content-Length': buffered.length
});

return res.end(buffered, 'binary');

Finally with Angular4 being the initiator of this roundtrip I am trying to render the image like so:

this.rest.send('http://localhost:4200/vedica/api/document/thumbnail', RequestMethod.Get,
        {uuid: '19516ea1-657e-4b21-8564-0cb87f29b064'}, true).subscribe(img => {
        // this.preview = img
        var urlCreator = window.URL;
        var url = urlCreator.createObjectURL(img);
        this.thumb.nativeElement.src = url;
    })

The 'img' received is a Blob {size: 81515, type: "image/png"}. Console shows no errors but renders no image in the <img #thumb/> tag. But I can see that it sets the src=blob:http%3A//localhost%3A3000/4cf847d5-5af3-4c5a-acbc-0201e60efdb7 for it. Image just has a broken image icon. When I try to read a cached response in a new tab, its accessible but renders nothing again.

Can you point out what I'm doing wrong? Have tried a lot, but no luck.

2 个答案:

答案 0 :(得分:0)

我认为问题不在于流是早关闭的,我认为会在下载的方式问题,看看这里: https://docs.openkm.com/kcenter/view/sdk4j-1.1/document-samples.html#getContent

从服务器端(OpenKM与用户界面之间的中间位置)通常是问题:

//response.setContentLength(is.available()); // Cause a bug, because at this point InputStream still has not its real size.

你应该使用

response.setContentLength(new Long(doc.getActualVersion().getSize()).intValue());

答案 1 :(得分:0)

通过将request-promise替换为裸request包解决此问题,以便向java BE发出此请求,并将管道回复直接转换为角度FE的包装响应:

let reply = request(options);
reply.pipe(res);