Skip to content

Commit 59e3d0b

Browse files
committed
fix: make acceptable file types for attachments configurable
1 parent 607778a commit 59e3d0b

File tree

7 files changed

+55
-3
lines changed

7 files changed

+55
-3
lines changed

src/app/core/config/config-fix.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1049,7 +1049,10 @@ export const defaultJsonConfig = {
10491049
},
10501050
"birth_certificate": {
10511051
"dataType": "file",
1052-
"label": $localize`:Label for a child attribute:Birth certificate`
1052+
"label": $localize`:Label for a child attribute:Birth certificate`,
1053+
"additional": {
1054+
"acceptedFileTypes": ".pdf"
1055+
}
10531056
}
10541057
},
10551058
} as EntityConfig,

src/app/features/file/edit-file/edit-file.component.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<input
22
type="file"
3+
[accept]="acceptedFileTypes"
34
style="display: none"
45
(change)="onFileSelected($event.target['files'][0])"
56
#fileUpload

src/app/features/file/edit-file/edit-file.component.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { FileService } from "../file.service";
1616
import { EntitySchemaService } from "../../../core/entity/schema/entity-schema.service";
1717
import { EntityMapperService } from "../../../core/entity/entity-mapper/entity-mapper.service";
1818
import { NAVIGATOR_TOKEN } from "../../../utils/di-tokens";
19+
import { FileFieldConfig } from "../file.datatype";
1920

2021
describe("EditFileComponent", () => {
2122
let component: EditFileComponent;
@@ -57,6 +58,19 @@ describe("EditFileComponent", () => {
5758
expect(component).toBeTruthy();
5859
});
5960

61+
it("should use acceptedFileTypes from field config", () => {
62+
setupComponent();
63+
component.formFieldConfig = {
64+
id: "testProp",
65+
dataType: "file",
66+
additional: { acceptedFileTypes: ".png" } as FileFieldConfig,
67+
};
68+
69+
component.ngOnInit();
70+
71+
expect(component.acceptedFileTypes).toBe(".png");
72+
});
73+
6074
it("should not upload a file that was selected but the process was canceled", () => {
6175
setupComponent();
6276
component.formControl.enable();

src/app/features/file/edit-file/edit-file.component.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { FontAwesomeModule } from "@fortawesome/angular-fontawesome";
2222
import { ErrorHintComponent } from "../../../core/common-components/error-hint/error-hint.component";
2323
import { NotAvailableOfflineError } from "../../../core/session/not-available-offline.error";
2424
import { NAVIGATOR_TOKEN } from "../../../utils/di-tokens";
25+
import { FileFieldConfig } from "../file.datatype";
2526

2627
/**
2728
* This component should be used as a `editComponent` when a property should store files.
@@ -51,6 +52,18 @@ export class EditFileComponent extends EditComponent<string> implements OnInit {
5152
private removeClicked = false;
5253
initialValue: string;
5354

55+
/**
56+
* config for the given form field / entity attribute, containing special settings for this component.
57+
* (re-declared here for better typing)
58+
*/
59+
declare additional: FileFieldConfig;
60+
61+
/**
62+
* The accepted file types for file selection dialog.
63+
* If not defined, allows any file.
64+
*/
65+
acceptedFileTypes: string = "*";
66+
5467
constructor(
5568
protected fileService: FileService,
5669
private alertService: AlertService,
@@ -63,6 +76,10 @@ export class EditFileComponent extends EditComponent<string> implements OnInit {
6376
override ngOnInit() {
6477
super.ngOnInit();
6578
this.initialValue = this.formControl.value;
79+
80+
this.acceptedFileTypes =
81+
this.additional?.acceptedFileTypes ?? this.acceptedFileTypes;
82+
6683
this.formControl.statusChanges
6784
.pipe(
6885
distinctUntilChanged(),

src/app/features/file/edit-photo/edit-photo.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
type="file"
3636
style="display: none"
3737
(change)="onFileSelected($event.target['files'][0])"
38-
accept="image/*"
38+
[accept]="acceptedFileTypes"
3939
#fileUpload
4040
/>
4141
</div>

src/app/features/file/edit-photo/edit-photo.component.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ export class EditPhotoComponent extends EditFileComponent implements OnInit {
5858

5959
override ngOnInit() {
6060
super.ngOnInit();
61-
this.compression = this.additional ?? this.compression;
61+
this.compression = this.additional?.imageCompression ?? this.compression;
62+
this.acceptedFileTypes = this.additional?.acceptedFileTypes ?? "image/*";
6263
if (this.formControl.value) {
6364
this.fileService
6465
.loadFile(this.entity, this.formControlName)

src/app/features/file/file.datatype.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,19 @@ export class FileDatatype extends StringDatatype {
4343
);
4444
}
4545
}
46+
47+
/**
48+
* (Optional) "additional" object to configure details of a "file" datatype / form field.
49+
*/
50+
export interface FileFieldConfig {
51+
/**
52+
* The accepted file types for file selection dialog.
53+
* If not defined, allows any file.
54+
*/
55+
acceptedFileTypes?: string;
56+
57+
/**
58+
* The maxSize to which the image will be automatically resized before upload.
59+
*/
60+
imageCompression?: number;
61+
}

0 commit comments

Comments
 (0)