Removed deprecated configurations and added additional RDA Common Standards for dataset Templates
parent
1c91103781
commit
5b0a66ce06
@ -1,52 +0,0 @@
|
||||
package eu.eudat.controllers;
|
||||
|
||||
import eu.eudat.logic.managers.FileManager;
|
||||
import eu.eudat.logic.services.ApiContext;
|
||||
import eu.eudat.models.data.files.ContentFile;
|
||||
import eu.eudat.models.data.helpers.responses.ResponseItem;
|
||||
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 {
|
||||
|
||||
private FileManager fileManager;
|
||||
@Autowired
|
||||
public FileController(ApiContext apiContext, FileManager fileManager) {
|
||||
super(apiContext);
|
||||
this.fileManager = fileManager;
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.POST, value = {"/upload"})
|
||||
public ResponseEntity<ResponseItem<List<ContentFile>>> handleFileUpload(@RequestParam("file") MultipartFile[] files) throws IOException {
|
||||
return ResponseEntity.status(HttpStatus.OK).body(
|
||||
new ResponseItem<List<ContentFile>>().status(ApiMessageCode.NO_MESSAGE).payload(fileManager.saveTempFile(files)));
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET, value = {"/{id}"})
|
||||
public ResponseEntity<Resource> handleFileUpload(@PathVariable(name = "id") String id,
|
||||
@RequestParam(name = "type") String type,
|
||||
@RequestParam(name = "location", required = false, defaultValue = "final") String location) throws IOException {
|
||||
Resource resource = fileManager.getFile(id, type, location);
|
||||
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);
|
||||
}
|
||||
}
|
@ -1,33 +0,0 @@
|
||||
package eu.eudat.logic.managers;
|
||||
|
||||
import eu.eudat.models.data.files.ContentFile;
|
||||
import eu.eudat.logic.services.helpers.FileStorageService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by ikalyvas on 3/15/2018.
|
||||
*/
|
||||
@Component
|
||||
public class FileManager {
|
||||
|
||||
private FileStorageService fileStorageService;
|
||||
|
||||
@Autowired
|
||||
public FileManager(FileStorageService fileStorageService) {
|
||||
this.fileStorageService = fileStorageService;
|
||||
}
|
||||
|
||||
public List<ContentFile> saveTempFile(MultipartFile file[]) throws IOException {
|
||||
return fileStorageService.writeToTempFileSystem(file);
|
||||
}
|
||||
|
||||
public Resource getFile(String filename, String type, String location) throws IOException {
|
||||
return fileStorageService.readFromFilesystem(filename, type, location);
|
||||
}
|
||||
}
|
@ -1,19 +0,0 @@
|
||||
package eu.eudat.logic.services.helpers;
|
||||
|
||||
import eu.eudat.models.data.files.ContentFile;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by ikalyvas on 3/15/2018.
|
||||
*/
|
||||
public interface FileStorageService {
|
||||
List<ContentFile> writeToTempFileSystem(MultipartFile[] multipartFiles) throws IOException;
|
||||
|
||||
ContentFile copyFromTempFileSystem(ContentFile file) throws IOException;
|
||||
|
||||
Resource readFromFilesystem(String filename, String type, String location) throws IOException;
|
||||
}
|
@ -1,83 +0,0 @@
|
||||
package eu.eudat.logic.services.helpers;
|
||||
|
||||
import eu.eudat.exceptions.files.TempFileNotFoundException;
|
||||
import eu.eudat.models.data.files.ContentFile;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.UrlResource;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Created by ikalyvas on 3/15/2018.
|
||||
*/
|
||||
@Service("fileStorageService")
|
||||
public class FileStorageServiceImpl implements FileStorageService {
|
||||
private static final Logger logger = LoggerFactory.getLogger(FileStorageServiceImpl.class);
|
||||
|
||||
private Environment environment;
|
||||
|
||||
@Autowired
|
||||
public FileStorageServiceImpl(Environment environment) {
|
||||
this.environment = environment;
|
||||
this.init();
|
||||
}
|
||||
|
||||
public List<ContentFile> writeToTempFileSystem(MultipartFile[] multipartFiles) throws IOException {
|
||||
List<ContentFile> contentFileList = new LinkedList<>();
|
||||
for (MultipartFile multipartFile : Arrays.asList(multipartFiles)) {
|
||||
UUID id = UUID.randomUUID();
|
||||
Files.copy(multipartFile.getInputStream(), Paths.get(environment.getProperty("files.storage.temp")).resolve(id.toString()));
|
||||
ContentFile contentFile = new ContentFile(multipartFile.getOriginalFilename(), id, "temp", getFileExtension(multipartFile.getOriginalFilename()));
|
||||
contentFileList.add(contentFile);
|
||||
}
|
||||
return contentFileList;
|
||||
}
|
||||
|
||||
public ContentFile copyFromTempFileSystem(ContentFile file) throws IOException, TempFileNotFoundException {
|
||||
UUID id = UUID.randomUUID();
|
||||
if (!Files.exists(Paths.get(environment.getProperty("files.storage.temp") + "/" + file.getId())))
|
||||
throw new TempFileNotFoundException();
|
||||
Files.copy(Paths.get(environment.getProperty("files.storage.temp") + "/" + file.getId()), Paths.get(environment.getProperty("files.storage.final")).resolve(id.toString()));
|
||||
ContentFile contentFile = new ContentFile(file.getFilename(), id, file.getLocation(), file.getType());
|
||||
return contentFile;
|
||||
}
|
||||
|
||||
public Resource readFromFilesystem(String filename, String type, String location) throws IOException {
|
||||
if (location.equals("temp")) {
|
||||
return new UrlResource(Paths.get(environment.getProperty("files.storage.temp") + '/' + filename).toUri());
|
||||
} else {
|
||||
return new UrlResource(Paths.get(environment.getProperty("files.storage.final") + '/' + filename).toUri());
|
||||
}
|
||||
}
|
||||
|
||||
public void init() {
|
||||
try {
|
||||
if (!Files.exists(Paths.get(environment.getProperty("files.storage.temp")))) {
|
||||
Files.createDirectory(Paths.get(environment.getProperty("files.storage.temp")));
|
||||
}
|
||||
if (!Files.exists(Paths.get(environment.getProperty("files.storage.final")))) {
|
||||
Files.createDirectory(Paths.get(environment.getProperty("files.storage.final")));
|
||||
}
|
||||
} catch (IOException e) {
|
||||
logger.error(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
private static String getFileExtension(String fileName) {
|
||||
if (fileName.lastIndexOf(".") != -1 && fileName.lastIndexOf(".") != 0)
|
||||
return fileName.substring(fileName.lastIndexOf(".") + 1);
|
||||
else return "";
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue