Search in sources :

Example 16 with GridFSDBFile

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"));
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) Query(org.springframework.data.mongodb.core.query.Query) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) GridFSDBFile(com.mongodb.gridfs.GridFSDBFile) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 17 with GridFSDBFile

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());
}
Also used : GridFSSerializer(utils.GridFSSerializer) HashMap(java.util.HashMap) GridFSDBFile(com.mongodb.gridfs.GridFSDBFile) Map(java.util.Map) HashMap(java.util.HashMap)

Example 18 with GridFSDBFile

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();
        }
    }
}
Also used : Count(org.apache.beam.sdk.transforms.Count) LoggerFactory(org.slf4j.LoggerFactory) GridFSInputFile(com.mongodb.gridfs.GridFSInputFile) Scanner(java.util.Scanner) Random(java.util.Random) ByteArrayInputStream(java.io.ByteArrayInputStream) Create(org.apache.beam.sdk.transforms.Create) IMongodConfig(de.flapdoodle.embed.mongo.config.IMongodConfig) ClassRule(org.junit.ClassRule) KvCoder(org.apache.beam.sdk.coders.KvCoder) AfterClass(org.junit.AfterClass) GridFSDBFile(com.mongodb.gridfs.GridFSDBFile) StandardCharsets(java.nio.charset.StandardCharsets) GridFS(com.mongodb.gridfs.GridFS) List(java.util.List) Max(org.apache.beam.sdk.transforms.Max) Network(de.flapdoodle.embed.process.runtime.Network) DB(com.mongodb.DB) NetworkTestHelper(org.apache.beam.sdk.io.common.NetworkTestHelper) DataInputStream(java.io.DataInputStream) KV(org.apache.beam.sdk.values.KV) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BeforeClass(org.junit.BeforeClass) Net(de.flapdoodle.embed.mongo.config.Net) Duration(org.joda.time.Duration) RunWith(org.junit.runner.RunWith) MongodExecutable(de.flapdoodle.embed.mongo.MongodExecutable) PipelineOptionsFactory(org.apache.beam.sdk.options.PipelineOptionsFactory) MongodConfigBuilder(de.flapdoodle.embed.mongo.config.MongodConfigBuilder) ArrayList(java.util.ArrayList) StringUtf8Coder(org.apache.beam.sdk.coders.StringUtf8Coder) Version(de.flapdoodle.embed.mongo.distribution.Version) TestPipeline(org.apache.beam.sdk.testing.TestPipeline) OutputStreamWriter(java.io.OutputStreamWriter) Storage(de.flapdoodle.embed.mongo.config.Storage) PipelineOptions(org.apache.beam.sdk.options.PipelineOptions) OutputStream(java.io.OutputStream) SourceTestUtils(org.apache.beam.sdk.testing.SourceTestUtils) Logger(org.slf4j.Logger) PAssert(org.apache.beam.sdk.testing.PAssert) MongoCmdOptionsBuilder(de.flapdoodle.embed.mongo.config.MongoCmdOptionsBuilder) Assert.assertTrue(org.junit.Assert.assertTrue) BoundedGridFSSource(org.apache.beam.sdk.io.mongodb.MongoDbGridFSIO.Read.BoundedGridFSSource) Test(org.junit.Test) JUnit4(org.junit.runners.JUnit4) PCollection(org.apache.beam.sdk.values.PCollection) InputStreamReader(java.io.InputStreamReader) MongodProcess(de.flapdoodle.embed.mongo.MongodProcess) BoundedSource(org.apache.beam.sdk.io.BoundedSource) Rule(org.junit.Rule) MongoClient(com.mongodb.MongoClient) MongodStarter(de.flapdoodle.embed.mongo.MongodStarter) Instant(org.joda.time.Instant) VarIntCoder(org.apache.beam.sdk.coders.VarIntCoder) ObjectId(org.bson.types.ObjectId) BufferedReader(java.io.BufferedReader) Assert.assertEquals(org.junit.Assert.assertEquals) TemporaryFolder(org.junit.rules.TemporaryFolder) InputStream(java.io.InputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) DataInputStream(java.io.DataInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) GridFS(com.mongodb.gridfs.GridFS) DataInputStream(java.io.DataInputStream) MongoClient(com.mongodb.MongoClient) GridFSDBFile(com.mongodb.gridfs.GridFSDBFile) DB(com.mongodb.DB) Test(org.junit.Test)

Example 19 with GridFSDBFile

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;
}
Also used : CmsContent(com.bc.pmpheep.back.po.CmsContent) HashMap(java.util.HashMap) MaterialNoteAttachment(com.bc.pmpheep.back.po.MaterialNoteAttachment) ArrayList(java.util.ArrayList) CheckedServiceException(com.bc.pmpheep.service.exception.CheckedServiceException) CmsExtra(com.bc.pmpheep.back.po.CmsExtra) CmsContent(com.bc.pmpheep.back.po.CmsContent) Content(com.bc.pmpheep.general.po.Content) GridFSDBFile(com.mongodb.gridfs.GridFSDBFile) MaterialExtra(com.bc.pmpheep.back.po.MaterialExtra)

Example 20 with GridFSDBFile

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();
}
Also used : GridFSDBFile(com.mongodb.gridfs.GridFSDBFile) OutputStream(java.io.OutputStream) ResponseBean(com.bc.pmpheep.controller.bean.ResponseBean) CheckedServiceException(com.bc.pmpheep.service.exception.CheckedServiceException) IOException(java.io.IOException) IOException(java.io.IOException) CheckedServiceException(com.bc.pmpheep.service.exception.CheckedServiceException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

GridFSDBFile (com.mongodb.gridfs.GridFSDBFile)20 BasicDBObject (com.mongodb.BasicDBObject)8 InputStream (java.io.InputStream)8 DBObject (com.mongodb.DBObject)6 CheckedServiceException (com.bc.pmpheep.service.exception.CheckedServiceException)5 FileNotFoundException (java.io.FileNotFoundException)5 IOException (java.io.IOException)5 Test (org.junit.Test)4 GridFS (com.mongodb.gridfs.GridFS)3 GridFSInputFile (com.mongodb.gridfs.GridFSInputFile)3 FileInputStream (java.io.FileInputStream)3 OutputStream (java.io.OutputStream)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 LogDetail (com.bc.pmpheep.annotation.LogDetail)2 ResponseBean (com.bc.pmpheep.controller.bean.ResponseBean)2 Content (com.bc.pmpheep.general.po.Content)2 DBCollection (com.mongodb.DBCollection)2 DBCursor (com.mongodb.DBCursor)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2