Skip to content

Commit

Permalink
YDA-5137: Set Content-Length header field in the download response.
Browse files Browse the repository at this point in the history
  • Loading branch information
HarmdR committed Jul 27, 2023
1 parent 2223594 commit 328288f
Show file tree
Hide file tree
Showing 9 changed files with 230 additions and 74 deletions.
14 changes: 9 additions & 5 deletions deposit/deposit.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ def data() -> Response:
def download() -> Response:
path = '/' + g.irods.zone + '/home' + request.args.get('filepath')
filename = path.rsplit('/', 1)[1]
READ_BUFFER_SIZE = 1024 * io.DEFAULT_BUFFER_SIZE

def read_file_chunks(path: str) -> Iterator[bytes]:
obj = g.irods.data_objects.get(path)
def read_file_chunks(data_object) -> Iterator[bytes]:
READ_BUFFER_SIZE = 1024 * io.DEFAULT_BUFFER_SIZE

try:
with obj.open('r') as fd:
with data_object.open('r') as fd:
while True:
buf = fd.read(READ_BUFFER_SIZE)
if buf:
Expand All @@ -76,10 +76,14 @@ def read_file_chunks(path: str) -> Iterator[bytes]:
abort(500)

if g.irods.data_objects.exists(path):
data_object = g.irods.data_objects.get(path)
size = data_object.replicas[0].size

