Search in sources :

Example 21 with FileMeta

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);
}
Also used : FileSystemResource(org.springframework.core.io.FileSystemResource) File(java.io.File) FileMeta(org.molgenis.data.file.model.FileMeta) Test(org.testng.annotations.Test)

Example 22 with FileMeta

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;
}
Also used : IOException(java.io.IOException) FileMeta(org.molgenis.data.file.model.FileMeta)

Example 23 with 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);
}
Also used : FileSystemResource(org.springframework.core.io.FileSystemResource) File(java.io.File) FileMeta(org.molgenis.data.file.model.FileMeta) RunAsSystem(org.molgenis.security.core.runas.RunAsSystem)

Example 24 with FileMeta

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");
}
Also used : FileMeta(org.molgenis.data.file.model.FileMeta) Test(org.testng.annotations.Test) AbstractMockitoTest(org.molgenis.test.AbstractMockitoTest)

Example 25 with FileMeta

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());
        }
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) File(java.io.File) FileMeta(org.molgenis.data.file.model.FileMeta) FileInputStream(java.io.FileInputStream) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Aggregations

FileMeta (org.molgenis.data.file.model.FileMeta)32 Test (org.testng.annotations.Test)14 File (java.io.File)12 AbstractMockitoTest (org.molgenis.test.AbstractMockitoTest)6 Attribute (org.molgenis.data.meta.model.Attribute)5 Entity (org.molgenis.data.Entity)4 FileSystemResource (org.springframework.core.io.FileSystemResource)4 InputStream (java.io.InputStream)3 MolgenisDataException (org.molgenis.data.MolgenisDataException)3 EntityType (org.molgenis.data.meta.model.EntityType)3 IOException (java.io.IOException)2 Stream (java.util.stream.Stream)2 ZipFile (net.lingala.zip4j.core.ZipFile)2 ZipException (net.lingala.zip4j.exception.ZipException)2 Repository (org.molgenis.data.Repository)2 EntityImportReport (org.molgenis.data.importer.EntityImportReport)2 ImportService (org.molgenis.data.importer.ImportService)2 FileIngestJobExecution (org.molgenis.file.ingest.meta.FileIngestJobExecution)2 ServletUriComponentsBuilder (org.springframework.web.servlet.support.ServletUriComponentsBuilder)2 UriComponents (org.springframework.web.util.UriComponents)2