private boolean isBinary(Entry entry) throws IOException { InputStream in = entry.openInputStream(); try { return RawText.isBinary(in); } finally { safeClose(in); } }
private ObjectNode fileAsJson(String path, org.tmatesoft.svn.core.io.SVNRepository repository) throws SVNException, IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); SVNProperties prop = new SVNProperties(); repository.getFile(path, -1l, prop, baos); SVNDirEntry entry = repository.info(path, -1l); long size = entry.getSize(); boolean isBinary; String mimeType; String data = null; if (size > MAX_FILE_SIZE_CAN_BE_VIEWED) { isBinary = true; mimeType = "application/octet-stream"; } else { byte[] bytes = baos.toByteArray(); isBinary = RawText.isBinary(bytes); if (!isBinary) { data = new String(bytes, FileUtil.detectCharset(bytes)); } mimeType = new Tika().detect(bytes, path); } String author = prop.getStringValue(SVNProperty.LAST_AUTHOR); User user = User.findByLoginId(author); String commitDate = prop.getStringValue(SVNProperty.COMMITTED_DATE); DateTimeFormatter dateFormatter = ISODateTimeFormat.dateTime(); Long commitTime = dateFormatter.parseMillis(commitDate); ObjectNode result = Json.newObject(); result.put("type", "file"); result.put("revisionNo", prop.getStringValue(SVNProperty.COMMITTED_REVISION)); result.put("author", author); result.put("avatar", getAvatar(user)); result.put("userName", user.name); result.put("userLoginId", user.loginId); result.put("createdDate", commitTime); result.put("commitMessage", entry.getCommitMessage()); result.put("commiter", author); result.put("size", size); result.put("isBinary", isBinary); result.put("mimeType", mimeType); result.put("data", data); return result; }
private boolean isBinary(byte[] content, int sz) { return RawText.isBinary(content, sz); }