@Test(dataProvider = "ignoreOnMacOSX") public void testGetBlobContentType_AutoDetect_True() throws IOException { FilesystemStorageStrategyImpl storageStrategyAutoDetectContentType = new FilesystemStorageStrategyImpl( new Provider<BlobBuilder>() { @Override public BlobBuilder get() { return new BlobBuilderImpl(); } }, TestUtils.TARGET_BASE_DIR, true, new FilesystemContainerNameValidatorImpl(), new FilesystemBlobKeyValidatorImpl(), defaultLocation); String blobKey = TestUtils.createRandomBlobKey("file-", ".jpg"); TestUtils.createBlobsInContainer(CONTAINER_NAME, blobKey); Blob blob = storageStrategyAutoDetectContentType.getBlob(CONTAINER_NAME, blobKey); assertEquals(blob.getMetadata().getContentMetadata().getContentType(), "image/jpeg"); blobKey = TestUtils.createRandomBlobKey("file-", ".pdf"); TestUtils.createBlobsInContainer(CONTAINER_NAME, blobKey); blob = storageStrategyAutoDetectContentType.getBlob(CONTAINER_NAME, blobKey); assertEquals(blob.getMetadata().getContentMetadata().getContentType(), "application/pdf"); blobKey = TestUtils.createRandomBlobKey("file-", ".mp4"); TestUtils.createBlobsInContainer(CONTAINER_NAME, blobKey); blob = storageStrategyAutoDetectContentType.getBlob(CONTAINER_NAME, blobKey); assertEquals(blob.getMetadata().getContentMetadata().getContentType(), "video/mp4"); }
@Override public Blob getBlob(final String container, final String key) { BlobBuilder builder = blobBuilders.get(); builder.name(key); File file = getFileForBlobKey(container, key); ByteSource byteSource; if (getDirectoryBlobSuffix(key) != null) { if (!file.isDirectory()) { return null; } logger.debug("%s - %s is a directory", container, key); byteSource = ByteSource.empty(); } else { byteSource = Files.asByteSource(file); } try { String cacheControl = null; String contentDisposition = null; String contentEncoding = null; String contentLanguage = null; String contentType = null; HashCode hashCode = null; String eTag = null; Date expires = null; Tier tier = Tier.STANDARD; ImmutableMap.Builder<String, String> userMetadata = ImmutableMap.builder(); UserDefinedFileAttributeView view = getUserDefinedFileAttributeView(file.toPath()); if (view != null) { try { Set<String> attributes = ImmutableSet.copyOf(view.list()); cacheControl = readStringAttributeIfPresent(view, attributes, XATTR_CACHE_CONTROL); contentDisposition = readStringAttributeIfPresent(view, attributes, XATTR_CONTENT_DISPOSITION); contentEncoding = readStringAttributeIfPresent(view, attributes, XATTR_CONTENT_ENCODING); contentLanguage = readStringAttributeIfPresent(view, attributes, XATTR_CONTENT_LANGUAGE); contentType = readStringAttributeIfPresent(view, attributes, XATTR_CONTENT_TYPE); if (contentType == null && autoDetectContentType) { contentType = probeContentType(file.toPath()); } if (attributes.contains(XATTR_CONTENT_MD5)) { ByteBuffer buf = ByteBuffer.allocate(view.size(XATTR_CONTENT_MD5)); view.read(XATTR_CONTENT_MD5, buf); byte [] etagBytes = buf.array(); if (etagBytes.length == 16) { hashCode = HashCode.fromBytes(buf.array()); eTag = "\"" + hashCode + "\""; } else { eTag = new String(etagBytes, US_ASCII); } } if (attributes.contains(XATTR_EXPIRES)) { ByteBuffer buf = ByteBuffer.allocate(view.size(XATTR_EXPIRES)); view.read(XATTR_EXPIRES, buf); buf.flip(); expires = new Date(buf.asLongBuffer().get()); } String tierString = readStringAttributeIfPresent(view, attributes, XATTR_STORAGE_TIER); if (tierString != null) { tier = Tier.valueOf(tierString); } for (String attribute : attributes) { if (!attribute.startsWith(XATTR_USER_METADATA_PREFIX)) { continue; } String value = readStringAttributeIfPresent(view, attributes, attribute); userMetadata.put(attribute.substring(XATTR_USER_METADATA_PREFIX.length()), value); } } catch (IOException e) { logger.debug("xattrs not supported on %s", file.toPath()); } builder.payload(byteSource) .cacheControl(cacheControl) .contentDisposition(contentDisposition) .contentEncoding(contentEncoding) .contentLanguage(contentLanguage) .contentLength(byteSource.size()) .contentMD5(hashCode) .eTag(eTag) .contentType(contentType) .expires(expires) .tier(tier) .userMetadata(userMetadata.build()); } else { builder.payload(byteSource) .contentLength(byteSource.size()) .contentMD5(byteSource.hash(Hashing.md5()).asBytes()); } } catch (FileNotFoundException fnfe) { return null; } catch (IOException e) { throw Throwables.propagate(e); } Blob blob = builder.build(); blob.getMetadata().setContainer(container); blob.getMetadata().setLastModified(new Date(file.lastModified())); blob.getMetadata().setSize(file.length()); if (blob.getPayload().getContentMetadata().getContentMD5() != null) blob.getMetadata().setETag(base16().lowerCase().encode(blob.getPayload().getContentMetadata().getContentMD5())); return blob; }
FilesystemStorageStrategyImpl implements LocalStorageStrategy { @Override public Blob getBlob(final String container, final String key) { BlobBuilder builder = blobBuilders.get(); builder.name(key); File file = getFileForBlobKey(container, key); ByteSource byteSource; if (getDirectoryBlobSuffix(key) != null) { if (!file.isDirectory()) { return null; } logger.debug("%s - %s is a directory", container, key); byteSource = ByteSource.empty(); } else { byteSource = Files.asByteSource(file); } try { String cacheControl = null; String contentDisposition = null; String contentEncoding = null; String contentLanguage = null; String contentType = null; HashCode hashCode = null; String eTag = null; Date expires = null; Tier tier = Tier.STANDARD; ImmutableMap.Builder<String, String> userMetadata = ImmutableMap.builder(); UserDefinedFileAttributeView view = getUserDefinedFileAttributeView(file.toPath()); if (view != null) { try { Set<String> attributes = ImmutableSet.copyOf(view.list()); cacheControl = readStringAttributeIfPresent(view, attributes, XATTR_CACHE_CONTROL); contentDisposition = readStringAttributeIfPresent(view, attributes, XATTR_CONTENT_DISPOSITION); contentEncoding = readStringAttributeIfPresent(view, attributes, XATTR_CONTENT_ENCODING); contentLanguage = readStringAttributeIfPresent(view, attributes, XATTR_CONTENT_LANGUAGE); contentType = readStringAttributeIfPresent(view, attributes, XATTR_CONTENT_TYPE); if (contentType == null && autoDetectContentType) { contentType = probeContentType(file.toPath()); } if (attributes.contains(XATTR_CONTENT_MD5)) { ByteBuffer buf = ByteBuffer.allocate(view.size(XATTR_CONTENT_MD5)); view.read(XATTR_CONTENT_MD5, buf); byte [] etagBytes = buf.array(); if (etagBytes.length == 16) { hashCode = HashCode.fromBytes(buf.array()); eTag = "\"" + hashCode + "\""; } else { eTag = new String(etagBytes, US_ASCII); } } if (attributes.contains(XATTR_EXPIRES)) { ByteBuffer buf = ByteBuffer.allocate(view.size(XATTR_EXPIRES)); view.read(XATTR_EXPIRES, buf); buf.flip(); expires = new Date(buf.asLongBuffer().get()); } String tierString = readStringAttributeIfPresent(view, attributes, XATTR_STORAGE_TIER); if (tierString != null) { tier = Tier.valueOf(tierString); } for (String attribute : attributes) { if (!attribute.startsWith(XATTR_USER_METADATA_PREFIX)) { continue; } String value = readStringAttributeIfPresent(view, attributes, attribute); userMetadata.put(attribute.substring(XATTR_USER_METADATA_PREFIX.length()), value); } } catch (IOException e) { logger.debug("xattrs not supported on %s", file.toPath()); } builder.payload(byteSource) .cacheControl(cacheControl) .contentDisposition(contentDisposition) .contentEncoding(contentEncoding) .contentLanguage(contentLanguage) .contentLength(byteSource.size()) .contentMD5(hashCode) .eTag(eTag) .contentType(contentType) .expires(expires) .tier(tier) .userMetadata(userMetadata.build()); } else { builder.payload(byteSource) .contentLength(byteSource.size()) .contentMD5(byteSource.hash(Hashing.md5()).asBytes()); } } catch (FileNotFoundException fnfe) { return null; } catch (IOException e) { throw Throwables.propagate(e); } Blob blob = builder.build(); blob.getMetadata().setContainer(container); blob.getMetadata().setLastModified(new Date(file.lastModified())); blob.getMetadata().setSize(file.length()); if (blob.getPayload().getContentMetadata().getContentMD5() != null) blob.getMetadata().setETag(base16().lowerCase().encode(blob.getPayload().getContentMetadata().getContentMD5())); return blob; } }
FilesystemStorageStrategyImpl implements LocalStorageStrategy { @Override public Blob getBlob(final String container, final String key) { BlobBuilder builder = blobBuilders.get(); builder.name(key); File file = getFileForBlobKey(container, key); ByteSource byteSource; if (getDirectoryBlobSuffix(key) != null) { if (!file.isDirectory()) { return null; } logger.debug("%s - %s is a directory", container, key); byteSource = ByteSource.empty(); } else { byteSource = Files.asByteSource(file); } try { String cacheControl = null; String contentDisposition = null; String contentEncoding = null; String contentLanguage = null; String contentType = null; HashCode hashCode = null; String eTag = null; Date expires = null; Tier tier = Tier.STANDARD; ImmutableMap.Builder<String, String> userMetadata = ImmutableMap.builder(); UserDefinedFileAttributeView view = getUserDefinedFileAttributeView(file.toPath()); if (view != null) { try { Set<String> attributes = ImmutableSet.copyOf(view.list()); cacheControl = readStringAttributeIfPresent(view, attributes, XATTR_CACHE_CONTROL); contentDisposition = readStringAttributeIfPresent(view, attributes, XATTR_CONTENT_DISPOSITION); contentEncoding = readStringAttributeIfPresent(view, attributes, XATTR_CONTENT_ENCODING); contentLanguage = readStringAttributeIfPresent(view, attributes, XATTR_CONTENT_LANGUAGE); contentType = readStringAttributeIfPresent(view, attributes, XATTR_CONTENT_TYPE); if (contentType == null && autoDetectContentType) { contentType = probeContentType(file.toPath()); } if (attributes.contains(XATTR_CONTENT_MD5)) { ByteBuffer buf = ByteBuffer.allocate(view.size(XATTR_CONTENT_MD5)); view.read(XATTR_CONTENT_MD5, buf); byte [] etagBytes = buf.array(); if (etagBytes.length == 16) { hashCode = HashCode.fromBytes(buf.array()); eTag = "\"" + hashCode + "\""; } else { eTag = new String(etagBytes, US_ASCII); } } if (attributes.contains(XATTR_EXPIRES)) { ByteBuffer buf = ByteBuffer.allocate(view.size(XATTR_EXPIRES)); view.read(XATTR_EXPIRES, buf); buf.flip(); expires = new Date(buf.asLongBuffer().get()); } String tierString = readStringAttributeIfPresent(view, attributes, XATTR_STORAGE_TIER); if (tierString != null) { tier = Tier.valueOf(tierString); } for (String attribute : attributes) { if (!attribute.startsWith(XATTR_USER_METADATA_PREFIX)) { continue; } String value = readStringAttributeIfPresent(view, attributes, attribute); userMetadata.put(attribute.substring(XATTR_USER_METADATA_PREFIX.length()), value); } } catch (IOException e) { logger.debug("xattrs not supported on %s", file.toPath()); } builder.payload(byteSource) .cacheControl(cacheControl) .contentDisposition(contentDisposition) .contentEncoding(contentEncoding) .contentLanguage(contentLanguage) .contentLength(byteSource.size()) .contentMD5(hashCode) .eTag(eTag) .contentType(contentType) .expires(expires) .tier(tier) .userMetadata(userMetadata.build()); } else { builder.payload(byteSource) .contentLength(byteSource.size()) .contentMD5(byteSource.hash(Hashing.md5()).asBytes()); } } catch (FileNotFoundException fnfe) { return null; } catch (IOException e) { throw Throwables.propagate(e); } Blob blob = builder.build(); blob.getMetadata().setContainer(container); blob.getMetadata().setLastModified(new Date(file.lastModified())); blob.getMetadata().setSize(file.length()); if (blob.getPayload().getContentMetadata().getContentMD5() != null) blob.getMetadata().setETag(base16().lowerCase().encode(blob.getPayload().getContentMetadata().getContentMD5())); return blob; } @Inject protected  FilesystemStorageStrategyImpl(Provider<BlobBuilder> blobBuilders,
         @Named(FilesystemConstants.PROPERTY_BASEDIR) String baseDir,
         @Named(FilesystemConstants.PROPERTY_AUTO_DETECT_CONTENT_TYPE) boolean autoDetectContentType,
         FilesystemContainerNameValidator filesystemContainerNameValidator,
         FilesystemBlobKeyValidator filesystemBlobKeyValidator,
         Supplier<Location> defaultLocation); }
