Skip to content

Commit 6328f4d

Browse files
committed
feat(core/forms): Remove file content if greather than the max size limit
1 parent 6c768b7 commit 6328f4d

File tree

5 files changed

+14
-47
lines changed

5 files changed

+14
-47
lines changed

projects/core/file-input/src/file-input.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ import {DomSanitizer, SafeResourceUrl} from '@angular/platform-browser';
4242
import {Observable, Subscription} from 'rxjs';
4343
import {filter} from 'rxjs/operators';
4444

45-
import {AjfFile} from './file';
45+
import {AjfFile, AjfFileSizeLimit} from './file';
4646

4747
@Directive({selector: '[ajfDropMessage]'})
4848
export class AjfDropMessage {}
@@ -247,6 +247,11 @@ export class AjfFileInput implements ControlValueAccessor {
247247
if (!isValidMimeType(type, this.accept)) {
248248
return;
249249
}
250+
if (size >= AjfFileSizeLimit) {
251+
this.value = {name, size, type, content: 'File too large'};
252+
this._emptyFile = false;
253+
return;
254+
}
250255
reader.onload = (_: ProgressEvent<FileReader>) => {
251256
const content = reader.result;
252257
if (typeof content !== 'string') {

projects/core/file-input/src/file.ts

+5
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,8 @@ export interface AjfFile {
2828
url?: string;
2929
deleteUrl: boolean;
3030
}
31+
32+
/**
33+
* Default size limit for file input fields.
34+
*/
35+
export const AjfFileSizeLimit = 52428800;

projects/core/forms/src/form-renderer.ts

-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ import {updateRepsNum} from './utils/slides-instances/update-reps-num';
9797
import {validSlide} from './utils/slides-instances/valid-slide';
9898
import {AjfValidationService} from './validation-service';
9999
import {isContainerNodeInstance} from './utils/nodes-instances/is-container-node-instance';
100-
import {isFileFieldInstance} from './utils/fields-instances/is-file-field-instance';
101100

102101
export const enum AjfFormInitStatus {
103102
Initializing,

projects/core/forms/src/utils/fields-instances/is-file-field-instance.ts

-37
This file was deleted.

projects/core/forms/src/utils/nodes-instances/node-to-node-instance.ts

+3-8
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
normalizeExpression,
2929
} from '@ajf/core/models';
3030

31+
import {AjfFileSizeLimit} from '@ajf/core/file-input';
3132
import {AjfField} from '../../interface/fields/field';
3233
import {AjfFieldType} from '../../interface/fields/field-type';
3334
import {AjfNodeInstance} from '../../interface/nodes-instances/node-instance';
@@ -60,12 +61,6 @@ import {isNodeGroupInstance} from './is-node-group-instance';
6061
import {isRepeatingSlideInstance} from './is-repeating-slide-instance';
6162
import {isSlideInstance} from './is-slide-instance';
6263

63-
/**
64-
* Default size limit for file input fields.
65-
* Added in validation conditions only if no size limit condition is found.
66-
*/
67-
export const fileSizeLimit = 52428800;
68-
6964
/**
7065
* It creates a nodeInstance relative to a node.
7166
* To create the instance it calls relative create builder by nodeType.
@@ -115,9 +110,9 @@ export function nodeToNodeInstance(
115110
}
116111
if (!limitSizeCondition) {
117112
field.validation.conditions.push({
118-
'condition': `${field.name} == null || ${field.name}.size < ${fileSizeLimit}`,
113+
'condition': `${field.name} == null || ${field.name}.size < ${AjfFileSizeLimit}`,
119114
'clientValidation': true,
120-
'errorMessage': `The file exceeds the ${(fileSizeLimit / 1024 ** 2).toFixed(
115+
'errorMessage': `The file exceeds the ${(AjfFileSizeLimit / 1024 ** 2).toFixed(
121116
0,
122117
)} MB limit`,
123118
});

0 commit comments

Comments
 (0)