2018-03-19 13:40:04 +01:00
|
|
|
package eu.eudat.controllers;
|
|
|
|
|
2018-06-27 12:29:21 +02:00
|
|
|
import eu.eudat.logic.managers.FileManager;
|
2018-08-31 16:12:31 +02:00
|
|
|
import eu.eudat.logic.services.ApiContext;
|
2018-06-27 12:29:21 +02:00
|
|
|
import eu.eudat.models.data.files.ContentFile;
|
|
|
|
import eu.eudat.models.data.helpers.responses.ResponseItem;
|
2018-03-19 13:40:04 +01:00
|
|
|
import eu.eudat.types.ApiMessageCode;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.core.io.Resource;
|
|
|
|
import org.springframework.core.io.UrlResource;
|
|
|
|
import org.springframework.http.HttpHeaders;
|
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
|
import org.springframework.http.ResponseEntity;
|
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by ikalyvas on 3/15/2018.
|
|
|
|
*/
|
|
|
|
@RestController
|
|
|
|
@CrossOrigin
|
|
|
|
@RequestMapping(value = {"/api/files"})
|
|
|
|
public class FileController extends BaseController {
|
|
|
|
|
2019-03-05 16:33:59 +01:00
|
|
|
private FileManager fileManager;
|
2018-03-19 13:40:04 +01:00
|
|
|
@Autowired
|
2019-03-05 16:33:59 +01:00
|
|
|
public FileController(ApiContext apiContext, FileManager fileManager) {
|
2018-03-19 13:40:04 +01:00
|
|
|
super(apiContext);
|
2019-03-05 16:33:59 +01:00
|
|
|
this.fileManager = fileManager;
|
2018-03-19 13:40:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@RequestMapping(method = RequestMethod.POST, value = {"/upload"})
|
2018-08-31 16:12:31 +02:00
|
|
|
public ResponseEntity<ResponseItem<List<ContentFile>>> handleFileUpload(@RequestParam("file") MultipartFile[] files) throws IOException {
|
|
|
|
return ResponseEntity.status(HttpStatus.OK).body(
|
2019-03-05 16:33:59 +01:00
|
|
|
new ResponseItem<List<ContentFile>>().status(ApiMessageCode.NO_MESSAGE).payload(fileManager.saveTempFile(files)));
|
2018-03-19 13:40:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@RequestMapping(method = RequestMethod.GET, value = {"/{id}"})
|
|
|
|
public ResponseEntity<Resource> handleFileUpload(@PathVariable(name = "id") String id,
|
|
|
|
@RequestParam(name = "type") String type,
|
2018-08-31 16:12:31 +02:00
|
|
|
@RequestParam(name = "location", required = false, defaultValue = "final") String location) throws IOException {
|
2019-03-05 16:33:59 +01:00
|
|
|
Resource resource = fileManager.getFile(id, type, location);
|
2018-08-31 16:12:31 +02:00
|
|
|
if (!resource.exists())
|
|
|
|
resource = new UrlResource(FileController.class.getClassLoader().getResource("images/default.png"));
|
|
|
|
return ResponseEntity.ok()
|
|
|
|
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename() + "." + type + "\"")
|
|
|
|
.body(resource);
|
2018-03-19 13:40:04 +01:00
|
|
|
}
|
|
|
|
}
|