return Response(
stream_with_context(read_file_chunks(path)),
stream_with_context(read_file_chunks(data_object)),
headers={
'Content-Disposition': f'attachment; filename={filename}',
'Content-Length': f'{size}',
'Content-Type': 'application/octet'
}
)
Expand Down
84 changes: 66 additions & 18 deletions deposit/metadata-form/src/AffiliationIdentifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class AffiliationIdentifier extends React.Component {
const titleAffiliationName = this.props.schema.properties.Affiliation_Name.title
const titleAffiliationIdentifier = this.props.schema.properties.Affiliation_Identifier.title

let label = <label className='form-label'>{titleAffiliationName}</label>
// let label = <label className='form-label'>{titleAffiliationName}</label>

const helpAffiliationName = this.props.uiSchema.Affiliation_Name['ui:help']
const helpAffiliationIdentifier = this.props.uiSchema.Affiliation_Identifier['ui:help']
Expand All @@ -71,10 +71,45 @@ class AffiliationIdentifier extends React.Component {
}
})
}
const required = this.props.required
const error = 'should be equal to one of the allowed values'
if ((this.props.rawErrors !== undefined && this.props.rawErrors.indexOf(error) >= 0) || (required && this.props.formData.Affiliation_Name == null)) {
label = <label className='text-danger form-label select-required'>{titleAffiliationName}*</label>

var parentContext = this.props.idSchema['$id'];
parentContext = parentContext.replace(this.props.idPrefix + '_', '');
parentContext = parentContext.replace('_Affiliation_', '');

var parts = parentContext.split('_');
parts.pop();
parentContext = parts.join('_');
console.log(parentContext);

try {
var bothRequired = this.props.registry.rootSchema.properties[parentContext].items.required.includes('Affiliation');
}
catch(err) {
var bothRequired = false;
}

var reqName = '';
var classesName = 'select-box';
var reqIdf = '';
var classesIdf = 'form-control';

var namePresent = !(typeof Affiliation_Name === "undefined" || Affiliation_Name.length == 0)
var idfPresent = !(typeof Affiliation_Identifier === "undefined" || Affiliation_Identifier.length == 0)

// Specific class handling for both elements Name/Identifier
if (namePresent) {
if (!idfPresent) {
classesIdf += ' is-invalid';
}
} else if (idfPresent) { // als naam wel is ingeuvuld
classesName += ' is-invalid';
} else if (bothRequired) {
classesName += ' is-invalid';
classesIdf += ' is-invalid';
}

// set the customStyle for the select if invalid
if (classesName.search('is-invalid')>-1) {
customStyles = {
control: styles => ({
...styles,
Expand All @@ -85,20 +120,29 @@ class AffiliationIdentifier extends React.Component {
}
})
}
} else if (required) {
label = <label className='form-label select-required select-filled'>{titleAffiliationName}*</label>
}

// Handling of '*' in the label of both fields
if (bothRequired) {
reqName = '*';
reqIdf = '*';
} else {
label = <label className='form-label select-required select-filled'>{titleAffiliationName}</label>
if (!(typeof Affiliation_Name === "undefined" || Affiliation_Name.length == 0)) {
reqIdf = '*';
}
if (!(typeof Affiliation_Identifier === "undefined" || Affiliation_Identifier.length == 0)) {
reqName = '*';
}
}

return (
<div className='d-flex'>
<div className='col compound-field'>
{label}
<label className='form-label select-required select-filled'>{titleAffiliationName}{reqName}</label>
<CreatableSelect
className='select-box'
className='select-box is-invalid'
options={this.options}
required={required}
required={reqName == '*'}
isDisabled={this.props.readonly}
placeholder={Affiliation_Name}
onChange={this.handleChange}
Expand All @@ -113,13 +157,17 @@ class AffiliationIdentifier extends React.Component {

<div className='col compound-field'>
<div className='mb-0 form-group'>
<label className='form-label'>{titleAffiliationIdentifier}</label>
<input type='text' className='form-control' readOnly={this.props.readonly} onChange={this.handleChangeIdentifier} value={Affiliation_Identifier} />
{helpAffiliationIdentifier && (
<small className='text-muted form-text'>
<p className='help-block'>{helpAffiliationIdentifier}</p>
</small>
)}
<label className='form-label'>{titleAffiliationIdentifier}{reqIdf}</label>
<input type='text'
className={classesIdf} //'form-control is-invalid'
readOnly={this.props.readonly}
onChange={this.handleChangeIdentifier}
value={Affiliation_Identifier} />
{helpAffiliationIdentifier && (
<small className='text-muted form-text'>
<p className='help-block'>{helpAffiliationIdentifier}</p>
</small>
)}
</div>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion deposit/static/deposit/js/metadata-form.js

Large diffs are not rendered by default.

84 changes: 66 additions & 18 deletions research/metadata-form/src/AffiliationIdentifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class AffiliationIdentifier extends React.Component {
const titleAffiliationName = this.props.schema.properties.Affiliation_Name.title
const titleAffiliationIdentifier = this.props.schema.properties.Affiliation_Identifier.title

let label = <label className='form-label'>{titleAffiliationName}</label>
// let label = <label className='form-label'>{titleAffiliationName}</label>

const helpAffiliationName = this.props.uiSchema.Affiliation_Name['ui:help']
const helpAffiliationIdentifier = this.props.uiSchema.Affiliation_Identifier['ui:help']
Expand All @@ -71,10 +71,45 @@ class AffiliationIdentifier extends React.Component {
}
})
}
const required = this.props.required
const error = 'should be equal to one of the allowed values'
if ((this.props.rawErrors !== undefined && this.props.rawErrors.indexOf(error) >= 0) || (required && this.props.formData.Affiliation_Name == null)) {
label = <label className='text-danger form-label select-required'>{titleAffiliationName}*</label>

var parentContext = this.props.idSchema['$id'];
parentContext = parentContext.replace(this.props.idPrefix + '_', '');
parentContext = parentContext.replace('_Affiliation_', '');

var parts = parentContext.split('_');
parts.pop();
parentContext = parts.join('_');
console.log(parentContext);

try {
var bothRequired = this.props.registry.rootSchema.properties[parentContext].items.required.includes('Affiliation');
}
catch(err) {
var bothRequired = false;
}

var reqName = '';
var classesName = 'select-box';
var reqIdf = '';
var classesIdf = 'form-control';

var namePresent = !(typeof Affiliation_Name === "undefined" || Affiliation_Name.length == 0)
var idfPresent = !(typeof Affiliation_Identifier === "undefined" || Affiliation_Identifier.length == 0)

// Specific class handling for both elements Name/Identifier
if (namePresent) {
if (!idfPresent) {
classesIdf += ' is-invalid';
}
} else if (idfPresent) { // als naam wel is ingeuvuld
classesName += ' is-invalid';
} else if (bothRequired) {
classesName += ' is-invalid';
classesIdf += ' is-invalid';
}

// set the customStyle for the select if invalid
if (classesName.search('is-invalid')>-1) {
customStyles = {
control: styles => ({
...styles,
Expand All @@ -85,20 +120,29 @@ class AffiliationIdentifier extends React.Component {
}
})
}
} else if (required) {
label = <label className='form-label select-required select-filled'>{titleAffiliationName}*</label>
}

// Handling of '*' in the label of both fields
if (bothRequired) {
reqName = '*';
reqIdf = '*';
} else {
label = <label className='form-label select-required select-filled'>{titleAffiliationName}</label>
if (!(typeof Affiliation_Name === "undefined" || Affiliation_Name.length == 0)) {
reqIdf = '*';
}
if (!(typeof Affiliation_Identifier === "undefined" || Affiliation_Identifier.length == 0)) {
reqName = '*';
}
}

return (
<div className='d-flex'>
<div className='col compound-field'>
{label}
<label className='form-label select-required select-filled'>{titleAffiliationName}{reqName}</label>
<CreatableSelect
className='select-box'
className='select-box is-invalid'
options={this.options}
required={required}
required={reqName == '*'}
isDisabled={this.props.readonly}
placeholder={Affiliation_Name}
onChange={this.handleChange}
Expand All @@ -113,13 +157,17 @@ class AffiliationIdentifier extends React.Component {

<div className='col compound-field'>
<div className='mb-0 form-group'>
<label className='form-label'>{titleAffiliationIdentifier}</label>
<input type='text' className='form-control' readOnly={this.props.readonly} onChange={this.handleChangeIdentifier} value={Affiliation_Identifier} />
{helpAffiliationIdentifier && (
<small className='text-muted form-text'>
<p className='help-block'>{helpAffiliationIdentifier}</p>
</small>
)}
<label className='form-label'>{titleAffiliationIdentifier}{reqIdf}</label>
<input type='text'
className={classesIdf} //'form-control is-invalid'
readOnly={this.props.readonly}
onChange={this.handleChangeIdentifier}
value={Affiliation_Identifier} />
{helpAffiliationIdentifier && (
<small className='text-muted form-text'>
<p className='help-block'>{helpAffiliationIdentifier}</p>
</small>
)}
</div>
</div>
</div>
Expand Down
14 changes: 9 additions & 5 deletions research/research.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ def index() -> Response:
def download() -> Response:
path = '/' + g.irods.zone + '/home' + request.args.get('filepath')
filename = path.rsplit('/', 1)[1]
READ_BUFFER_SIZE = 1024 * io.DEFAULT_BUFFER_SIZE

def read_file_chunks(path: str) -> Iterator[bytes]:
obj = g.irods.data_objects.get(path)
def read_file_chunks(data_object) -> Iterator[bytes]:
READ_BUFFER_SIZE = 1024 * io.DEFAULT_BUFFER_SIZE

try:
with obj.open('r') as fd:
with data_object.open('r') as fd:
while True:
buf = fd.read(READ_BUFFER_SIZE)
if buf:
Expand All @@ -61,10 +61,14 @@ def read_file_chunks(path: str) -> Iterator[bytes]:
abort(500)

if g.irods.data_objects.exists(path):
data_object = g.irods.data_objects.get(path)
size = data_object.replicas[0].size

return Response(
stream_with_context(read_file_chunks(path)),
stream_with_context(read_file_chunks(data_object)),
headers={
'Content-Disposition': f'attachment; filename={filename}',
'Content-Length': f'{size}',
'Content-Type': 'application/octet'
}
)
Expand Down
6 changes: 3 additions & 3 deletions research/static/research/js/metadata-form.js

Large diffs are not rendered by default.

Loading

0 comments on commit 328288f

Please sign in to comment.