Skip to content

Commit 1e2b99d

Browse files
committed
ufal/upload-on-first-attempt-fix (#435)
* Moved file size limit into FileUploader options and handled Upload cancelation. * Check that uploading is successful
1 parent 4b3fd9d commit 1e2b99d

File tree

2 files changed

+45
-26
lines changed

2 files changed

+45
-26
lines changed

cypress/e2e/submission.cy.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ describe('New Submission page', () => {
122122

123123
// Wait for upload to complete before proceeding
124124
cy.wait('@upload');
125+
// Check the upload success notice
126+
cy.get('ds-notification').contains('Upload successful');
127+
// Close the upload success notice
128+
cy.get('[data-dismiss="alert"]').click({multiple: true});
125129

126130
// Wait for deposit button to not be disabled & click it.
127131
cy.get('button#deposit').should('not.be.disabled').click();

src/app/shared/upload/uploader/uploader.component.ts

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
1-
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, EventEmitter, HostListener, Input, Output, ViewEncapsulation, } from '@angular/core';
1+
import {
2+
AfterViewInit,
3+
ChangeDetectionStrategy,
4+
ChangeDetectorRef,
5+
Component,
6+
EventEmitter,
7+
HostListener,
8+
Input,
9+
OnInit,
10+
Output,
11+
ViewEncapsulation,
12+
} from '@angular/core';
213

314
import { firstValueFrom, Observable, of as observableOf } from 'rxjs';
4-
import { FileItem, FileUploader } from 'ng2-file-upload';
15+
import { FileUploader, FileUploaderOptions} from 'ng2-file-upload';
516
import uniqueId from 'lodash/uniqueId';
617
import { ScrollToService } from '@nicky-lenaers/ngx-scroll-to';
718

@@ -18,6 +29,7 @@ import { getFirstCompletedRemoteData } from '../../../core/shared/operators';
1829
import { RemoteData } from '../../../core/data/remote-data';
1930
import { ConfigurationProperty } from '../../../core/shared/configuration-property.model';
2031
import { TranslateService } from '@ngx-translate/core';
32+
import { FileLikeObject } from 'ng2-file-upload/file-upload/file-like-object.class';
2133

2234
export const MAX_UPLOAD_FILE_SIZE_CFG_PROPERTY = 'spring.servlet.multipart.max-file-size';
2335
@Component({
@@ -28,7 +40,7 @@ export const MAX_UPLOAD_FILE_SIZE_CFG_PROPERTY = 'spring.servlet.multipart.max-f
2840
encapsulation: ViewEncapsulation.Emulated
2941
})
3042

31-
export class UploaderComponent {
43+
export class UploaderComponent implements OnInit, AfterViewInit {
3244

3345
/**
3446
* The message to show when drag files on the drop zone
@@ -120,6 +132,12 @@ export class UploaderComponent {
120132
queueLimit: this.uploadFilesOptions.maxFileNumber,
121133
});
122134

135+
// Update the max file size in the uploader options. Fetch the max file size from the BE configuration.
136+
void this.getMaxFileSizeInBytes().then((maxFileSize) => {
137+
this.uploader.options.maxFileSize = maxFileSize === -1 ? undefined : maxFileSize;
138+
this.uploader.setOptions(this.uploader.options);
139+
});
140+
123141
if (isUndefined(this.enableDragOverDocument)) {
124142
this.enableDragOverDocument = false;
125143
}
@@ -139,19 +157,6 @@ export class UploaderComponent {
139157
this.onBeforeUpload = () => {return;};
140158
}
141159
this.uploader.onBeforeUploadItem = async (item) => {
142-
// Check if the file size is within the maximum upload size
143-
const canUpload = await this.checkFileSizeLimit(item);
144-
// If the file size is too large, emit an error and cancel all uploads
145-
if (!canUpload) {
146-
this.onUploadError.emit({
147-
item: item,
148-
response: this.translate.instant('submission.sections.upload.upload-failed.size-limit-exceeded'),
149-
status: 400,
150-
headers: {}
151-
});
152-
this.uploader.cancelAll();
153-
return;
154-
}
155160
if (item.url !== this.uploader.options.url) {
156161
item.url = this.uploader.options.url;
157162
}
@@ -193,6 +198,16 @@ export class UploaderComponent {
193198
this.onUploadError.emit({ item: item, response: response, status: status, headers: headers });
194199
this.uploader.cancelAll();
195200
};
201+
this.uploader.onWhenAddingFileFailed = (item: any, filter: any, options: any) => {
202+
if (this.itemFileSizeExceeded(item, options)) {
203+
this.onUploadError.emit({
204+
item: item,
205+
response: this.translate.instant('submission.sections.upload.upload-failed.size-limit-exceeded'),
206+
status: 400,
207+
headers: {}
208+
});
209+
}
210+
};
196211
this.uploader.onProgressAll = () => this.onProgress();
197212
this.uploader.onProgressItem = () => this.onProgress();
198213
}
@@ -247,20 +262,20 @@ export class UploaderComponent {
247262
this.cookieService.set(XSRF_COOKIE, token);
248263
}
249264

250-
// Check if the file size is within the maximum upload size
251-
private async checkFileSizeLimit(item: FileItem): Promise<boolean> {
265+
private async getMaxFileSizeInBytes() {
252266
const maxFileUploadSize = await firstValueFrom(this.getMaxFileUploadSizeFromCfg());
253267
if (maxFileUploadSize) {
254268
const maxSizeInGigabytes = parseInt(maxFileUploadSize?.[0], 10);
255-
const maxSizeInBytes = this.gigabytesToBytes(maxSizeInGigabytes);
256-
// If maxSizeInBytes is -1, it means the value in the config is invalid. The file won't be uploaded and the user
257-
// will see error messages in the UI.
258-
if (maxSizeInBytes === -1) {
259-
return false;
260-
}
261-
return item?.file?.size <= maxSizeInBytes;
269+
return this.gigabytesToBytes(maxSizeInGigabytes);
262270
}
263-
return false;
271+
// If maxSizeInBytes is -1, it means the value in the config is invalid. The file won't be uploaded and the user
272+
// will see error messages in the UI.
273+
return -1;
274+
}
275+
// Check if the file size is exceeded the maximum upload size
276+
private itemFileSizeExceeded(item: FileLikeObject, options: FileUploaderOptions): boolean {
277+
const maxSizeInBytes = options.maxFileSize;
278+
return item?.size > maxSizeInBytes;
264279
}
265280

266281
// Convert gigabytes to bytes

0 commit comments

Comments
 (0)