use of io.openk9.model.BinaryPayload in project openk9 by smclab.
the class ResourcesHttpHandler method _sendResource.
private Mono<Void> _sendResource(Tenant tenant, Datasource datasource, PluginDriverDTO pluginDriverDTO, long datasourceId, String documentId, String resourceId, HttpServerRequest httpRequest, HttpServerResponse httpResponse) {
return _queryParser.apply(QueryParser.Context.of(tenant, null, null, null, httpRequest, QueryParser.QueryCondition.DEFAULT)).flatMap(consumer -> _search.search(factory -> {
SearchRequest searchRequest = factory.createSearchRequestData(tenant.getTenantId(), pluginDriverDTO.getName());
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.must(QueryBuilders.idsQuery().addIds(documentId));
boolQueryBuilder.must(QueryBuilders.nestedQuery(_RESOURCES_BINARIES, QueryBuilders.matchQuery(_RESOURCES_BINARIES_ID, resourceId), ScoreMode.Max));
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
consumer.accept(boolQueryBuilder);
searchSourceBuilder.query(boolQueryBuilder);
searchSourceBuilder.fetchSource(new String[] { _RESOURCES_BINARIES_DATA, _RESOURCES_BINARIES_CONTENT_TYPE }, null);
return searchRequest.source(searchSourceBuilder);
})).flatMap(response -> {
SearchHits searchHits = response.getHits();
SearchHit[] hits = searchHits.getHits();
if (hits.length == 0) {
return httpResponse.sendNotFound();
} else if (hits.length > 1) {
_log.warn("found more than one resource (datasourceId: " + datasourceId + " resourceId: " + resourceId + " tenantId: " + tenant.getTenantId() + " documentId: " + documentId + ")");
}
SearchHit hit = hits[0];
String source = hit.getSourceAsString();
Resources resources = _jsonFactory.fromJson(source, Resources.class);
ResourcesPayload resourcesPayload = resources.getResources();
BinaryPayload binaryPayload = resourcesPayload.getBinaries().get(0);
String data = binaryPayload.getData();
String contentType = binaryPayload.getContentType();
_manageCache(httpRequest, httpResponse);
if (contentType != null && !contentType.isBlank()) {
httpResponse.header(_CONTENT_TYPE, contentType);
}
byte[] decode = Base64.getDecoder().decode(data);
return Mono.from(httpResponse.sendByteArray(Mono.just(decode)));
});
}
Aggregations