com.processpuzzle.fitnesse.connect.testbed.service.FileUploadService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of fitnesse-connect-testbed-backend Show documentation
Show all versions of fitnesse-connect-testbed-backend Show documentation
Test server for integration tests
package com.processpuzzle.fitnesse.connect.testbed.service;
import java.io.IOException;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
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;
import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.processpuzzle.fitnesse.connect.testbed.file.StorageFileNotFoundException;
import com.processpuzzle.fitnesse.connect.testbed.file.StorageService;
@Controller
@RequestMapping( "/api/files" )
public class FileUploadService {
private final StorageService storageService;
// constructors
@Autowired public FileUploadService( StorageService storageService ) {
this.storageService = storageService;
this.storageService.init();
}
// public accessors and mutators
@PostMapping( "" ) public String handleFileUpload( @RequestParam( "file" ) MultipartFile file, RedirectAttributes redirectAttributes ) {
storageService.store( file );
redirectAttributes.addFlashAttribute( "message", "You successfully uploaded " + file.getOriginalFilename() + "!" );
return "redirect:/api/files";
}
@ExceptionHandler( StorageFileNotFoundException.class ) public ResponseEntity> handleStorageFileNotFound( StorageFileNotFoundException exc ) {
return ResponseEntity.notFound().build();
}
@GetMapping public String listUploadedFiles( Model model ) throws IOException {
// @formatter:off
model.addAttribute( "files", storageService.loadAll().map(
path -> MvcUriComponentsBuilder.fromMethodName( FileUploadService.class, "serveFile", path.getFileName().toString() ).build().toString() )
.collect( Collectors.toList() ));
// @formatter:on
return "uploadForm";
}
@GetMapping( "/files/{filename:.+}" ) @ResponseBody public ResponseEntity serveFile( @PathVariable String filename ) {
Resource file = storageService.loadAsResource( filename );
return ResponseEntity.ok().header( HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + file.getFilename() + "\"" ).body( file );
}
}