Skip to content

Commit

Permalink
MauroDataMappergh-808 Attachment file size limit validation
Browse files Browse the repository at this point in the history
  • Loading branch information
GBishop-PHSA committed Jun 16, 2023
1 parent 2a35857 commit 94c8ddc
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/app/model/api-properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,5 +334,13 @@ export const propertyMetadata: ApiPropertyMetadata[] = [
isSystem: true,
publiclyVisible: true,
requiresReload: true,
},
{
key: 'feature.attachment_size_limit_mb',
category: 'Features',
editType: ApiPropertyEditType.Value,
isSystem: true,
publiclyVisible: true,
requiresReload: true
}
];
32 changes: 31 additions & 1 deletion src/app/shared/attachment-list/attachment-list.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ import {
ReferenceFile,
ReferenceFileCreatePayload,
ReferenceFileIndexResponse,
Securable
Securable,
ApiProperty,
ApiPropertyIndexResponse
} from '@maurodatamapper/mdm-resources';
import { EditableRecord } from '@mdm/model/editable-forms';
import { MatDialog } from '@angular/material/dialog';
Expand Down Expand Up @@ -76,6 +78,9 @@ export class AttachmentListComponent implements AfterViewInit {
EditableRecord<ReferenceFile, ReferenceFileEditor>
>();
apiEndpoint: string;
attachmentFileSizeLimit = 0;

private readonly attachmentFileSizeLimitKey = 'feature.attachment_size_limit_mb';

constructor(
private resources: MdmResourcesService,
Expand Down Expand Up @@ -137,6 +142,18 @@ export class AttachmentListComponent implements AfterViewInit {

this.dataSource.data = this.records;
});

this.resources.apiProperties
.listPublic()
.pipe(
catchError(errors => {
this.messageHandler.showError('There was a problem getting the configuration properties.', errors);
return [];
})
)
.subscribe((response: ApiPropertyIndexResponse) => {
this.loadAttachmentFileSizeLimit(response.body.items);
});
}

applyFilter() {
Expand Down Expand Up @@ -269,6 +286,11 @@ export class AttachmentListComponent implements AfterViewInit {
fileContents: Array.from(fileBytes)
};

if (this.attachmentFileSizeLimit > 0 && +file.size/1000000 > this.attachmentFileSizeLimit) {
this.messageHandler.showError(`There was a problem saving the attachment. Files cannot be larger than ${this.attachmentFileSizeLimit}mb.`);
return EMPTY;
}

this.resources.catalogueItem
.saveReferenceFiles(this.domainType, this.parent.id, data)
.pipe(
Expand All @@ -290,4 +312,12 @@ export class AttachmentListComponent implements AfterViewInit {
});
};
}

private loadAttachmentFileSizeLimit(properties: ApiProperty[]) {
this.attachmentFileSizeLimit = JSON.parse(this.getContentProperty(properties, this.attachmentFileSizeLimitKey));
}

private getContentProperty(properties: ApiProperty[], key: string): string {
return properties?.find(p => p.key === key)?.value;
}
}

0 comments on commit 94c8ddc

Please sign in to comment.