/** * This method first calls saveChunks(long) if the file data has not been saved yet. Then it persists the file entry to GridFS. * * @param chunkSize Size of chunks for file in bytes. * @throws MongoException if there's a problem saving the file. */ public void save(final long chunkSize) { if (outputStream != null) { throw new MongoException("cannot mix OutputStream and regular save()"); } // note that chunkSize only changes chunkSize in case we actually save chunks // otherwise there is a risk file and chunks are not compatible if (!savedChunks) { try { saveChunks(chunkSize); } catch (IOException ioe) { throw new MongoException("couldn't save chunks", ioe); } } super.save(); }
@Override @Deprecated public boolean containsKey(final String key) { return containsField(key); }
public static DBObject applyFieldFilter(DBObject object, final Set<String> includeFields, final Set<String> excludeFields) { if (object instanceof GridFSFile) { GridFSFile file = (GridFSFile) object; DBObject metadata = file.getMetaData(); if (metadata != null) { file.setMetaData(applyFieldFilter(metadata, includeFields, excludeFields)); } } else { object = MongoDBHelper.applyExcludeFields(object, excludeFields); object = MongoDBHelper.applyIncludeFields(object, includeFields); } return object; } }
FileInfo(final GridFSFile savedFile,final boolean withInputStream) { this.md5 = savedFile.getMD5(); this.fileId = (ObjectId)savedFile.getId(); this.contentType = savedFile.getContentType(); this.fileSize = FileUtils.readableFileSize(savedFile.getLength()); this.storeName = savedFile.getFilename(); this.savedDate = savedFile.getUploadDate(); this.fileSizeBytes = savedFile.getLength(); if (withInputStream && savedFile instanceof GridFSDBFile) { this.inputStream = ((GridFSDBFile)savedFile).getInputStream(); } attributes=new HashMap<>(); }
public static ContentFile convert(GridFSFile fsFile) { ContentFile contentFile = new ContentFile(); contentFile.setFileId(fsFile.getId().toString()); contentFile.setFilename(fsFile.getFilename()); contentFile.setLength(fsFile.getLength()); contentFile.setMd5(fsFile.getMD5()); contentFile.setRequestDate((Date) fsFile.getMetaData().get(ContentFile.REQUEST_DATE)); contentFile.setThesisMetadataId((String) fsFile.getMetaData().get(ContentFile.THESIS_METADATA_ID)); return contentFile; }
@Override public MongoFileRef createFile(String id, InputStream content) { GridFSFile fsFile = gridfsAccessor.getGridFs().createFile(content, id, true); fsFile.save(); try { content.close(); } catch (final IOException e) { throw new GeneralServiceException(e, "Could not save binary data for path {}", id); } return new MongoFileRef(new DBRef(gridfsAccessor.getCollection().getName(), fsFile.getId()), fsFile.getLength()); }
/** * 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(); } } } }
private Date findObsoletedDate(GridFSFile toObsolete, List<? extends GridFSFile> files) { if (getType(toObsolete).isEmpty()) { log.warn("file {} (id={}) has no type", toObsolete.getFilename(), toObsolete.getId()); } else { for (GridFSFile file : files) { if (obsoletes(file, toObsolete)) { return file.getUploadDate(); } } } return null; }
private static RepositoryItemAttributes loadAttributes(GridFSFile file) { RepositoryItemAttributes attributes = new RepositoryItemAttributes(); attributes.setContentLength(file.getLength()); attributes.setLastModified(file.getUploadDate().getTime()); attributes.setMimeType(file.getContentType()); return attributes; }
private MongoRepositoryItem(MongoRepository repository, GridFSFile dbFile, State state) { super(dbFile.getId().toString(), state, loadAttributes(dbFile), repository); this.dbFile = dbFile; // don't call ours setMetadata(...) super.setMetadata(new HashMap<String, String>()); }
private void putMetadataInGridFS(boolean save) { DBObject metadataDBO = new BasicDBObject(); for (Entry<String, String> entry : metadata.entrySet()) { metadataDBO.put(entry.getKey(), entry.getValue()); } dbFile.setMetaData(metadataDBO); if (save) { dbFile.save(); } }
protected void refreshAttributesOnClose() { dbFile = ((MongoRepository) repository).getGridFS().findOne(getId()); if (dbFile == null) { throw new KurentoException("Grid object not found for id " + getId()); } state = State.STORED; attributes.setContentLength(dbFile.getLength()); }
public int compare(GridFSFile fl, GridFSFile fr) { return fl.getUploadDate().compareTo(fr.getUploadDate()); } }));
@Override public boolean containsField(final String s) { return keySet().contains(s); }
private String getType(GridFSFile file) { Object o = file.getMetaData().get(PROP_TYPE); if (o instanceof String) { return (String) o; } else { return ""; } }
private static RepositoryItemAttributes loadAttributes(GridFSFile file) { RepositoryItemAttributes attributes = new RepositoryItemAttributes(); attributes.setContentLength(file.getLength()); attributes.setLastModified(file.getUploadDate().getTime()); attributes.setMimeType(file.getContentType()); return attributes; }
private MongoRepositoryItem(MongoRepository repository, GridFSFile dbFile, State state) { super(dbFile.getId().toString(), state, loadAttributes(dbFile), repository); this.dbFile = dbFile; // don't call ours setMetadata(...) super.setMetadata(new HashMap<String, String>()); }
private void putMetadataInGridFS(boolean save) { DBObject metadataDBO = new BasicDBObject(); for (Entry<String, String> entry : metadata.entrySet()) { metadataDBO.put(entry.getKey(), entry.getValue()); } dbFile.setMetaData(metadataDBO); if (save) { dbFile.save(); } }
protected void refreshAttributesOnClose() { dbFile = ((MongoRepository) repository).getGridFS().findOne(getId()); if (dbFile == null) { throw new KurentoException("Grid object not found for id " + getId()); } state = State.STORED; attributes.setContentLength(dbFile.getLength()); }
private boolean obsoletesMain(GridFSFile file, GridFSFile toObsolete) { return getType(file).equals(getType(toObsolete)) && file.getUploadDate().after(toObsolete.getUploadDate()); }