1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
| @Service public class HugeFileService { @Autowired private MongoTemplate mongoTemplate;
@Autowired private GridFsTemplate gridFsTemplate;
@Autowired private GridFSBucket gridFSBucket;
public HugeFileEntity fileAdd(MultipartFile uploadFile) throws IOException { if (uploadFile == null) { return null; } String fileName = uploadFile.getOriginalFilename(); Builder<HugeFileEntity> entityBuilder = Builder.of(HugeFileEntity::new); entityBuilder .with(HugeFileEntity::setContentType, uploadFile.getContentType()) .with(HugeFileEntity::setFileType, StrUtil.subAfter(fileName, ".", true)) .with(HugeFileEntity::setCreationDate, new Date()) .with(HugeFileEntity::setName, fileName) .with(HugeFileEntity::setSize, uploadFile.getSize()) .with(HugeFileEntity::setLastUpdateTime, new Date()) .with(HugeFileEntity::setMd5, MD5.create().digestHex(uploadFile.getInputStream())); String gridFsId = this.uploadFile(uploadFile.getInputStream(), uploadFile.getContentType()); entityBuilder.with(HugeFileEntity::setGridFsId, gridFsId); return mongoTemplate.save(entityBuilder.build()); }
public String uploadFile(InputStream inputStream, String contentType) { String fileName = IdUtil.objectId(); return gridFsTemplate.store(inputStream, fileName, contentType).toString(); }
public void deleteFile(String id) { HugeFileEntity entity = mongoTemplate.findById(id, HugeFileEntity.class); if (entity != null) { Query deleteFileQuery = new Query().addCriteria(Criteria.where("_id").is(entity.getGridFsId())); gridFsTemplate.delete(deleteFileQuery); Query query = new Query(Criteria.where("id").is(entity.getId())); mongoTemplate.remove(query, HugeFileEntity.class); } }
public Optional<HugeFileEntity> findFileById(String id, boolean useContent) throws IOException { HugeFileEntity entity = mongoTemplate.findById(id, HugeFileEntity.class); if (entity == null) { return Optional.empty(); } if (!useContent) { return Optional.of(entity); } Query gridFsQuery = new Query().addCriteria(Criteria.where("_id").is(entity.getGridFsId())); GridFSFile file = gridFsTemplate.findOne(gridFsQuery); GridFSDownloadStream ins = gridFSBucket.openDownloadStream(file.getObjectId()); if (ins.getGridFSFile().getLength() <= 0) { return Optional.empty(); } GridFsResource resource = new GridFsResource(file, ins); entity.setContent(IoUtil.readBytes(resource.getInputStream())); return Optional.of(entity); }
public Optional<HugeFileEntity> findFileById(String id) throws IOException { return this.findFileById(id, false); }
public List<HugeFileEntity> queryPage(int pageNum, int pageSize) { Query query = new Query().with(Sort.by(Sort.Direction.DESC, "creationDate")); long offset = (long) (pageNum - 1) * pageSize; query.skip(offset); query.limit(pageSize); Field field = query.fields(); field.exclude("content"); return mongoTemplate.find(query, HugeFileEntity.class); } }
|