@Override public GridFSUploadStream openUploadStream(final ClientSession clientSession, final BsonValue id, final String filename) { return openUploadStream(clientSession, id, filename, new GridFSUploadOptions()); }
/** * Compute the {@link GridFSUploadOptions} to be used from the given {@literal contentType} and {@literal metadata} * {@link Document}. * * @param contentType can be {@literal null}. * @param metadata can be {@literal null} * @return never {@literal null}. */ protected GridFSUploadOptions computeUploadOptionsFor(@Nullable String contentType, @Nullable Document metadata) { Document targetMetadata = new Document(); if (StringUtils.hasText(contentType)) { targetMetadata.put(GridFsResource.CONTENT_TYPE_FIELD, contentType); } if (metadata != null) { targetMetadata.putAll(metadata); } GridFSUploadOptions options = new GridFSUploadOptions(); options.metadata(targetMetadata); return options; }
private GridFSUploadStream createGridFSUploadStream(@Nullable final ClientSession clientSession, final BsonValue id, final String filename, final GridFSUploadOptions options) { notNull("options", options); Integer chunkSizeBytes = options.getChunkSizeBytes(); int chunkSize = chunkSizeBytes == null ? this.chunkSizeBytes : chunkSizeBytes; checkCreateIndex(clientSession); return new GridFSUploadStreamImpl(clientSession, filesCollection, chunksCollection, id, filename, chunkSize, disableMD5, options.getMetadata()); }
@Override public void putBlobStream(BucketPath bucketPath, PayloadStream payloadStream) { LOGGER.debug("start putBlobStream for " + bucketPath); GridFSBucket bucket = getGridFSBucket(bucketPath); checkBucketExists(bucket); String filename = bucketPath.getObjectHandle().getName(); GridFSUploadOptions uploadOptions = new GridFSUploadOptions(); uploadOptions.metadata(new Document()); SimpleStorageMetadataImpl storageMetadata = new SimpleStorageMetadataImpl(payloadStream.getStorageMetadata()); storageMetadata.setType(StorageType.BLOB); storageMetadata.setName(BucketPathUtil.getAsString(bucketPath)); uploadOptions.getMetadata().put(STORAGE_METADATA_KEY, gsonHelper.toJson(storageMetadata)); InputStream is = payloadStream.openStream(); ObjectId objectId = bucket.uploadFromStream(filename, is, uploadOptions); IOUtils.closeQuietly(is); deleteAllExcept(bucket, filename, objectId); LOGGER.debug("finished putBlobStream for " + bucketPath); }
private GridFSUploadOptions getGridFSUploadOptions(String uniqueId, String fileName, boolean compress, long timestamp, Map<String, String> metadataMap) { Document metadata = new Document(); if (metadataMap != null) { for (String key : metadataMap.keySet()) { metadata.put(key, metadataMap.get(key)); } } metadata.put(TIMESTAMP, timestamp); metadata.put(COMPRESSED_FLAG, compress); metadata.put(DOCUMENT_UNIQUE_ID_KEY, uniqueId); metadata.put(FILE_UNIQUE_ID_KEY, getGridFsId(uniqueId, fileName)); return new GridFSUploadOptions().chunkSizeBytes(1024).metadata(metadata); }
private void executeUploadFromStream(@Nullable final ClientSession clientSession, final BsonValue id, final String filename, final InputStream source, final GridFSUploadOptions options) { GridFSUploadStream uploadStream = createGridFSUploadStream(clientSession, id, filename, options); Integer chunkSizeBytes = options.getChunkSizeBytes(); int chunkSize = chunkSizeBytes == null ? this.chunkSizeBytes : chunkSizeBytes; byte[] buffer = new byte[chunkSize]; int len; try { while ((len = source.read(buffer)) != -1) { uploadStream.write(buffer, 0, len); } uploadStream.close(); } catch (IOException e) { uploadStream.abort(); throw new MongoGridFSException("IOException when reading from the InputStream", e); } }
private void executeUploadFromStream(@Nullable final ClientSession clientSession, final BsonValue id, final String filename, final InputStream source, final GridFSUploadOptions options) { GridFSUploadStream uploadStream = createGridFSUploadStream(clientSession, id, filename, options); Integer chunkSizeBytes = options.getChunkSizeBytes(); int chunkSize = chunkSizeBytes == null ? this.chunkSizeBytes : chunkSizeBytes; byte[] buffer = new byte[chunkSize]; int len; try { while ((len = source.read(buffer)) != -1) { uploadStream.write(buffer, 0, len); } uploadStream.close(); } catch (IOException e) { uploadStream.abort(); throw new MongoGridFSException("IOException when reading from the InputStream", e); } }
@Override public ObjectId uploadFromStream(final String filename, final InputStream source) { return uploadFromStream(filename, source, new GridFSUploadOptions()); }
public ObjectId store(InputStream content, @Nullable String filename, @Nullable String contentType, @Nullable Document metadata) { Assert.notNull(content, "InputStream must not be null!"); GridFSUploadOptions options = new GridFSUploadOptions(); Document mData = new Document(); if (StringUtils.hasText(contentType)) { mData.put(GridFsResource.CONTENT_TYPE_FIELD, contentType); } if (metadata != null) { mData.putAll(metadata); } options.metadata(mData); return getGridFs().uploadFromStream(filename, content, options); }
private GridFSUploadStream createGridFSUploadStream(@Nullable final ClientSession clientSession, final BsonValue id, final String filename, final GridFSUploadOptions options) { notNull("options", options); Integer chunkSizeBytes = options.getChunkSizeBytes(); int chunkSize = chunkSizeBytes == null ? this.chunkSizeBytes : chunkSizeBytes; checkCreateIndex(clientSession); return new GridFSUploadStreamImpl(clientSession, filesCollection, chunksCollection, id, filename, chunkSize, disableMD5, options.getMetadata()); }
private void executeUploadFromStream(@Nullable final ClientSession clientSession, final BsonValue id, final String filename, final InputStream source, final GridFSUploadOptions options) { GridFSUploadStream uploadStream = createGridFSUploadStream(clientSession, id, filename, options); Integer chunkSizeBytes = options.getChunkSizeBytes(); int chunkSize = chunkSizeBytes == null ? this.chunkSizeBytes : chunkSizeBytes; byte[] buffer = new byte[chunkSize]; int len; try { while ((len = source.read(buffer)) != -1) { uploadStream.write(buffer, 0, len); } uploadStream.close(); } catch (IOException e) { uploadStream.abort(); throw new MongoGridFSException("IOException when reading from the InputStream", e); } }
@Override public void uploadFromStream(final BsonValue id, final String filename, final InputStream source) { uploadFromStream(id, filename, source, new GridFSUploadOptions()); }
/** * Persists the data from 'source' into GridFS using the 'payloadPath' * property as filename. * * @param source - the source input stream * @param metadata - the doc metadata */ public void create(InputStream source, Document metadata) throws StorageException { GridFSUploadOptions options = new GridFSUploadOptions() .metadata(metadata); mongoFileId = getBucket().uploadFromStream(payloadPath, source, options); }
private GridFSUploadStream createGridFSUploadStream(@Nullable final ClientSession clientSession, final BsonValue id, final String filename, final GridFSUploadOptions options) { notNull("options", options); Integer chunkSizeBytes = options.getChunkSizeBytes(); int chunkSize = chunkSizeBytes == null ? this.chunkSizeBytes : chunkSizeBytes; checkCreateIndex(clientSession); return new GridFSUploadStreamImpl(clientSession, filesCollection, chunksCollection, id, filename, chunkSize, disableMD5, options.getMetadata()); }
@Override public GridFSUploadStream openUploadStream(final BsonValue id, final String filename) { return openUploadStream(id, filename, new GridFSUploadOptions()); }
@Override public String save(Media media) { Document doc = new Document() .append("type", media.getType()) .append("subType", media.getSubType()) .append("size", media.getSize()) .append("hash", media.getHash()); if(media.getApi() != null) { doc.append("api", media.getApi()); } GridFSUploadOptions options = new GridFSUploadOptions() .metadata(doc); getGridFs() .uploadFromStream( new BsonString(media.getId()), media.getFileName(), new ByteArrayInputStream(media.getData()), options ); return media.getId(); }
@Override public ObjectId uploadFromStream(final ClientSession clientSession, final String filename, final InputStream source) { return uploadFromStream(clientSession, filename, source, new GridFSUploadOptions()); }
GridFSUploadOptions options = new GridFSUploadOptions() .metadata(Document.parse(metadata.toJson())); BsonValue _id = metadata.remove("_id"); GridFSUploadOptions options = new GridFSUploadOptions() .metadata(Document.parse(metadata.toJson()));
@Override public void uploadFromStream(final ClientSession clientSession, final BsonValue id, final String filename, final InputStream source) { uploadFromStream(clientSession, id, filename, source, new GridFSUploadOptions()); }
@Override public void uploadFromStream(final ClientSession clientSession, final BsonValue id, final String filename, final InputStream source) { uploadFromStream(clientSession, id, filename, source, new GridFSUploadOptions()); }