Skip to content

Commit f3c2200

Browse files
committed
Added File downloadSingleArtifactsFile() method.
1 parent 065d44f commit f3c2200

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

src/main/java/org/gitlab4j/api/JobApi.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,10 +220,43 @@ public InputStream downloadArtifactsFile(Integer projectId, String ref, String j
220220
* @param projectId The ID of the project owned by the authenticated user
221221
* @param jobId The unique job identifier
222222
* @param artifactPath Path to a file inside the artifacts archive
223-
* @return an InputStream to read the specified artifacts file from
223+
* @param directory the File instance of the directory to save the file to, if null will use "java.io.tmpdir"
224+
* @return a File instance pointing to the download of the specified artifacts file
224225
* @throws GitLabApiException if any exception occurs
225226
*/
227+
public File downloadSingleArtifactsFile(Integer projectId, Integer jobId, Path artifactPath, File directory) throws GitLabApiException {
228+
229+
Response response = get(Response.Status.OK, getDefaultPerPageParam(), "projects", projectId, "jobs", jobId, "artifacts", artifactPath);
230+
try {
231+
232+
if (directory == null)
233+
directory = new File(System.getProperty("java.io.tmpdir"));
226234

235+
String filename = artifactPath.getFileName().toString();
236+
File file = new File(directory, filename);
237+
238+
InputStream in = response.readEntity(InputStream.class);
239+
Files.copy(in, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
240+
return (file);
241+
242+
} catch (IOException ioe) {
243+
throw new GitLabApiException(ioe);
244+
}
245+
}
246+
247+
/**
248+
* Download a single artifact file from within the job's artifacts archive.
249+
*
250+
* Only a single file is going to be extracted from the archive and streamed to a client.
251+
*
252+
* GET /projects/:id/jobs/:job_id/artifacts/*artifact_path
253+
*
254+
* @param projectId The ID of the project owned by the authenticated user
255+
* @param jobId The unique job identifier
256+
* @param artifactPath Path to a file inside the artifacts archive
257+
* @return an InputStream to read the specified artifacts file from
258+
* @throws GitLabApiException if any exception occurs
259+
*/
227260
public InputStream downloadSingleArtifactsFile(Integer projectId, Integer jobId, Path artifactPath) throws GitLabApiException {
228261
Response response = get(Response.Status.OK, getDefaultPerPageParam(), "projects", projectId, "jobs", jobId, "artifacts", artifactPath);
229262
return (response.readEntity(InputStream.class));

0 commit comments

Comments
 (0)