paulcwarren/spring-content

ContentRepository cannot persistent content

Closed this issue · 2 comments

Hi,

I am using MongoDB to save the file meta information and use content-fs-spring-boot-starter 1.2.1 to persist the FileStream like this:

contentRepository.setContent(meta, Files.newInputStream('/tmp/test.mp3'));
metaRepository.save(meta);

It was OK when I played the music on a GET-endpoint after I persisted the meta. However, after I restarted my Spring-Boot application and tried to play the music again, I got the Exception 'InputStream must not be null' when getContent was called. It seemed that the content had not been persisted really.

Do you have any idea? Thanks!

Sounds like you are not setting a permanent root for the filesystem content store? By default the spring boot starter will choose a new temporary folder in your tmpdir everytime it starts up - giving an experience a bit like using H2 or HSQLDB. Usually fine for testing.

You need to override that behavior with a bean to sets a permanent root. Or, since you're using Mongo, use Mongo's GridFS?). The docs example is also using a temp dir which probably isn't the best example but hopefully you get the point. You need to set a permanent root; e.g. /data/mystore.

Thank you very much! I set up the file system root, and It works now as you suggested.

@Configuration
@EnableFilesystemStores
public class ContentConfig {
    @Value("${file.upload.dir}")
    private String uploadDir;

    @Bean
    File filesystemRoot() {
        try {
            return Files.createDirectory(Paths.get(uploadDir)).toFile();
        } catch (FileAlreadyExistsException faee) {
          return new File(uploadDir);
        } catch (IOException ioe) {}

        return null;
    }

    @Bean
    FileSystemResourceLoader fileSystemResourceLoader() {
        return new FileSystemResourceLoader(filesystemRoot().getAbsolutePath());
    }
}