@Override public void deleteAllImages() { gridFsTemplate.delete(new Query()); } }
@Override public void deleteContentFiles(String thesisMetadataId) throws IOException { gridFsTemplate.delete(metadataFilesQuery(thesisMetadataId)); }
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); } } }
@Override public void deleteContentFiles(Collection<String> fileIds) { gridFsTemplate.delete(query(where("_id").in(fileIds))); }
protected void deleteOnFirstWrite() { if (firstWrite) { try { gridfs.delete(query(whereFilename().is(resource.getFilename()))); } finally { firstWrite = false; } } }
@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); }
Resource resource = gridFs.getResource(location); if (resource != null && resource.exists()) { gridFs.delete(query(whereFilename().is(resource.getFilename())));
@Override public void delete() { if (gridfs.findOne(query(whereFilename().is(location))) == null) { return; } gridfs.delete(query(whereFilename().is(location))); } }
/** * 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 void clean() { // cannot drop the collections because dropping removes indexes that are rebuild only on // application context start mongoTemplate.remove(new Query(), ThesisDepositRequest.class); mongoTemplate.remove(new Query(), ThesisMetadata.class); mongoTemplate.remove(new Query(), Institution.class); mongoTemplate.remove(new Query(), ThesisTempLink.class); gridFsTemplate.delete(query(new Criteria())); } }