public ObjectId store(InputStream content, String filename) { return store(content, filename, (Object) null); }
public GridFsResource[] getResources(String locationPattern) { if (!StringUtils.hasText(locationPattern)) { return new GridFsResource[0]; } AntPath path = new AntPath(locationPattern); if (path.isPattern()) { GridFSFindIterable files = find(query(whereFilename().regex(path.toRegex()))); List<GridFsResource> resources = new ArrayList<>(); for (GridFSFile file : files) { resources.add(getResource(file)); } return resources.toArray(new GridFsResource[0]); } return new GridFsResource[] { getResource(locationPattern) }; }
public GridFsResource getResource(String location) { return Optional.ofNullable(findOne(query(whereFilename().is(location)))) // .map(this::getResource) // .orElseGet(() -> GridFsResource.absent(location)); }
public ObjectId store(InputStream content, @Nullable String filename, @Nullable String contentType, @Nullable Object metadata) { return store(content, filename, contentType, toDocument(metadata)); }
@Override public void setContent(S property, InputStream content) { Object contentId = BeanUtils.getFieldWithAnnotation(property, ContentId.class); if (contentId == null) { contentId = UUID.randomUUID(); BeanUtils.setFieldWithAnnotation(property, ContentId.class, contentId.toString()); } String location = converter.convert(contentId, String.class); Resource resource = gridFs.getResource(location); if (resource != null && resource.exists()) { gridFs.delete(query(whereFilename().is(resource.getFilename()))); } try { gridFs.store(content, location); resource = gridFs.getResource(location); } catch (Exception e) { logger.error(format("Unexpected error setting content for entity %s", property), e); throw new StoreAccessException(format("Setting content for entity %s", property), e); } long contentLen = 0L; try { contentLen = resource.contentLength(); } catch (IOException ioe) { logger.debug(format("Unable to retrieve content length for %s", contentId)); } BeanUtils.setFieldWithAnnotation(property, ContentLength.class, contentLen); }
public class MyDocumentRepositoryImpl implements MyDocumentRepositoryCustom { public static final String MONGO_ID = "_id"; @Autowired GridFsTemplate gridFsTemplate; @Override public void linkFileToMyDoc(MyDocument myDocument, InputStream stream, String fileName) { GridFSFile fsFile = gridFsTemplate.store(stream, fileName); myDocument.setFile( (ObjectId) fsFile.getId()); } @Override public void unLinkFileToMyDoc(MyDocument myDocument) { ObjectId objectId = myDocument.getFile(); if (null != objectId) { gridFsTemplate.delete( Query.query(Criteria.where(MONGO_ID).is(objectId)) ); myDocument.setFile(null); } } }
@RequestMapping(method = RequestMethod.GET) ResponseEntity<Resource> get(String userId) { HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setContentType(MediaType.IMAGE_JPEG); return new ResponseEntity<>( this.gridFsTemplate.getResource(userId), httpHeaders, HttpStatus.OK); } }
Resource resource = gridFs.getResource(location); if (resource != null && resource.exists()) { gridFs.delete(query(whereFilename().is(resource.getFilename())));
@Override public void deleteContentFiles(String thesisMetadataId) throws IOException { gridFsTemplate.delete(metadataFilesQuery(thesisMetadataId)); }
public InputStream getInputStream() throws IOException, IllegalStateException { GridFSFile file = gridfs.findOne(query(whereFilename().is(location))); if (file == null) { return null; } return gridfs.getResource(location).getInputStream(); }
@Bean @Qualifier("ResourceSync") public GridFsTemplate resourceSyncGridFsTemplate() throws Exception { return new GridFsTemplate(mongoDbFactory(), mappingMongoConverter(), GRID_FS_RESOURCESYNC_BUCKET); }
/** * Removes obsolete versions of files from the repository. * <p> * If a file already has an 'obsoleted' timestamp that is old enough it is deleted. * If a file does not yet have a 'obsoleted' timestamp and there exists a newer main * file of the same type it gets the timestamp and will be deleted at some later point. */ @Scheduled(fixedDelay=1000*3600) void cleanup() { long now = System.currentTimeMillis(); List<GridFSDBFile> files = fs.find(new Query()); for (GridFSFile f : files) { Date obs = (Date) f.getMetaData().get(PROP_OBSOLETED); if (obs != null && now - obs.getTime() > MAX_AGE) { fs.delete(query(where("_id").is(f.getId()))); } else { obs = findObsoletedDate(f, files); if (obs != null) { f.getMetaData().put(PROP_OBSOLETED, obs); f.save(); } } } }
public GridFSFile findOne(Query query) { return find(query).first(); }
@Override public byte[] getImage(String filename) { GridFsResource gridFsResource = gridFsTemplate.getResource(filename); if (gridFsResource != null) { try { return IOUtils.toByteArray(gridFsResource.getInputStream()); } catch (IOException e) { throw new RuntimeException(e); } } return null; }
@Override public void deleteAllImages() { gridFsTemplate.delete(new Query()); } }
@Bean public GridFsTemplate gridFsTemplate() throws Exception { return new GridFsTemplate(mongoDbFactory(), mappingMongoConverter(), "blogfs"); } }
public GridFSFile findOne(Query query) { return find(query).first(); }