com.abixen.platform.core.interfaces.web.common.ImageLibraryController.java Source code

Java tutorial

Introduction

Here is the source code for com.abixen.platform.core.interfaces.web.common.ImageLibraryController.java

Source

/**
 * Copyright (c) 2010-present Abixen Systems. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.abixen.platform.core.interfaces.web.common;

import com.abixen.platform.core.infrastructure.configuration.properties.PlatformResourceConfigurationProperties;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

@Slf4j
@RestController
@RequestMapping(value = "/api/images")
public class ImageLibraryController {

    @Autowired
    private PlatformResourceConfigurationProperties platformResourceConfigurationProperties;

    @RequestMapping(value = "layout/{fileName}/", method = RequestMethod.GET)
    public ResponseEntity<byte[]> getImage(@PathVariable String fileName) throws IOException {

        log.debug("fileName: " + fileName);

        InputStream in;
        try {
            in = new FileInputStream(platformResourceConfigurationProperties.getImageLibraryDirectory()
                    + "/layout-miniature/" + fileName);
        } catch (FileNotFoundException e) {
            in = new FileInputStream(platformResourceConfigurationProperties.getImageLibraryDirectory()
                    + "/layout-miniature/default-layout-icon.png");
        }
        byte[] b = IOUtils.toByteArray(in);

        in.close();

        final HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.IMAGE_PNG);

        return new ResponseEntity<byte[]>(b, headers, HttpStatus.CREATED);
    }
}