All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sippnex.fileblade.controller.FileController Maven / Gradle / Ivy

package com.sippnex.fileblade.controller;

import com.sippnex.fileblade.domain.FbFile;
import com.sippnex.fileblade.service.FileService;
import com.sippnex.fileblade.util.PathUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.IOException;

@RestController
@RequestMapping("/fileblade/files")
public class FileController {

    private final FileService fileService;

    public FileController(FileService fileService) {
        this.fileService = fileService;
    }

    @GetMapping("**")
    public void download(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String pathString = PathUtils.getPathFromRequest(request);
        FbFile fbFile = fileService.readFile(pathString);
        IOUtils.copy(new ByteArrayInputStream(fbFile.getContent()), response.getOutputStream());
    }

    @PostMapping("**")
    public void upload(HttpServletRequest request, @RequestParam("file") MultipartFile file) throws IOException {
        String pathString = PathUtils.getPathFromRequest(request);
        fileService.writeFile(new FbFile(pathString, file.getBytes()));
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy