Search in sources :

Example 11 with GridFSDBFile

use of com.mongodb.gridfs.GridFSDBFile in project pmph by BCSquad.

the class FileDownLoadController method download.

/**
 * 小组文件下载
 *
 * @param id       图片在MongoDB中的id
 * @param groupId  小组id
 * @param response 服务响应
 * @return ResponseBean对象
 */
@LogDetail(businessType = BUSSINESS_TYPE, logRemark = "小组文件下载")
@RequestMapping(value = "/groupfile/download/{id}", method = RequestMethod.GET)
public ResponseBean download(@PathVariable("id") String id, @RequestParam("groupId") long groupId, HttpServletRequest request, HttpServletResponse response) {
    if (groupId < 1) {
        throw new CheckedServiceException(CheckedExceptionBusiness.FILE, CheckedExceptionResult.FILE_DOWNLOAD_FAILED, "小组id错误(负数或零)");
    }
    response.setCharacterEncoding("utf-8");
    response.setContentType("application/force-download");
    GridFSDBFile file = fileService.get(id);
    if (null == file) {
        logger.warn("未找到id为'{}'的文件", id);
        throw new CheckedServiceException(CheckedExceptionBusiness.FILE, CheckedExceptionResult.FILE_DOWNLOAD_FAILED, "未找到对应文件");
    }
    String fileName = groupFileService.getFileName(id);
    fileName = returnFileName(request, fileName);
    response.setHeader("Content-Disposition", "attachment;fileName=" + fileName);
    try (OutputStream out = response.getOutputStream()) {
        file.writeTo(out);
        out.flush();
        out.close();
        return new ResponseBean(groupFileService.updatePmphGroupFileOfDown(groupId, id));
    } catch (IOException ex) {
        logger.warn("文件下载时出现IO异常:{}", ex.getMessage());
        throw new CheckedServiceException(CheckedExceptionBusiness.FILE, CheckedExceptionResult.FILE_DOWNLOAD_FAILED, "文件在传输时中断");
    }
}
Also used : GridFSDBFile(com.mongodb.gridfs.GridFSDBFile) ServletOutputStream(javax.servlet.ServletOutputStream) CheckedServiceException(com.bc.pmpheep.service.exception.CheckedServiceException) ResponseBean(com.bc.pmpheep.controller.bean.ResponseBean) LogDetail(com.bc.pmpheep.annotation.LogDetail) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 12 with GridFSDBFile

use of com.mongodb.gridfs.GridFSDBFile in project pmph by BCSquad.

the class FileDownLoadController method download.

/**
 * <pre>
 * 功能描述:普通文件下载(更新下载数)
 * 使用示范:
 *
 * &#64;param type 模块类型
 * &#64;param id 文件在MongoDB中的id
 * &#64;param response 服务响应
 * </pre>
 *
 * @throws UnsupportedEncodingException
 */
@LogDetail(businessType = BUSSINESS_TYPE, logRemark = "普通文件下载(更新下载数)")
@RequestMapping(value = "/file/{type}/download/{id}", method = RequestMethod.GET)
public void download(@PathVariable("type") String type, @PathVariable("id") String id, HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
    response.setCharacterEncoding("utf-8");
    response.setContentType("application/force-download");
    GridFSDBFile file = fileService.get(id);
    if (null == file) {
        logger.warn("未找到id为'{}'的文件", id);
        return;
    }
    String fileName = returnFileName(request, file.getFilename());
    response.setHeader("Content-Disposition", "attachment;fileName=" + fileName);
    try (OutputStream out = response.getOutputStream()) {
        file.writeTo(out);
        out.flush();
        out.close();
        if (Const.CMS_TYPE.equals(type)) {
            // CMS附件
            cmsExtraService.updateCmsExtraDownLoadCountsByAttachment(id);
        }
        if (Const.MATERIAL_NOTICE_TYPE.equals(type)) {
            // 教材通知
            materialNoticeAttachmentService.updateMaterialNoticeAttachmentDownLoadCountsByAttachment(id);
        }
        if (Const.MATERIAL_NOTE_TYPE.equals(type)) {
            // 教材备注
            materialNoteAttachmentService.updateMaterialNoteAttachmentDownLoadCountsByAttachment(id);
        }
        if (Const.CLINICAL_DECISION.equals(type)) {
            // 临床决断
            productService.updateProductAttachmenDownLoadCountsByAttachment(id);
        }
    } catch (IOException ex) {
        logger.error("文件下载时出现IO异常:{}", ex.getMessage());
    }
}
Also used : GridFSDBFile(com.mongodb.gridfs.GridFSDBFile) ServletOutputStream(javax.servlet.ServletOutputStream) LogDetail(com.bc.pmpheep.annotation.LogDetail) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 13 with GridFSDBFile

use of com.mongodb.gridfs.GridFSDBFile in project pmph by BCSquad.

the class ActivityManagementServiceImpl method getActivitDetailsyById.

@Override
public Map<String, Object> getActivitDetailsyById(Long id) {
    Map<String, Object> resultMap = new HashMap<String, Object>();
    if (ObjectUtil.isNull(id)) {
        throw new CheckedServiceException(CheckedExceptionBusiness.CMS, CheckedExceptionResult.NULL_PARAM, "参数为空");
    }
    // 按id 获取Activity对象
    Activity activityById = activityManagementDao.getActivityById(id);
    resultMap.put("activity", activityById);
    // 按ActivityDescCmsId 获取Content对象
    Content content = contentService.get(activityById.getActivityDescCmsId());
    if (ObjectUtil.isNull(content)) {
        content = new Content();
        content.setId(activityById.getActivityDescCmsId());
        content.setContent("");
    }
    resultMap.put("content", content);
    if (ObjectUtil.notNull(activityById.getMaterialId())) {
        Material materialById = materialDao.getMaterialById(activityById.getMaterialId());
        resultMap.put("material", materialById);
    }
    if (ObjectUtil.notNull(activityById.getInfoExpressCmsId())) {
        CmsContent cmsContentById = cmsContentDao.getCmsContentById(activityById.getInfoExpressCmsId());
        resultMap.put("infoExpress", cmsContentById);
    }
    if (StringUtil.notEmpty(activityById.getCover())) {
        String imgFileName = "默认封面.png";
        String imgFilePath = RouteUtil.DEFAULT_USER_AVATAR;
        // 文章封面图片
        GridFSDBFile file = null;
        if (activityById.getCover() != null) {
            file = fileService.get(activityById.getCover());
        }
        if (ObjectUtil.notNull(file)) {
            imgFileName = file.getFilename();
        }
        resultMap.put("imgFileName", imgFileName);
        if (!"DEFAULT".equals(activityById.getCover())) {
            imgFilePath = activityById.getCover();
        }
        resultMap.put("imgFilePath", imgFilePath);
    }
    return resultMap;
}
Also used : HashMap(java.util.HashMap) Content(com.bc.pmpheep.general.po.Content) GridFSDBFile(com.mongodb.gridfs.GridFSDBFile) CheckedServiceException(com.bc.pmpheep.service.exception.CheckedServiceException)

Example 14 with GridFSDBFile

use of com.mongodb.gridfs.GridFSDBFile in project camel by apache.

the class GridFsProducer method process.

public void process(Exchange exchange) throws Exception {
    String operation = endpoint.getOperation();
    if (operation == null) {
        operation = exchange.getIn().getHeader(GridFsEndpoint.GRIDFS_OPERATION, String.class);
    }
    if (operation == null || "create".equals(operation)) {
        final String filename = exchange.getIn().getHeader(Exchange.FILE_NAME, String.class);
        Long chunkSize = exchange.getIn().getHeader(GridFsEndpoint.GRIDFS_CHUNKSIZE, Long.class);
        InputStream ins = exchange.getIn().getMandatoryBody(InputStream.class);
        GridFSInputFile gfsFile = endpoint.getGridFs().createFile(ins, filename, true);
        if (chunkSize != null && chunkSize > 0) {
            gfsFile.setChunkSize(chunkSize);
        }
        final String ct = exchange.getIn().getHeader(Exchange.CONTENT_TYPE, String.class);
        if (ct != null) {
            gfsFile.setContentType(ct);
        }
        String metaData = exchange.getIn().getHeader(GridFsEndpoint.GRIDFS_METADATA, String.class);
        DBObject dbObject = (DBObject) JSON.parse(metaData);
        gfsFile.setMetaData(dbObject);
        gfsFile.save();
        //add headers with the id and file name produced by the driver.
        exchange.getIn().setHeader(Exchange.FILE_NAME_PRODUCED, gfsFile.getFilename());
        exchange.getIn().setHeader(GridFsEndpoint.GRIDFS_FILE_ID_PRODUCED, gfsFile.getId());
    } else if ("remove".equals(operation)) {
        final String filename = exchange.getIn().getHeader(Exchange.FILE_NAME, String.class);
        endpoint.getGridFs().remove(filename);
    } else if ("findOne".equals(operation)) {
        final String filename = exchange.getIn().getHeader(Exchange.FILE_NAME, String.class);
        GridFSDBFile file = endpoint.getGridFs().findOne(filename);
        if (file != null) {
            exchange.getIn().setHeader(GridFsEndpoint.GRIDFS_METADATA, JSON.serialize(file.getMetaData()));
            exchange.getIn().setHeader(Exchange.FILE_CONTENT_TYPE, file.getContentType());
            exchange.getIn().setHeader(Exchange.FILE_LENGTH, file.getLength());
            exchange.getIn().setHeader(Exchange.FILE_LAST_MODIFIED, file.getUploadDate());
            exchange.getIn().setBody(file.getInputStream(), InputStream.class);
        } else {
            throw new FileNotFoundException("No GridFS file for " + filename);
        }
    } else if ("listAll".equals(operation)) {
        final String filename = exchange.getIn().getHeader(Exchange.FILE_NAME, String.class);
        DBCursor cursor;
        if (filename == null) {
            cursor = endpoint.getGridFs().getFileList();
        } else {
            cursor = endpoint.getGridFs().getFileList(new BasicDBObject("filename", filename));
        }
        exchange.getIn().setBody(new DBCursorFilenameReader(cursor), Reader.class);
    } else if ("count".equals(operation)) {
        final String filename = exchange.getIn().getHeader(Exchange.FILE_NAME, String.class);
        DBCursor cursor;
        if (filename == null) {
            cursor = endpoint.getGridFs().getFileList();
        } else {
            cursor = endpoint.getGridFs().getFileList(new BasicDBObject("filename", filename));
        }
        exchange.getIn().setBody(cursor.count(), Integer.class);
    }
}
Also used : GridFSInputFile(com.mongodb.gridfs.GridFSInputFile) BasicDBObject(com.mongodb.BasicDBObject) DBCursor(com.mongodb.DBCursor) InputStream(java.io.InputStream) GridFSDBFile(com.mongodb.gridfs.GridFSDBFile) FileNotFoundException(java.io.FileNotFoundException) Reader(java.io.Reader) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject)

Example 15 with GridFSDBFile

use of com.mongodb.gridfs.GridFSDBFile in project commons by craftercms.

the class AbstractJongoRepository method listFilesByName.

@Override
public List<FileInfo> listFilesByName(final String filename) {
    final List<GridFSDBFile> files = gridfs.find(new BasicDBObject("filename", new BasicDBObject("$regex", ".*" + filename + ".*")));
    final ArrayList<FileInfo> toReturn = new ArrayList<FileInfo>();
    for (GridFSDBFile file : files) {
        toReturn.add(new FileInfo(file, false));
    }
    return toReturn;
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) GridFSDBFile(com.mongodb.gridfs.GridFSDBFile) ArrayList(java.util.ArrayList)

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