/** Read the String representation of filesystem attribute, or return null if not present. */ private static String readStringAttributeIfPresent( UserDefinedFileAttributeView view, Set<String> attributes, String name) throws IOException { if (!attributes.contains(name)) { return null; } ByteBuffer buf = ByteBuffer.allocate(view.size(name)); view.read(name, buf); return new String(buf.array(), StandardCharsets.UTF_8); }
static Map<String, ByteBuffer> readUserDefinedFileAttributes(Path file) throws IOException { UserDefinedFileAttributeView view = getFileAttributeView(file, UserDefinedFileAttributeView.class); Map<String, ByteBuffer> result = new HashMap<>(); for (String name : view.list()) { int size = view.size(name); ByteBuffer bb = ByteBuffer.allocate(size); int n = view.read(name, bb); assertTrue(n == size); bb.flip(); result.put(name, bb); } return result; }
public static long getFileKey(Path filePath) { if (!Files.exists(filePath)) { return -1; } try { if (OSDetector.isApple()) { Xattrj xattrj = getXattrj(); if (xattrj == null) { return -1; } String fileKey = xattrj.readAttribute(filePath.toFile(), "fileKey"); if (fileKey == null) { return -1; } return Long.parseLong(fileKey); } else { UserDefinedFileAttributeView userDefinedFileAttributeView = Files.getFileAttributeView(filePath, UserDefinedFileAttributeView.class); List<String> list = userDefinedFileAttributeView.list(); if (!list.contains("fileKey")) { return -1; } ByteBuffer byteBuffer = ByteBuffer.allocate(userDefinedFileAttributeView.size("fileKey")); userDefinedFileAttributeView.read("fileKey", byteBuffer); CharBuffer charBuffer = _CHARSET.decode((ByteBuffer) byteBuffer.flip()); return Long.parseLong(charBuffer.toString()); } } catch (Exception e) { _logger.error(e.getMessage(), e); return -1; } }
public static String getFileKey(Path filePath) { if (!Files.exists(filePath)) { return ""; } try { if (OSDetector.isWindows()) { UserDefinedFileAttributeView userDefinedFileAttributeView = Files.getFileAttributeView(filePath, UserDefinedFileAttributeView.class); List<String> list = userDefinedFileAttributeView.list(); if (!list.contains("fileKey")) { return ""; } ByteBuffer byteBuffer = ByteBuffer.allocate(userDefinedFileAttributeView.size("fileKey")); userDefinedFileAttributeView.read("fileKey", byteBuffer); CharBuffer charBuffer = _CHARSET.decode((ByteBuffer) byteBuffer.flip()); return charBuffer.toString(); } else { BasicFileAttributes basicFileAttributes = Files.readAttributes(filePath, BasicFileAttributes.class); Object fileKey = basicFileAttributes.fileKey(); return fileKey.toString(); } } catch (Exception e) { _logger.error(e.getMessage(), e); return ""; } }
@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) { 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; Date expires = null; ImmutableMap.Builder<String, String> userMetadata = ImmutableMap.builder(); UserDefinedFileAttributeView view = getUserDefinedFileAttributeView(file.toPath()); if (view != null) { 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); hashCode = HashCode.fromBytes(buf.array()); } 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()); } 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); } builder .payload(byteSource) .cacheControl(cacheControl) .contentDisposition(contentDisposition) .contentEncoding(contentEncoding) .contentLanguage(contentLanguage) .contentLength(byteSource.size()) .contentMD5(hashCode) .contentType(contentType) .expires(expires) .userMetadata(userMetadata.build()); } else { builder .payload(byteSource) .contentLength(byteSource.size()) .contentMD5(byteSource.hash(Hashing.md5()).asBytes()); } } 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; }