Skip to content

Commit

Permalink
fix(1749): Use query param dir=true to indicate directory download (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
tkyi authored Oct 15, 2024
1 parent 4ae5570 commit ef31fca
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
5 changes: 2 additions & 3 deletions plugins/builds/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,11 @@ Example payload:
Arguments:

* `type` - Return type for build artifact, `download` or `preview`
* `dir` - If downloading directory or not (`true` or `false`, default `false`). Must be set with `type=download`.

`GET /builds/{id}/artifacts/{name*}?type=preview`

`GET /builds/{id}/artifacts/this/is/a/directory/path/?type=download`

*Note: To download a directory, there must be a trailing slash (`/`) in the name and `type=download`.*
`GET /builds/{id}/artifacts/this/is/a/directory/path?type=download&dir=true`

#### Get build statuses
`GET /builds/statuses`
Expand Down
11 changes: 6 additions & 5 deletions plugins/builds/artifacts/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ module.exports = config => ({
.then(async () => {
// Directory should fetch manifest and
// gather all files that belong to that directory
if (artifact.endsWith('/') && req.query.type === 'download') {
if (req.query.dir && req.query.type === 'download') {
// Create a zip name from the directory structure
const zipName = artifact.split('/').slice(-2)[0];
const zipName = artifact.split('/').slice(-1)[0];

try {
const token = jwt.sign({
Expand All @@ -69,7 +69,7 @@ module.exports = config => ({
method: 'GET'
}).text();
const manifestArray = manifest.trim().split('\n');
const directoryArray = manifestArray.filter(f => f.startsWith(`./${artifact}`));
const directoryArray = manifestArray.filter(f => f.startsWith(`./${artifact}/`));

// Create a stream and set up archiver
const archive = archiver('zip', { zlib: { level: 9 } });
Expand Down Expand Up @@ -100,7 +100,7 @@ module.exports = config => ({
archive.emit('error', err); // Emit error to stop the archive process
});

const relativePath = file.replace(`./${artifact}`, `./${zipName}/`);
const relativePath = file.replace(`./${artifact}/`, `./${zipName}/`);

// Append the file stream to the archive with the correct relative path
archive.append(fileStream, { name: relativePath });
Expand Down Expand Up @@ -168,7 +168,8 @@ module.exports = config => ({
name: artifactSchema
}),
query: joi.object({
type: typeSchema
type: typeSchema,
dir: joi.boolean().truthy('true').falsy('false').default(false)
}).options({ allowUnknown: true })
}
}
Expand Down
6 changes: 3 additions & 3 deletions test/plugins/builds.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7304,7 +7304,7 @@ describe('build plugin test', () => {
'Content-Length': largeFileContent.length
});

options.url = `/builds/${id}/artifacts/./artifacts/?type=download`;
options.url = `/builds/${id}/artifacts/./artifacts?type=download&dir=true`;

return server.inject(options).then(reply => {
assert.equal(reply.statusCode, 200);
Expand All @@ -7327,7 +7327,7 @@ describe('build plugin test', () => {
.get('/v1/builds/12345/ARTIFACTS/./artifacts/sample-mp4-file.mp4?token=sign&type=download')
.reply(500);

options.url = `/builds/${id}/artifacts/./artifacts/?type=download`;
options.url = `/builds/${id}/artifacts/./artifacts?type=download&dir=true`;

try {
await server.inject(options);
Expand All @@ -7344,7 +7344,7 @@ describe('build plugin test', () => {
});

it('returns 500 for a missing manifest for artifact directory', () => {
options.url = `/builds/${id}/artifacts/doesnotexist/?type=download`;
options.url = `/builds/${id}/artifacts/doesnotexist?type=download&dir=true`;

nock(logBaseUrl)
.defaultReplyHeaders(headersMock)
Expand Down

0 comments on commit ef31fca

Please sign in to comment.