use of org.molgenis.data.file.model.FileMeta in project molgenis by molgenis.
the class StyleServiceTest method getBootstrap3ThemeData.
@Test
public void getBootstrap3ThemeData() throws MolgenisStyleException {
String styleName = "my-style";
StyleSheet styleSheet = mock(StyleSheet.class);
when(styleSheet.getId()).thenReturn(styleName);
FileMeta fileMeta = mock(FileMeta.class);
String fileId = "fileId";
when(fileMeta.getId()).thenReturn(fileId);
when(styleSheet.getBootstrap3Theme()).thenReturn(fileMeta);
Query<StyleSheet> expectedQuery = new QueryImpl<StyleSheet>().eq(StyleSheetMetadata.NAME, styleName);
when(dataService.findOne(STYLE_SHEET, expectedQuery, StyleSheet.class)).thenReturn(styleSheet);
File styleFile = mock(File.class);
String mockFilePath = "mock/file/path";
when(styleFile.getPath()).thenReturn(mockFilePath);
when(fileStore.getFile(fileId)).thenReturn(styleFile);
BootstrapVersion version = BootstrapVersion.BOOTSTRAP_VERSION_3;
FileSystemResource themeData = styleService.getThemeData(styleName, version);
assertEquals(themeData.getPath(), mockFilePath);
}
use of org.molgenis.data.file.model.FileMeta in project molgenis by molgenis.
the class StyleServiceImpl method createStyleSheetFileMeta.
private FileMeta createStyleSheetFileMeta(String fileName, InputStream data) throws MolgenisStyleException {
String fileId = idGenerator.generateId();
try {
fileStore.store(data, fileId);
} catch (IOException e) {
throw new MolgenisStyleException("Unable to save style file with name : " + fileName, e);
}
FileMeta fileMeta = fileMetaFactory.create(fileId);
fileMeta.setContentType("css");
fileMeta.setFilename(fileName);
fileMeta.setSize(fileStore.getFile(fileId).length());
fileMeta.setUrl(buildFileUrl(fileId));
dataService.add(FileMetaMetaData.FILE_META, fileMeta);
return fileMeta;
}
use of org.molgenis.data.file.model.FileMeta in project molgenis by molgenis.
the class StyleServiceImpl method getThemeData.
@Override
@RunAsSystem
public FileSystemResource getThemeData(String styleName, BootstrapVersion bootstrapVersion) throws MolgenisStyleException {
StyleSheet styleSheet = findThemeByName(styleName);
if (styleSheet == null) {
throw new MolgenisStyleException("No theme found for with name: " + styleName);
}
// Fetch the theme file from the store.
FileMeta fileMeta;
if (bootstrapVersion.equals(BOOTSTRAP_VERSION_3)) {
fileMeta = styleSheet.getBootstrap3Theme();
} else {
fileMeta = styleSheet.getBootstrap4Theme();
// If no bootstrap 4 theme was set fetch the default theme from the resources folder
if (fileMeta == null) {
StyleSheet fallBackTheme = findThemeByName(BOOTSTRAP_FALL_BACK_THEME);
fileMeta = fallBackTheme.getBootstrap4Theme();
}
}
File file = fileStore.getFile(fileMeta.getId());
return new FileSystemResource(file);
}
use of org.molgenis.data.file.model.FileMeta in project molgenis by molgenis.
the class FileMetaRepositoryDecoratorTest method testDelete.
@Test
public void testDelete() throws Exception {
FileMeta fileMeta = getMockFileMeta("id");
fileMetaRepositoryDecorator.delete(fileMeta);
verify(delegateRepository).delete(fileMeta);
verify(fileStore).delete("id");
}
use of org.molgenis.data.file.model.FileMeta in project molgenis by molgenis.
the class FileDownloadController method getFile.
@GetMapping("/{id:.+}")
public void getFile(@PathVariable("id") String id, HttpServletResponse response) throws IOException {
FileMeta fileMeta = dataService.findOneById(FILE_META, id, FileMeta.class);
if (fileMeta == null) {
response.setStatus(HttpStatus.NOT_FOUND.value());
} else {
// Not so nice but keep to serve old legacy files
File fileStoreFile = fileStore.getFile(fileMeta.getFilename());
if (!fileStoreFile.exists()) {
fileStoreFile = fileStore.getFile(id);
}
if (!fileStoreFile.exists()) {
response.setStatus(HttpStatus.NOT_FOUND.value());
}
// if file meta data exists for this file
String outputFilename = fileMeta.getFilename();
String contentType = fileMeta.getContentType();
if (contentType != null) {
response.setContentType(contentType);
}
Long size = fileMeta.getSize();
if (size != null) {
response.setContentLength(size.intValue());
}
response.setHeader("Content-Disposition", "attachment; filename=" + outputFilename.replace(" ", "_"));
try (InputStream is = new FileInputStream(fileStoreFile)) {
FileCopyUtils.copy(is, response.getOutputStream());
}
}
}
Aggregations