<commit_before>package com.gentics.mesh.core.verticle.node;

import static com.gentics.mesh.core.rest.error.HttpStatusCodeErrorException.error;
import static io.netty.handler.codec.http.HttpResponseStatus.NOT_FOUND;

import java.io.File;

import com.gentics.mesh.core.data.node.Node;
import com.gentics.mesh.core.image.spi.ImageManipulator;
import com.gentics.mesh.handler.InternalActionContext;

import io.vertx.core.Handler;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.HttpHeaders;
import io.vertx.ext.web.RoutingContext;
import rx.Observable;

public class NodeBinaryHandler implements Handler<Node> {

	private RoutingContext rc;
	private ImageManipulator imageManipulator;

	public NodeBinaryHandler(RoutingContext rc, ImageManipulator imageManipulator) {
		this.rc = rc;
		this.imageManipulator = imageManipulator;
	}

	@Override
	public void handle(Node node) {
		if (!node.getSchema().isBinary()) {
			rc.fail(error(NOT_FOUND, "node_error_no_binary_node"));
			return;
		}
		File binaryFile = node.getBinaryFile();
		if (!binaryFile.exists()) {
			rc.fail(error(NOT_FOUND, "node_error_binary_data_not_found"));
			return;
		} else {
			InternalActionContext ac = InternalActionContext.create(rc);
			String contentLength = String.valueOf(node.getBinaryFileSize());
			String fileName = node.getBinaryFileName();
			String contentType = node.getBinaryContentType();
			// Resize the image if needed
			if (node.hasBinaryImage() && ac.getImageRequestParameter().isSet()) {
				Observable<io.vertx.rxjava.core.buffer.Buffer> buffer = imageManipulator.handleResize(node.getBinaryFile(), node.getBinarySHA512Sum(),
						ac.getImageRequestParameter());
				buffer.subscribe(imageBuffer -> {
					rc.response().putHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(imageBuffer.length()));
					rc.response().putHeader(HttpHeaders.CONTENT_TYPE, "image/jpeg");
					// TODO encode filename?
					rc.response().putHeader("content-disposition", "attachment; filename=" + fileName);
					rc.response().end((Buffer) imageBuffer.getDelegate());
				} , error -> {
					rc.fail(error);
				});
			} else {
				node.getBinaryFileBuffer().setHandler(bh -> {
					Buffer buffer = bh.result();
					rc.response().putHeader(HttpHeaders.CONTENT_LENGTH, contentLength);
					rc.response().putHeader(HttpHeaders.CONTENT_TYPE, contentType);
					// TODO encode filename?
					rc.response().putHeader("content-disposition", "attachment; filename=" + fileName);
					rc.response().end(buffer);
				});
			}
		}

	}

}<commit_msg>Show images inline<commit_after>package com.gentics.mesh.core.verticle.node;

import static com.gentics.mesh.core.rest.error.HttpStatusCodeErrorException.error;
import static io.netty.handler.codec.http.HttpResponseStatus.NOT_FOUND;

import java.io.File;

import com.gentics.mesh.core.data.node.Node;
import com.gentics.mesh.core.image.spi.ImageManipulator;
import com.gentics.mesh.handler.InternalActionContext;

import io.vertx.core.Handler;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.HttpHeaders;
import io.vertx.ext.web.RoutingContext;
import rx.Observable;

public class NodeBinaryHandler implements Handler<Node> {

	private RoutingContext rc;
	private ImageManipulator imageManipulator;

	public NodeBinaryHandler(RoutingContext rc, ImageManipulator imageManipulator) {
		this.rc = rc;
		this.imageManipulator = imageManipulator;
	}

	@Override
	public void handle(Node node) {
		if (!node.getSchema().isBinary()) {
			rc.fail(error(NOT_FOUND, "node_error_no_binary_node"));
			return;
		}
		File binaryFile = node.getBinaryFile();
		if (!binaryFile.exists()) {
			rc.fail(error(NOT_FOUND, "node_error_binary_data_not_found"));
			return;
		} else {
			InternalActionContext ac = InternalActionContext.create(rc);
			String contentLength = String.valueOf(node.getBinaryFileSize());
			String fileName = node.getBinaryFileName();
			String contentType = node.getBinaryContentType();
			// Resize the image if needed
			if (node.hasBinaryImage() && ac.getImageRequestParameter().isSet()) {
				Observable<io.vertx.rxjava.core.buffer.Buffer> buffer = imageManipulator.handleResize(node.getBinaryFile(), node.getBinarySHA512Sum(),
						ac.getImageRequestParameter());
				buffer.subscribe(imageBuffer -> {
					rc.response().putHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(imageBuffer.length()));
					rc.response().putHeader(HttpHeaders.CONTENT_TYPE, "image/jpeg");
					// TODO encode filename?
					rc.response().putHeader("content-disposition", "inline; filename=" + fileName);
					rc.response().end((Buffer) imageBuffer.getDelegate());
				} , error -> {
					rc.fail(error);
				});
			} else {
				node.getBinaryFileBuffer().setHandler(bh -> {
					Buffer buffer = bh.result();
					rc.response().putHeader(HttpHeaders.CONTENT_LENGTH, contentLength);
					rc.response().putHeader(HttpHeaders.CONTENT_TYPE, contentType);
					// TODO encode filename?
					rc.response().putHeader("content-disposition", "attachment; filename=" + fileName);
					rc.response().end(buffer);
				});
			}
		}

	}

}
{'fraction_non_alphanumeric': 0.13243724056137576, 'fraction_numerical': 0.0011860051393556038, 'mean_word_length': 4.743473325766175, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6963653', 'n_tokens_mistral': 1765, 'n_tokens_neox': 1555, 'n_words': 248}