Skip to content

Commit

Permalink
Merge pull request #189 from guardian/fix-transcript-download
Browse files Browse the repository at this point in the history
fix transcript and extractText download
  • Loading branch information
marjisound authored Jan 3, 2024
2 parents 6e0cbc2 + 58bedb8 commit c2574ce
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions frontend/src/js/components/viewer/DownloadModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ Email Body:
var element = document.createElement('a');
const escaped = encodeURIComponent(downloadable);
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + escaped);
element.setAttribute('download', this.state.saveAs + '.' + this.state.extension);
element.setAttribute('download', `${this.state.saveAs}.${this.state.extension}`);

element.click();
}
Expand All @@ -130,15 +130,21 @@ Email Body:
e.stopPropagation();

const filename = `${this.state.saveAs}.${this.state.extension}`;
const getDownloadType = downloadType => downloadType.startsWith('transcript') ? 'transcript' : this.state.downloadType;

switch (this.state.downloadType) {
switch (getDownloadType(this.state.downloadType)) {
case 'preview': {
authorizedDownload(authPreviewLink(this.props.resource.uri, filename))
.then(target => window.location.href = target);
break;
}
case 'extractedText': {
const text = this.props.resource.text.replace('<result-highlight>', '').replace('</result-highlight>', '');
const text = this.props.resource.text.contents.replace('<result-highlight>', '').replace('</result-highlight>', '');
this.download(text);
break;
}
case 'transcript': {
const text = this.props.resource.transcript[this.state.downloadLanguage].contents.replace('<result-highlight>', '').replace('</result-highlight>', '');
this.download(text);
break;
}
Expand All @@ -163,10 +169,16 @@ Email Body:
types.push({value: 'preview', label: 'PDF Preview'});
}

if (this.props.resource.text) {
if (this.props.resource.text.contents) {
types.push({value: 'extractedText', label: 'Extracted Text'});
}

if (this.props.resource.transcript) {
Object.keys(this.props.resource.transcript).forEach(language => {
types.push({value: `transcript${startCase(language)}`, label: `Transcript (${startCase(language)})`, language})
})
}

if (this.props.resource.ocr) {
const languages = Object.keys(this.props.resource.ocr);

Expand All @@ -179,13 +191,18 @@ Email Body:
}

downloadTypeSelected = (v) => {
switch (v.value) {
const getValue = value => value.startsWith('transcript') ? 'transcript' : value;

switch (getValue(v.value)) {
case 'preview':
this.setState({downloadType: 'perview', extension: 'pdf'});
break;
case 'extractedText':
this.setState({downloadType: 'extractedText', extension: 'txt'});
break;
case 'transcript':
this.setState({downloadType: v.value, downloadLanguage: v.language, extension: `${v.language}.transcript.txt`});
break;
case 'ocrText':
this.setState({downloadType: 'ocrText', downloadLanguage: v.language, extension: `${v.language}.ocr.txt` });
break;
Expand Down

0 comments on commit c2574ce

Please sign in to comment.