Skip to content

Commit

Permalink
fix(actions/manager/deployments): Suggest zip name based on deploymen…
Browse files Browse the repository at this point in the history
…t name.
  • Loading branch information
binh-dam-ibigroup committed Sep 15, 2021
1 parent 53c5933 commit ebefbbf
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions lib/manager/actions/deployments.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import { browserHistory } from 'react-router'
import { createAction, type ActionType } from 'redux-actions'

import { createVoidPayloadAction, secureFetch } from '../../common/actions'
import { receiveProject } from './projects'
import { startJobMonitor } from './status'
import fileDownload from '../../common/util/file-download'

import type {Deployment, Feed, SummarizedFeedVersion} from '../../types'
import type {dispatchFn, getStateFn} from '../../types/reducers'

import { startJobMonitor } from './status'
import { receiveProject } from './projects'

const DEPLOYMENT_URL = `/api/manager/secure/deployments`

// Deployment Actions
Expand Down Expand Up @@ -132,11 +132,28 @@ export function downloadBuildArtifact (deployment: Deployment, filename: ?string
}
}

/**
* Set a suggested file name for the given deployment.
*
* Note: Although a suggested file name is returned in the 'content-disposition' header of the 'download' endpoint,
* it is still needed to set it here because we trigger the "Save As" dialog separately from the actual fetch.
* Also, it is probably simpler to recompute the file name here instead of extracting it from the response.
*
* @param deployment The deployment object from which to derive the zip file name.
* @returns The suggested zip file name ending in ".zip" for the browser "Save As" dialog.
*/
function getDeploymentZipFileName (deployment: Deployment): string {
// Remove all spaces and special chars from deployment name.
const zipBase = deployment.name.replace(/[^a-zA-Z0-9]/g, '')
return `${zipBase}.zip`
}

export function downloadDeployment (deployment: Deployment) {
return function (dispatch: dispatchFn, getState: getStateFn) {
const zipName = getDeploymentZipFileName(deployment)
return dispatch(secureFetch(`${DEPLOYMENT_URL}/${deployment.id}/download`))
.then(response => response.blob())
.then(blob => fileDownload(blob, 'test.zip', 'application/zip'))
.then(blob => fileDownload(blob, zipName, 'application/zip'))
}
}

Expand Down

0 comments on commit ebefbbf

Please sign in to comment.