use of com.mongodb.gridfs.GridFSDBFile in project tutorials by eugenp.
the class GridFSLiveTest method givenFileWithMetadataExist_whenFindingFileById_thenFileWithMetadataIsFound.
@Test
public void givenFileWithMetadataExist_whenFindingFileById_thenFileWithMetadataIsFound() {
DBObject metaData = new BasicDBObject();
metaData.put("user", "alex");
InputStream inputStream = null;
String id = "";
try {
inputStream = new FileInputStream("src/main/resources/test.png");
id = gridFsTemplate.store(inputStream, "test.png", "image/png", metaData).getId().toString();
} catch (FileNotFoundException ex) {
logger.error("File not found", ex);
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException ex) {
logger.error("Failed to close", ex);
}
}
}
GridFSDBFile gridFSDBFile = gridFsTemplate.findOne(new Query(Criteria.where("_id").is(id)));
assertNotNull(gridFSDBFile);
assertNotNull(gridFSDBFile.getInputStream());
assertThat(gridFSDBFile.numChunks(), is(1));
assertThat(gridFSDBFile.containsField("filename"), is(true));
assertThat(gridFSDBFile.get("filename"), is("test.png"));
assertThat(gridFSDBFile.getId(), is(id));
assertThat(gridFSDBFile.keySet().size(), is(9));
assertNotNull(gridFSDBFile.getMD5());
assertNotNull(gridFSDBFile.getUploadDate());
assertNull(gridFSDBFile.getAliases());
assertNotNull(gridFSDBFile.getChunkSize());
assertThat(gridFSDBFile.getContentType(), is("image/png"));
assertThat(gridFSDBFile.getFilename(), is("test.png"));
assertThat(gridFSDBFile.getMetaData().get("user"), is("alex"));
}
use of com.mongodb.gridfs.GridFSDBFile in project play-cookbook by spinscale.
the class Application method getImages.
public static void getImages() {
List<GridFSDBFile> files = GridFsHelper.getFiles();
Map map = new HashMap();
map.put("items", files);
renderJSON(map, new GridFSSerializer());
}
use of com.mongodb.gridfs.GridFSDBFile in project beam by apache.
the class MongoDBGridFSIOTest method testWriteMessage.
@Test
public void testWriteMessage() throws Exception {
ArrayList<String> data = new ArrayList<>(100);
ArrayList<Integer> intData = new ArrayList<>(100);
for (int i = 0; i < 1000; i++) {
data.add("Message " + i);
}
for (int i = 0; i < 100; i++) {
intData.add(i);
}
pipeline.apply("String", Create.of(data)).apply("StringInternal", MongoDbGridFSIO.write().withUri("mongodb://localhost:" + port).withDatabase(DATABASE).withChunkSize(100L).withBucket("WriteTest").withFilename("WriteTestData"));
pipeline.apply("WithWriteFn", Create.of(intData)).apply("WithWriteFnInternal", MongoDbGridFSIO.<Integer>write((output, outStream) -> {
// one byte per output
outStream.write(output.byteValue());
}).withUri("mongodb://localhost:" + port).withDatabase(DATABASE).withBucket("WriteTest").withFilename("WriteTestIntData"));
pipeline.run();
MongoClient client = null;
try {
StringBuilder results = new StringBuilder();
client = new MongoClient("localhost", port);
DB database = client.getDB(DATABASE);
GridFS gridfs = new GridFS(database, "WriteTest");
List<GridFSDBFile> files = gridfs.find("WriteTestData");
assertTrue(files.size() > 0);
for (GridFSDBFile file : files) {
assertEquals(100, file.getChunkSize());
int l = (int) file.getLength();
try (InputStream ins = file.getInputStream()) {
DataInputStream dis = new DataInputStream(ins);
byte[] b = new byte[l];
dis.readFully(b);
results.append(new String(b, StandardCharsets.UTF_8));
}
}
String dataString = results.toString();
for (int x = 0; x < 1000; x++) {
assertTrue(dataString.contains("Message " + x));
}
files = gridfs.find("WriteTestIntData");
boolean[] intResults = new boolean[100];
for (GridFSDBFile file : files) {
int l = (int) file.getLength();
try (InputStream ins = file.getInputStream()) {
DataInputStream dis = new DataInputStream(ins);
byte[] b = new byte[l];
dis.readFully(b);
for (byte aB : b) {
intResults[aB] = true;
}
}
}
for (int x = 0; x < 100; x++) {
assertTrue("Did not get a result for " + x, intResults[x]);
}
} finally {
if (client != null) {
client.close();
}
}
}
use of com.mongodb.gridfs.GridFSDBFile in project pmph by BCSquad.
the class CmsContentServiceImpl method getCmsContentAndContentAndAttachmentById.
@Override
public Map<String, Object> getCmsContentAndContentAndAttachmentById(Long id) throws CheckedServiceException {
Map<String, Object> resultMap = new HashMap<String, Object>();
if (ObjectUtil.isNull(id)) {
throw new CheckedServiceException(CheckedExceptionBusiness.CMS, CheckedExceptionResult.NULL_PARAM, "参数为空");
}
// 按id 获取CmsContent对象
CmsContent cmsContent = cmsContentDao.getCmsContentById(id);
String authDate = cmsContent.getAuthDate();
if (StringUtil.notEmpty(authDate)) {
cmsContent.setAuthDate(DateUtil.date2Str(DateUtil.str2Date(authDate)));
}
String deadlinePromote = cmsContent.getDeadlinePromote();
if (StringUtil.notEmpty(deadlinePromote)) {
cmsContent.setDeadlinePromote(DateUtil.date2Str(DateUtil.str2Date(deadlinePromote)));
}
String deadlineStick = cmsContent.getDeadlineStick();
if (StringUtil.notEmpty(deadlineStick)) {
cmsContent.setDeadlineStick(DateUtil.date2Str(DateUtil.str2Date(deadlineStick)));
}
String deadlineHot = cmsContent.getDeadlineHot();
if (StringUtil.notEmpty(deadlineHot)) {
cmsContent.setDeadlineHot(DateUtil.date2Str(DateUtil.str2Date(deadlineHot)));
}
resultMap.put("cmsContent", cmsContent);
// 判断内容是否已经发布或审核通过
String fileDownLoadType = null;
if (cmsContent.getIsPublished() || Const.CMS_AUTHOR_STATUS_2.shortValue() == cmsContent.getAuthStatus().shortValue()) {
fileDownLoadType = Const.CMS_FILE_DOWNLOAD;
} else {
fileDownLoadType = Const.FILE_DOWNLOAD;
}
// 按mid 获取Content对象
Content content = contentService.get(cmsContent.getMid());
if (ObjectUtil.isNull(content)) {
content = new Content();
content.setId(cmsContent.getMid());
content.setContent("");
}
resultMap.put("content", content);
// 按contentId 获取CMS内容附件
List<CmsExtra> cmsExtras = cmsExtraService.getCmsExtraByContentId(id);
List<CmsExtra> cmsList = new ArrayList<CmsExtra>(cmsExtras.size());
for (CmsExtra cmsExtra : cmsExtras) {
String attachment = cmsExtra.getAttachment();
if (!attachment.equals(cmsContent.getCover())) {
// 拼接附件下载路径
cmsExtra.setAttachment(fileDownLoadType + attachment);
cmsList.add(cmsExtra);
}
}
resultMap.put("cmsExtras", cmsList);
// 根据MaterialId 获取教材备注附件
List<MaterialNoteAttachment> materialNoteAttachments = null;
if (cmsContent.getIsMaterialEntry()) {
MaterialExtra materialExtra = materialExtraService.getMaterialExtraByMaterialId(cmsContent.getMaterialId());
if (ObjectUtil.notNull(materialExtra)) {
// 教材备注附件
materialNoteAttachments = materialNoteAttachmentService.getMaterialNoteAttachmentByMaterialExtraId(materialExtra.getId());
}
}
resultMap.put("MaterialNoteAttachment", materialNoteAttachments);
if (Const.CMS_CATEGORY_ID_1.longValue() == cmsContent.getCategoryId().longValue()) {
// 文章封面图片
CmsExtra cmsExtra = cmsExtraService.getCmsExtraByAttachment(cmsContent.getCover());
String imgFileName = "默认封面.png";
String imgFilePath = RouteUtil.DEFAULT_USER_AVATAR;
if (ObjectUtil.notNull(cmsExtra)) {
imgFileName = cmsExtra.getAttachmentName();
} else {
GridFSDBFile file = fileService.get(cmsContent.getCover());
if (ObjectUtil.notNull(file)) {
imgFileName = file.getFilename();
}
}
resultMap.put("imgFileName", imgFileName);
if (!"DEFAULT".equals(cmsContent.getCover())) {
imgFilePath = cmsContent.getCover();
}
resultMap.put("imgFilePath", imgFilePath);
}
return resultMap;
}
use of com.mongodb.gridfs.GridFSDBFile in project pmph by BCSquad.
the class ImageController method avatar.
/**
* 图片显示
*
* @param id 图片在MongoDB中的id
* @param response 服务响应
*/
@SuppressWarnings({ "rawtypes" })
@RequestMapping(value = "/image/{id}", method = RequestMethod.GET)
public ResponseBean avatar(@PathVariable("id") String id, HttpServletResponse response) {
response.setContentType("image/png");
GridFSDBFile file = fileService.get(id);
if (null == file) {
// logger.warn("未找到id为'{}'的图片文件", id);
return new ResponseBean(new CheckedServiceException(CheckedExceptionBusiness.FILE, CheckedExceptionResult.NULL_PARAM, "未找到id为" + id + "的图片文件"));
}
try (OutputStream out = response.getOutputStream()) {
file.writeTo(out);
out.flush();
out.close();
} catch (IOException ex) {
return new ResponseBean(ex);
// logger.error("文件下载时出现IO异常:{}", ex.getMessage());
} catch (Exception ex) {
logger.warn("图片查看时出现异常:{}", ex.getMessage());
throw new CheckedServiceException(CheckedExceptionBusiness.TEACHER_CHECK, CheckedExceptionResult.OBJECT_NOT_FOUND, "图片不存在");
// ResponseBean responseBean = new ResponseBean(ex);
// responseBean.setMsg("图片不存在");
// responseBean.setCode(ResponseBean.MONGO_EXCEPTION);
// return responseBean;
}
return new ResponseBean();
}
Aggregations