package org.kohsuke.github; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; import java.util.Base64; /** * A Content of a repository. * * @author Alexandre COLLIGNON * @see GHRepository#getFileContent(String) GHRepository#getFileContent(String) */ @SuppressWarnings({ "UnusedDeclaration" }) public class GHContent extends GitHubInteractiveObject implements Refreshable { /* * In normal use of this class, repository field is set via wrap(), but in the code search API, there's a nested * 'repository' field that gets populated from JSON. */ private GHRepository repository; private String type; private String encoding; private long size; private String sha; private String name; private String path; private String target; private String content; private String url; // this is the API url private String git_url; // this is the Blob url private String html_url; // this is the UI private String download_url; /** * Gets owner. * * @return the owner */ public GHRepository getOwner() { return repository; } /** * Gets type. * * @return the type */ public String getType() { return type; } /** * Gets encoding. * * @return the encoding */ public String getEncoding() { return encoding; } /** * Gets size. * * @return the size */ public long getSize() { return size; } /** * Gets sha. * * @return the sha */ public String getSha() { return sha; } /** * Gets name. * * @return the name */ public String getName() { return name; } /** * Gets path. * * @return the path */ public String getPath() { return path; } /** * Gets target of a symlink. This will only be set if {@code "symlink".equals(getType())} * * @return the target */ public String getTarget() { return target; } /** * Retrieve the decoded content that is stored at this location. * *
* Due to the nature of GitHub's API, you're not guaranteed that the content will already be populated, so this may * trigger network activity, and can throw an IOException. * * @return the content * @throws IOException * the io exception * @deprecated Use {@link #read()} */ @SuppressFBWarnings("DM_DEFAULT_ENCODING") public String getContent() throws IOException { return new String(Base64.getMimeDecoder().decode(getEncodedContent())); } /** * Retrieve the base64-encoded content that is stored at this location. * *
* Due to the nature of GitHub's API, you're not guaranteed that the content will already be populated, so this may * trigger network activity, and can throw an IOException. * * @return the encoded content * @throws IOException * the io exception * @deprecated Use {@link #read()} */ public String getEncodedContent() throws IOException { refresh(content); return content; } /** * Gets url. * * @return the url */ public String getUrl() { return url; } /** * Gets git url. * * @return the git url */ public String getGitUrl() { return git_url; } /** * Gets html url. * * @return the html url */ public String getHtmlUrl() { return html_url; } /** * Retrieves the actual content stored here. */ /** * Retrieves the actual bytes of the blob. * * @return the input stream * @throws IOException * the io exception */ public InputStream read() throws IOException { refresh(content); if (encoding.equals("base64")) { try { Base64.Decoder decoder = Base64.getMimeDecoder(); return new ByteArrayInputStream(decoder.decode(content.getBytes(StandardCharsets.US_ASCII))); } catch (IllegalArgumentException e) { throw new AssertionError(e); // US-ASCII is mandatory } } throw new UnsupportedOperationException("Unrecognized encoding: " + encoding); } /** * URL to retrieve the raw content of the file. Null if this is a directory. * * @return the download url * @throws IOException * the io exception */ public String getDownloadUrl() throws IOException { refresh(download_url); return download_url; } /** * Is file boolean. * * @return the boolean */ public boolean isFile() { return "file".equals(type); } /** * Is directory boolean. * * @return the boolean */ public boolean isDirectory() { return "dir".equals(type); } /** * Fully populate the data by retrieving missing data. *
* Depending on the original API call where this object is created, it may not contain everything.
*
* @throws IOException
* the io exception
*/
protected synchronized void populate() throws IOException {
root.createRequest().withUrlPath(url).fetchInto(this);
}
/**
* List immediate children of this directory.
*
* @return the paged iterable
* @throws IOException
* the io exception
*/
public PagedIterable