org.springframework.data.mongodb.gridfs.GridFsResource Java Examples

The following examples show how to use org.springframework.data.mongodb.gridfs.GridFsResource. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GridFsTests.java    From spring-data-examples with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldStoreAndReadFile() throws IOException {

	byte[] bytes;
	try (InputStream is = new BufferedInputStream(new ClassPathResource("./example-file.txt").getInputStream())) {
		bytes = StreamUtils.copyToByteArray(is);
	}

	// store file
	gridFsOperations.store(new ByteArrayInputStream(bytes), "example-file.txt");

	GridFsResource resource = gridFsOperations.getResource("example-file.txt");

	byte[] loaded;
	try (InputStream is = resource.getInputStream()) {
		loaded = StreamUtils.copyToByteArray(is);
	}

	assertThat(bytes).isEqualTo(loaded);
}
 
Example #2
Source File: GridDaoImpl.java    From beihu-boot with Apache License 2.0 5 votes vote down vote up
private MongoFile gfs2Mg(GridFSFile gridFSDBFile) {
    try {
        MongoFile mongoFile = new MongoFile();
        Document metaData = gridFSDBFile.getMetadata();
        mongoFile.setContentType(String.valueOf(metaData.get("contentType")));
        mongoFile.setFilename(String.valueOf(metaData.get("filename")));
        mongoFile.setCreateTime(gridFSDBFile.getUploadDate().getTime());
        mongoFile.setGridId(gridFSDBFile.getFilename());
        GridFsResource gridFsResource = gridFsTemplate.getResource(gridFSDBFile.getFilename());
        mongoFile.setContent(org.springframework.util.StreamUtils.copyToByteArray(gridFsResource.getInputStream()));
        return mongoFile;
    } catch (IOException e) {
        throw new RuntimeException("文件读取异常!");
    }
}
 
Example #3
Source File: MongoDBArtifactStore.java    From hawkbit-extensions with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected AbstractDbArtifact store(final String tenant, final DbArtifactHash base16Hashes, final String contentType,
        final String tempFile) throws IOException {

    final GridFSFile result = gridFs.findOne(new Query().addCriteria(
            Criteria.where(FILENAME).is(base16Hashes.getSha1()).and(TENANT_QUERY).is(sanitizeTenant(tenant))));

    if (result == null) {
        try {
            final GridFSFile temp = loadTempFile(tempFile);

            final Document metadata = new Document();
            metadata.put(SHA1, base16Hashes.getSha1());
            metadata.put(TENANT, tenant);
            metadata.put(FILENAME, base16Hashes.getSha1());
            metadata.put(CONTENT_TYPE, contentType);

            final GridFsResource resource = gridFs.getResource(temp);
            final ObjectId id = gridFs.store(resource.getInputStream(), base16Hashes.getSha1(), contentType, metadata);
            final GridFSFile file = gridFs.findOne(new Query().addCriteria(Criteria.where(ID).is(id)));

            return createGridFsArtifact(file, contentType, base16Hashes);

        } catch (final MongoClientException e) {
            throw new ArtifactStoreException(e.getMessage(), e);
        }
    }

    return createGridFsArtifact(result, contentType, base16Hashes);
}
 
Example #4
Source File: GridFsStoreResource.java    From spring-content with Apache License 2.0 4 votes vote down vote up
public GridFsStoreResource(Resource delegate, GridFsTemplate gridfs) {
	Assert.isInstanceOf(GridFsResource.class,
			"delegate must be an instance of GridFsResource");
	this.delegate = (GridFsResource) delegate;
	this.gridfs = gridfs;
}