com.intuit.karate.demo.controller.UploadController.java Source code

Java tutorial

Introduction

Here is the source code for com.intuit.karate.demo.controller.UploadController.java

Source

/*
 * The MIT License
 *
 * Copyright 2017 Intuit Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.intuit.karate.demo.controller;

import com.intuit.karate.demo.domain.FileInfo;
import java.io.File;
import java.util.UUID;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

/**
 *
 * @author pthomas3
 */
@Controller
@RequestMapping("/files")
public class UploadController {

    private static final Logger logger = LoggerFactory.getLogger(UploadController.class);

    private static final String FILES_BASE = "target/demofiles/";

    public UploadController() throws Exception {
        File file = new File(FILES_BASE);
        FileUtils.forceMkdir(file);
        logger.info("created directory: {}", file);
    }

    @PostMapping
    public @ResponseBody FileInfo upload(@RequestParam("file") MultipartFile multipartFile,
            @RequestParam String name) throws Exception {
        String uuid = UUID.randomUUID().toString();
        String filePath = FILES_BASE + uuid;
        FileUtils.copyToFile(multipartFile.getInputStream(), new File(filePath));
        FileUtils.writeStringToFile(new File(filePath + "_meta.txt"), name, "utf-8");
        return new FileInfo(uuid, name);
    }

    @GetMapping("/{id:.+}")
    public ResponseEntity<Resource> download(@PathVariable String id) throws Exception {
        String filePath = FILES_BASE + id;
        File file = new File(filePath);
        File meta = new File(filePath + "_meta.txt");
        String name = FileUtils.readFileToString(meta, "utf-8");
        return ResponseEntity.ok().header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + name + "\"")
                .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM_VALUE)
                .body(new FileSystemResource(file));
    }

}