com.mongodb.client.gridfs.model.GridFSFile#getMetadata ( )源码实例Demo

下面列出了com.mongodb.client.gridfs.model.GridFSFile#getMetadata ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: hawkbit-extensions   文件: MongoDBArtifactStore.java
@SuppressWarnings("squid:S2589")
// False positive: file.getMetadata() can return null
private static final String getContentType(final GridFSFile file) {
    final Document metadata = file.getMetadata();
    String contentType = null;
    if (metadata != null) {
        contentType = metadata.getString(CONTENT_TYPE);
    }
    if (contentType == null) {
        try {
            contentType = file.getContentType();
        } catch (final MongoGridFSException e) {
            throw new ArtifactStoreException("Could not determine content type for file " + file.getId(), e);
        }
    }
    return contentType;
}
 
源代码2 项目: lumongo   文件: MongoDocumentStorage.java
@Override
public InputStream getAssociatedDocumentStream(String uniqueId, String fileName) {
	GridFSBucket gridFS = createGridFSConnection();
	GridFSFile file = gridFS.find(new Document(ASSOCIATED_METADATA + "." + FILE_UNIQUE_ID_KEY, getGridFsId(uniqueId, fileName))).first();

	if (file == null) {
		return null;
	}

	InputStream is = gridFS.openDownloadStream(file.getObjectId());
	;

	Document metadata = file.getMetadata();
	if (metadata.containsKey(COMPRESSED_FLAG)) {
		boolean compressed = (boolean) metadata.remove(COMPRESSED_FLAG);
		if (compressed) {
			is = new InflaterInputStream(is);
		}
	}

	return is;
}
 
源代码3 项目: nifi   文件: GridFSITTestBase.java
public boolean fileHasProperties(String name, String bucketName, Map<String, String> attrs) {
    GridFSBucket bucket = GridFSBuckets.create(client.getDatabase(DB), bucketName);
    MongoCursor it = bucket.find(Document.parse(String.format("{ \"filename\": \"%s\" }", name))).iterator();
    boolean retVal = false;

    if (it.hasNext()) {
        GridFSFile file = (GridFSFile)it.next();
        Document metadata = file.getMetadata();
        if (metadata != null && metadata.size() == attrs.size()) {
            retVal = true;
            for (Map.Entry<String, Object> entry : metadata.entrySet()) {
                Object val = attrs.get(entry.getKey());
                if (val == null || !entry.getValue().equals(val)) {
                    retVal = false;
                    break;
                }
            }
        }
    }

    it.close();

    return retVal;
}
 
源代码4 项目: beihu-boot   文件: GridDaoImpl.java
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("文件读取异常!");
    }
}
 
源代码5 项目: lumongo   文件: MongoDocumentStorage.java
private AssociatedDocument loadGridFSToAssociatedDocument(GridFSBucket gridFS, GridFSFile file, FetchType fetchType) throws IOException {
	AssociatedDocument.Builder aBuilder = AssociatedDocument.newBuilder();
	aBuilder.setFilename(file.getFilename());
	Document metadata = file.getMetadata();

	boolean compressed = false;
	if (metadata.containsKey(COMPRESSED_FLAG)) {
		compressed = (boolean) metadata.remove(COMPRESSED_FLAG);
	}

	long timestamp = (long) metadata.remove(TIMESTAMP);

	aBuilder.setCompressed(compressed);
	aBuilder.setTimestamp(timestamp);

	aBuilder.setDocumentUniqueId((String) metadata.remove(DOCUMENT_UNIQUE_ID_KEY));
	for (String field : metadata.keySet()) {
		aBuilder.addMetadata(Metadata.newBuilder().setKey(field).setValue((String) metadata.get(field)));
	}

	if (FetchType.FULL.equals(fetchType)) {

		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		gridFS.downloadToStream(file.getObjectId(), byteArrayOutputStream);
		byte[] bytes = byteArrayOutputStream.toByteArray();
		if (null != bytes) {
			if (compressed) {
				bytes = CommonCompression.uncompressZlib(bytes);
			}
			aBuilder.setDocument(ByteString.copyFrom(bytes));
		}
	}
	aBuilder.setIndexName(indexName);
	return aBuilder.build();
}
 
源代码6 项目: lumongo   文件: MongoDocumentStorage.java
public void getAssociatedDocuments(OutputStream outputstream, Document filter) throws IOException {
	Charset charset = Charset.forName("UTF-8");

	GridFSBucket gridFS = createGridFSConnection();
	GridFSFindIterable gridFSFiles = gridFS.find(filter);
	outputstream.write("{\n".getBytes(charset));
	outputstream.write(" \"associatedDocs\": [\n".getBytes(charset));

	boolean first = true;
	for (GridFSFile gridFSFile : gridFSFiles) {
		if (first) {
			first = false;
		}
		else {
			outputstream.write(",\n".getBytes(charset));
		}

		Document metadata = gridFSFile.getMetadata();

		String uniqueId = metadata.getString(DOCUMENT_UNIQUE_ID_KEY);
		String uniquieIdKeyValue = "  { \"uniqueId\": \"" + uniqueId + "\", ";
		outputstream.write(uniquieIdKeyValue.getBytes(charset));

		String filename = gridFSFile.getFilename();
		String filenameKeyValue = "\"filename\": \"" + filename + "\", ";
		outputstream.write(filenameKeyValue.getBytes(charset));

		Date uploadDate = gridFSFile.getUploadDate();
		String uploadDateKeyValue = "\"uploadDate\": {\"$date\":" + uploadDate.getTime() + "}";
		outputstream.write(uploadDateKeyValue.getBytes(charset));

		metadata.remove(TIMESTAMP);
		metadata.remove(COMPRESSED_FLAG);
		metadata.remove(DOCUMENT_UNIQUE_ID_KEY);
		metadata.remove(FILE_UNIQUE_ID_KEY);

		if (!metadata.isEmpty()) {
			String metaJson = metadata.toJson();
			String metaString = ", \"meta\": " + metaJson;
			outputstream.write(metaString.getBytes(charset));
		}

		outputstream.write(" }".getBytes(charset));

	}
	outputstream.write("\n ]\n}".getBytes(charset));
}