Skip to content

Commit

Permalink
Add downloadList to download images as zip
Browse files Browse the repository at this point in the history
  • Loading branch information
chregu committed Sep 24, 2022
1 parent fd9dacb commit 08bdd4d
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 32 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 3.10.0 - [24-09-2022]

- Add support for downloading a list of images as Zip via `rokka.sourceimages.listDownload`

# 3.9.1 - [19-09-2022]

- Fix getting new Token, when apiKey is set via `rokka.user.getNewToken`
Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,22 @@ Check out [the rokka documentation](https://rokka.io/documentation/references/se
Sorting works with user metadata as well and can be passed as either an array or as a
comma separated string.

#### rokka.sourceimages.downloadList(organization, params) → Promise

Get a list of source images as zip. Same parameters as the `list` method

Example:

```js
const search = {
'user:int:id': '42',
'height': '64'
}
rokka.sourceimages.list('myorg', { search: search })
.then(function(result) {})
.catch(function(err) {});
```

#### rokka.sourceimages.get(organization, hash, queryParams) → Promise

Get information of a source image by hash.
Expand Down
108 changes: 76 additions & 32 deletions src/apis/sourceimages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ export interface APISourceimages {
organization: string,
params?: SearchQueryParams | undefined,
) => Promise<SourceimagesListResponse>
downloadList: (
organization: string,
params?: SearchQueryParams | undefined,
) => Promise<RokkaDownloadResponse>
get: (
organization: string,
hash: string,
Expand Down Expand Up @@ -225,6 +229,42 @@ async function stream2buffer(stream: Stream): Promise<Buffer> {
stream.on('error', err => reject(`error converting stream - ${err}`))
})
}

function getQueryParamsForList({
limit = null,
offset = null,
sort = null,
search = null,
facets = null,
deleted = null,
}: SearchQueryParams) {
let queryParams: SearchQueryParams = {}

if (limit !== null) {
queryParams.limit = limit
}
if (offset !== null) {
queryParams.offset = offset
}
if (facets !== null) {
queryParams.facets = facets
}
if (deleted !== null) {
queryParams.deleted = deleted
}

if (sort !== null) {
if (Array.isArray(sort)) {
sort = sort.join(',')
}
queryParams.sort = sort
}
if (search !== null) {
queryParams = Object.assign(queryParams, search)
}
return queryParams
}

/**
* ### Source Images
*
Expand Down Expand Up @@ -273,43 +313,47 @@ export default (state: State): { sourceimages: APISourceimages } => {
*/
list: (
organization,
{
limit = null,
offset = null,
sort = null,
search = null,
facets = null,
deleted = null,
}: SearchQueryParams = {},
): Promise<RokkaResponse> => {
let queryParams: SearchQueryParams = {}
params: SearchQueryParams = {},
): Promise<SourceimagesListResponse> => {
const queryParams = getQueryParamsForList(params)

if (limit !== null) {
queryParams.limit = limit
}
if (offset !== null) {
queryParams.offset = offset
}
if (facets !== null) {
queryParams.facets = facets
}
if (deleted !== null) {
queryParams.deleted = deleted
}
return state.request(
'GET',
`sourceimages/${organization}`,
null,
queryParams,
)
},
/**
* Get a list of source images as zip. Same parameters as the `list` method
*
* Example:
*
* ```js
* const search = {
* 'user:int:id': '42',
* 'height': '64'
* }
* rokka.sourceimages.list('myorg', { search: search })
* .then(function(result) {})
* .catch(function(err) {});
* ```
*
* @authenticated
* @param {string} organization name
* @param {Object} params Query string params (limit, offset, sort and search)
* @return {Promise}
*/

if (sort !== null) {
if (Array.isArray(sort)) {
sort = sort.join(',')
}
queryParams.sort = sort
}
if (search !== null) {
queryParams = Object.assign(queryParams, search)
}
downloadList: (
organization,
params: SearchQueryParams = {},
): Promise<RokkaDownloadResponse> => {
const queryParams = getQueryParamsForList(params)

return state.request(
'GET',
`sourceimages/${organization}`,
`sourceimages/${organization}/download`,
null,
queryParams,
)
Expand Down

0 comments on commit 08bdd4d

Please sign in to comment.