Skip to content

Commit

Permalink
Handle empty image / file fields.
Browse files Browse the repository at this point in the history
  • Loading branch information
robgietema committed May 19, 2023
1 parent fffe85c commit 3ecd608
Showing 1 changed file with 25 additions and 21 deletions.
46 changes: 25 additions & 21 deletions src/models/document/document.js
Expand Up @@ -354,35 +354,39 @@ export class Document extends Model {
const fileFields = this._type.getFactoryFields('File');
mapSync(fileFields, (field) => {
// Set data
json[field] = {
'content-type': json[field]['content-type'],
download: `${this.getUrl(req)}/@@download/file`,
filename: json[field].filename,
size: json[field].size,
};
if (json[field]) {
json[field] = {
'content-type': json[field]['content-type'],
download: `${this.getUrl(req)}/@@download/file`,
filename: json[field].filename,
size: json[field].size,
};
}
});

// Loop through image fields
const imageFields = this._type.getFactoryFields('Image');
mapSync(imageFields, (field) => {
// Set data
json[field] = {
'content-type': json[field]['content-type'],
download: `${this.getUrl(req)}/@@images/${json[field].uuid}.${last(
json[field].filename.split('.'),
)}`,
filename: json[field].filename,
size: json[field].size,
width: json[field].width,
height: json[field].height,
scales: mapValues(json[field].scales, (scale) => ({
width: scale.width,
height: scale.height,
download: `${this.getUrl(req)}/@@images/${scale.uuid}.${last(
if (json[field]) {
json[field] = {
'content-type': json[field]['content-type'],
download: `${this.getUrl(req)}/@@images/${json[field].uuid}.${last(
json[field].filename.split('.'),
)}`,
})),
};
filename: json[field].filename,
size: json[field].size,
width: json[field].width,
height: json[field].height,
scales: mapValues(json[field].scales, (scale) => ({
width: scale.width,
height: scale.height,
download: `${this.getUrl(req)}/@@images/${scale.uuid}.${last(
json[field].filename.split('.'),
)}`,
})),
};
}
});

// Loop through relation list fields
Expand Down

0 comments on commit 3ecd608

Please sign in to comment.