FilesystemStorageStrategyImpl implements LocalStorageStrategy { @Override public Blob getBlob(final String container, final String key) { BlobBuilder builder = blobBuilders.get(); builder.name(key); File file = getFileForBlobKey(container, key); ByteSource byteSource; if (getDirectoryBlobSuffix(key) != null) { if (!file.isDirectory()) { return null; } logger.debug("%s - %s is a directory", container, key); byteSource = ByteSource.empty(); } else { byteSource = Files.asByteSource(file); } try { String cacheControl = null; String contentDisposition = null; String contentEncoding = null; String contentLanguage = null; String contentType = null; HashCode hashCode = null; String eTag = null; Date expires = null; Tier tier = Tier.STANDARD; ImmutableMap.Builder<String, String> userMetadata = ImmutableMap.builder(); UserDefinedFileAttributeView view = getUserDefinedFileAttributeView(file.toPath()); if (view != null) { try { Set<String> attributes = ImmutableSet.copyOf(view.list()); cacheControl = readStringAttributeIfPresent(view, attributes, XATTR_CACHE_CONTROL); contentDisposition = readStringAttributeIfPresent(view, attributes, XATTR_CONTENT_DISPOSITION); contentEncoding = readStringAttributeIfPresent(view, attributes, XATTR_CONTENT_ENCODING); contentLanguage = readStringAttributeIfPresent(view, attributes, XATTR_CONTENT_LANGUAGE); contentType = readStringAttributeIfPresent(view, attributes, XATTR_CONTENT_TYPE); if (contentType == null && autoDetectContentType) { contentType = probeContentType(file.toPath()); } if (attributes.contains(XATTR_CONTENT_MD5)) { ByteBuffer buf = ByteBuffer.allocate(view.size(XATTR_CONTENT_MD5)); view.read(XATTR_CONTENT_MD5, buf); byte [] etagBytes = buf.array(); if (etagBytes.length == 16) { hashCode = HashCode.fromBytes(buf.array()); eTag = "\"" + hashCode + "\""; } else { eTag = new String(etagBytes, US_ASCII); } } if (attributes.contains(XATTR_EXPIRES)) { ByteBuffer buf = ByteBuffer.allocate(view.size(XATTR_EXPIRES)); view.read(XATTR_EXPIRES, buf); buf.flip(); expires = new Date(buf.asLongBuffer().get()); } String tierString = readStringAttributeIfPresent(view, attributes, XATTR_STORAGE_TIER); if (tierString != null) { tier = Tier.valueOf(tierString); } for (String attribute : attributes) { if (!attribute.startsWith(XATTR_USER_METADATA_PREFIX)) { continue; } String value = readStringAttributeIfPresent(view, attributes, attribute); userMetadata.put(attribute.substring(XATTR_USER_METADATA_PREFIX.length()), value); } } catch (IOException e) { logger.debug("xattrs not supported on %s", file.toPath()); } builder.payload(byteSource) .cacheControl(cacheControl) .contentDisposition(contentDisposition) .contentEncoding(contentEncoding) .contentLanguage(contentLanguage) .contentLength(byteSource.size()) .contentMD5(hashCode) .eTag(eTag) .contentType(contentType) .expires(expires) .tier(tier) .userMetadata(userMetadata.build()); } else { builder.payload(byteSource) .contentLength(byteSource.size()) .contentMD5(byteSource.hash(Hashing.md5()).asBytes()); } } catch (FileNotFoundException fnfe) { return null; } catch (IOException e) { throw Throwables.propagate(e); } Blob blob = builder.build(); blob.getMetadata().setContainer(container); blob.getMetadata().setLastModified(new Date(file.lastModified())); blob.getMetadata().setSize(file.length()); if (blob.getPayload().getContentMetadata().getContentMD5() != null) blob.getMetadata().setETag(base16().lowerCase().encode(blob.getPayload().getContentMetadata().getContentMD5())); return blob; } @Inject protected  FilesystemStorageStrategyImpl(Provider<BlobBuilder> blobBuilders,
         @Named(FilesystemConstants.PROPERTY_BASEDIR) String baseDir,
         @Named(FilesystemConstants.PROPERTY_AUTO_DETECT_CONTENT_TYPE) boolean autoDetectContentType,
         FilesystemContainerNameValidator filesystemContainerNameValidator,
         FilesystemBlobKeyValidator filesystemBlobKeyValidator,
         Supplier<Location> defaultLocation); @Override boolean containerExists(String container); @Override Collection<String> getAllContainerNames(); @Override boolean createContainerInLocation(String container, Location location, CreateContainerOptions options); @Override ContainerAccess getContainerAccess(String container); @Override void setContainerAccess(String container, ContainerAccess access); @Override void deleteContainer(String container); @Override void clearContainer(final String container); @Override void clearContainer(String container, ListContainerOptions options); @Override StorageMetadata getContainerMetadata(String container); @Override boolean blobExists(String container, String key); @Override Iterable<String> getBlobKeysInsideContainer(String container, String prefix); @Override Blob getBlob(final String container, final String key); @Override String putBlob(final String containerName, final Blob blob); @Override void removeBlob(final String container, final String blobKey); @Override BlobAccess getBlobAccess(String containerName, String blobName); @Override void setBlobAccess(String container, String name, BlobAccess access); @Override Location getLocation(final String containerName); @Override String getSeparator(); boolean createContainer(String container); Blob newBlob(@ParamValidators({ FilesystemBlobKeyValidator.class }) String name); File getFileForBlobKey(String container, String blobKey); boolean directoryExists(String container, String directory); void createDirectory(String container, String directory); void deleteDirectory(String container, String directory); long countBlobs(String container, ListContainerOptions options); }
