/** * Obtain an absent {@link GridFsResource}. * * @param filename filename of the absent resource, must not be {@literal null}. * @return never {@literal null}. * @since 2.1 */ public static GridFsResource absent(String filename) { Assert.notNull(filename, "Filename must not be null"); return new GridFsResource(filename); }
public GridFsResource getResource(String location) { return Optional.ofNullable(findOne(query(whereFilename().is(location)))) // .map(this::getResource) // .orElseGet(() -> GridFsResource.absent(location)); }
private void verifyExists() throws FileNotFoundException { if (!exists()) { throw new FileNotFoundException(String.format("%s does not exist.", getDescription())); } } }
@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 InputStream getInputStream() throws IOException, IllegalStateException { verifyExists(); return super.getInputStream(); }
@Override public String getDescription() { return String.format("GridFs resource [%s]", this.getFilename()); }
public Photo getDogePhoto(User user, String dogeId) throws IOException { DogePhoto dogePhoto = this.dogePhotoRepository.findOneByIdAndUser(dogeId, user); return () -> this.fs.getResource(dogePhoto.getFileRef()).getInputStream(); }
@Override public long lastModified() throws IOException { verifyExists(); return file.getUploadDate().getTime(); }
@Override public String getDescription() { return String.format("GridFs resource [%s]", this.getFilename()); }
public InputStream getInputStream() throws IOException, IllegalStateException { GridFSFile file = gridfs.findOne(query(whereFilename().is(location))); if (file == null) { return null; } return gridfs.getResource(location).getInputStream(); }
/** * Returns the {@link Resource}'s id. * * @return never {@literal null}. * @throws IllegalStateException if the file does not {@link #exists()}. */ public Object getId() { Assert.state(exists(), () -> String.format("%s does not exist.", getDescription())); return file.getId(); }
public GridFsResource getResource(GridFSFile file) { Assert.notNull(file, "GridFSFile must not be null!"); return new GridFsResource(file, getGridFs().openDownloadStream(file.getObjectId())); }
@Override public long contentLength() throws IOException { verifyExists(); return file.getLength(); }
public GridFsResource getResource(String location) { return Optional.ofNullable(findOne(query(whereFilename().is(location)))) // .map(this::getResource) // .orElseGet(() -> GridFsResource.absent(location)); }
/** * Returns the {@link Resource}'s content type. * * @return never {@literal null}. * @throws com.mongodb.MongoGridFSException in case no content type declared on {@link GridFSFile#getMetadata()} nor * provided via {@link GridFSFile#getContentType()}. * @throws IllegalStateException if the file does not {@link #exists()}. */ @SuppressWarnings("deprecation") public String getContentType() { Assert.state(exists(), () -> String.format("%s does not exist.", getDescription())); return Optionals .firstNonEmpty( () -> Optional.ofNullable(file.getMetadata()).map(it -> it.get(CONTENT_TYPE_FIELD, String.class)), () -> Optional.ofNullable(file.getContentType())) .orElseThrow(() -> new MongoGridFSException("No contentType data for this GridFS file")); }
/** * Obtain an absent {@link GridFsResource}. * * @param filename filename of the absent resource, must not be {@literal null}. * @return never {@literal null}. * @since 2.1 */ public static GridFsResource absent(String filename) { Assert.notNull(filename, "Filename must not be null"); return new GridFsResource(filename); }
@Override public long lastModified() throws IOException { verifyExists(); return file.getUploadDate().getTime(); }
private void verifyExists() throws FileNotFoundException { if (!exists()) { throw new FileNotFoundException(String.format("%s does not exist.", getDescription())); } } }
public GridFsResource getResource(GridFSFile file) { Assert.notNull(file, "GridFSFile must not be null!"); return new GridFsResource(file, getGridFs().openDownloadStream(file.getObjectId())); }
@Override public long contentLength() throws IOException { verifyExists(); return file.getLength(); }