Skip to content

Commit

Permalink
Merge pull request #826 from GBishop-PHSA/feature/gh-808
Browse files Browse the repository at this point in the history
gh-808 Attachment file size limit validation
  • Loading branch information
joe-crawford authored Apr 22, 2024
2 parents 159b37c + a159fe2 commit a019ea2
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/app/model/api-properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,14 @@ export const propertyMetadata: ApiPropertyMetadata[] = [
publiclyVisible: true,
requiresReload: true,
},
{
key: 'feature.attachment_size_limit_mb',
category: 'Features',
editType: ApiPropertyEditType.Value,
isSystem: true,
publiclyVisible: true,
requiresReload: true
},
{
key: 'feature.copy_annotations_to_new_version',
category: 'Features',
Expand Down
37 changes: 35 additions & 2 deletions src/app/shared/attachment-list/attachment-list.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ import {
ViewChildren,
ViewChild,
ElementRef,
EventEmitter
EventEmitter,
ChangeDetectorRef
} from '@angular/core';
import { MdmResourcesService } from '@mdm/modules/resources';
import { MessageHandlerService } from '@mdm/services/utility/message-handler.service';
Expand All @@ -40,7 +41,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,12 +79,16 @@ export class AttachmentListComponent implements AfterViewInit {
EditableRecord<ReferenceFile, ReferenceFileEditor>
>();
apiEndpoint: string;
attachmentFileSizeLimit = 0;

private readonly attachmentFileSizeLimitKey = 'feature.attachment_size_limit_mb';

constructor(
private resources: MdmResourcesService,
private messageHandler: MessageHandlerService,
private sharedService: SharedService,
private editingService: EditingService,
private changeRef: ChangeDetectorRef,
private gridService: GridService,
private dialog: MatDialog
) {}
Expand All @@ -90,6 +97,7 @@ export class AttachmentListComponent implements AfterViewInit {
this.apiEndpoint = this.sharedService.backendURL;

this.canEdit = this.parent.availableActions.includes('update');
this.changeRef.detectChanges();

this.sort.sortChange.subscribe(() => (this.paginator.pageIndex = 0));
this.dataSource.sort = this.sort;
Expand Down Expand Up @@ -137,6 +145,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 +289,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 +315,12 @@ export class AttachmentListComponent implements AfterViewInit {
});
};
}

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

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

0 comments on commit a019ea2

Please sign in to comment.