FilesystemStorageStrategyImpl implements LocalStorageStrategy { @Override public Blob getBlob(final String container, final String key) { BlobBuilder builder = blobBuilders.get(); builder.name(key); File file = getFileForBlobKey(container, key); ByteSource byteSource; if (getDirectoryBlobSuffix(key) != null) { if (!file.isDirectory()) { return null; } logger.debug("%s - %s is a directory", container, key); byteSource = ByteSource.empty(); } else { byteSource = Files.asByteSource(file); } try { String cacheControl = null; String contentDisposition = null; String contentEncoding = null; String contentLanguage = null; String contentType = null; HashCode hashCode = null; String eTag = null; Date expires = null; Tier tier = Tier.STANDARD; ImmutableMap.Builder<String, String> userMetadata = ImmutableMap.builder(); UserDefinedFileAttributeView view = getUserDefinedFileAttributeView(file.toPath()); if (view != null) { try { Set<String> attributes = ImmutableSet.copyOf(view.list()); cacheControl = readStringAttributeIfPresent(view, attributes, XATTR_CACHE_CONTROL); contentDisposition = readStringAttributeIfPresent(view, attributes, XATTR_CONTENT_DISPOSITION); contentEncoding = readStringAttributeIfPresent(view, attributes, XATTR_CONTENT_ENCODING); contentLanguage = readStringAttributeIfPresent(view, attributes, XATTR_CONTENT_LANGUAGE); contentType = readStringAttributeIfPresent(view, attributes, XATTR_CONTENT_TYPE); if (contentType == null && autoDetectContentType) { contentType = probeContentType(file.toPath()); } if (attributes.contains(XATTR_CONTENT_MD5)) { ByteBuffer buf = ByteBuffer.allocate(view.size(XATTR_CONTENT_MD5)); view.read(XATTR_CONTENT_MD5, buf); byte [] etagBytes = buf.array(); if (etagBytes.length == 16) { hashCode = HashCode.fromBytes(buf.array()); eTag = "\"" + hashCode + "\""; } else { eTag = new String(etagBytes, US_ASCII); } } if (attributes.contains(XATTR_EXPIRES)) { ByteBuffer buf = ByteBuffer.allocate(view.size(XATTR_EXPIRES)); view.read(XATTR_EXPIRES, buf); buf.flip(); expires = new Date(buf.asLongBuffer().get()); } String tierString = readStringAttributeIfPresent(view, attributes, XATTR_STORAGE_TIER); if (tierString != null) { tier = Tier.valueOf(tierString); } for (String attribute : attributes) { if (!attribute.startsWith(XATTR_USER_METADATA_PREFIX)) { continue; } String value = readStringAttributeIfPresent(view, attributes, attribute); userMetadata.put(attribute.substring(XATTR_USER_METADATA_PREFIX.length()), value); } } catch (IOException e) { logger.debug("xattrs not supported on %s", file.toPath()); } builder.payload(byteSource) .cacheControl(cacheControl) .contentDisposition(contentDisposition) .contentEncoding(contentEncoding) .contentLanguage(contentLanguage) .contentLength(byteSource.size()) .contentMD5(hashCode) .eTag(eTag) .contentType(contentType) .expires(expires) .tier(tier) .userMetadata(userMetadata.build()); } else { builder.payload(byteSource) .contentLength(byteSource.size()) .contentMD5(byteSource.hash(Hashing.md5()).asBytes()); } } catch (FileNotFoundException fnfe) { return null; } catch (IOException e) { throw Throwables.propagate(e); } Blob blob = builder.build(); blob.getMetadata().setContainer(container); blob.getMetadata().setLastModified(new Date(file.lastModified())); blob.getMetadata().setSize(file.length()); if (blob.getPayload().getContentMetadata().getContentMD5() != null) blob.getMetadata().setETag(base16().lowerCase().encode(blob.getPayload().getContentMetadata().getContentMD5())); return blob; } @Inject protected  FilesystemStorageStrategyImpl(Provider<BlobBuilder> blobBuilders,
         @Named(FilesystemConstants.PROPERTY_BASEDIR) String baseDir,
         @Named(FilesystemConstants.PROPERTY_AUTO_DETECT_CONTENT_TYPE) boolean autoDetectContentType,
         FilesystemContainerNameValidator filesystemContainerNameValidator,
         FilesystemBlobKeyValidator filesystemBlobKeyValidator,
         Supplier<Location> defaultLocation); @Override boolean containerExists(String container); @Override Collection<String> getAllContainerNames(); @Override boolean createContainerInLocation(String container, Location location, CreateContainerOptions options); @Override ContainerAccess getContainerAccess(String container); @Override void setContainerAccess(String container, ContainerAccess access); @Override void deleteContainer(String container); @Override void clearContainer(final String container); @Override void clearContainer(String container, ListContainerOptions options); @Override StorageMetadata getContainerMetadata(String container); @Override boolean blobExists(String container, String key); @Override Iterable<String> getBlobKeysInsideContainer(String container, String prefix); @Override Blob getBlob(final String container, final String key); @Override String putBlob(final String containerName, final Blob blob); @Override void removeBlob(final String container, final String blobKey); @Override BlobAccess getBlobAccess(String containerName, String blobName); @Override void setBlobAccess(String container, String name, BlobAccess access); @Override Location getLocation(final String containerName); @Override String getSeparator(); boolean createContainer(String container); Blob newBlob(@ParamValidators({ FilesystemBlobKeyValidator.class }) String name); File getFileForBlobKey(String container, String blobKey); boolean directoryExists(String container, String directory); void createDirectory(String container, String directory); void deleteDirectory(String container, String directory); long countBlobs(String container, ListContainerOptions options);  }