From 3d3b55b4a6f0ae5f53d4dc49dc6e753a3fdabef4 Mon Sep 17 00:00:00 2001 From: milanmajchrak Date: Wed, 22 Jul 2026 20:59:49 +0200 Subject: [PATCH 1/5] feat(submission): say when a file is being ingested instead of just "Saving" An ingest from a server path happens inside the save request, so for a large dataset the submission form sat on a bare "Saving..." for minutes with nothing to say a transfer had started or whether the tab could be closed. While a save carries a pending local.bitstream.redirectToURL value the progress bar now reads "Ingesting file from server..." with a running mm:ss counter, alongside a note that the file is being read directly from the server and the tab should stay open. Ordinary saves are unchanged. The elapsed time is measured from wall-clock timestamps rather than by counting timer ticks, because browsers throttle timers in a backgrounded tab - exactly where a long ingest is left running. The pending state is latched when the save is dispatched, so the label describes the request actually in flight rather than a form that has since changed, and a pending removal of the field wins over a value still sitting in the section data, so clearing the field is not reported as an ingest. "Saving..." and "Depositing..." were hard-coded English in the template and are now translated like everything else. Co-Authored-By: Claude Opus 4.8 (1M context) --- ...datashare-upload-from-path.service.spec.ts | 218 ++++++++++++++++++ .../datashare-upload-from-path.service.ts | 180 +++++++++++++++ .../submission-form-footer.component.html | 22 +- .../submission-form-footer.component.scss | 12 + .../submission-form-footer.component.spec.ts | 203 +++++++++++++++- .../submission-form-footer.component.ts | 91 +++++++- src/assets/i18n/en.json5 | 10 + 7 files changed, 722 insertions(+), 14 deletions(-) create mode 100644 src/app/datashare/datashare-upload-from-path.service.spec.ts create mode 100644 src/app/datashare/datashare-upload-from-path.service.ts diff --git a/src/app/datashare/datashare-upload-from-path.service.spec.ts b/src/app/datashare/datashare-upload-from-path.service.spec.ts new file mode 100644 index 00000000000..44fb3b1e0b1 --- /dev/null +++ b/src/app/datashare/datashare-upload-from-path.service.spec.ts @@ -0,0 +1,218 @@ +import { BehaviorSubject } from 'rxjs'; + +import { JsonPatchOperationType } from '../core/json-patch/json-patch.model'; +import { + JsonPatchOperationObject, + JsonPatchOperationsResourceEntry, +} from '../core/json-patch/json-patch-operations.reducer'; +import { SubmissionObjectEntry } from '../submission/objects/submission-objects.reducer'; +import { DatashareUploadFromPathService } from './datashare-upload-from-path.service'; + +describe('DatashareUploadFromPathService', () => { + + const submissionId = '826'; + const sectionId = 'metadatapageone'; + const otherSectionId = 'sectionOfAnotherSubmission'; + const serverPath = '/srv/dspace/ingest/dataset.zip'; + + let submission$: BehaviorSubject; + let sectionOperations$: BehaviorSubject; + let service: DatashareUploadFromPathService; + + const submissionWithSectionData = (data: any): SubmissionObjectEntry => ({ + sections: { + [sectionId]: { data } as any, + }, + }); + + const operationOn = (path: string, value: any): JsonPatchOperationObject => ({ + operation: { + op: JsonPatchOperationType.add, + path, + value, + }, + timeCompleted: 1, + }); + + const pendingOperations = (children: { [resourceId: string]: JsonPatchOperationObject[] }): JsonPatchOperationsResourceEntry => ({ + children: Object.keys(children).reduce((acc: any, key: string) => Object.assign(acc, { [key]: { body: children[key] } }), {}), + transactionStartTime: null, + commitPending: false, + }); + + const pathOperation = (value: any, onSectionId = sectionId): JsonPatchOperationObject => + operationOn(`/sections/${onSectionId}/${DatashareUploadFromPathService.PATH_FIELD}`, value); + + const removeOperationOn = (path: string): JsonPatchOperationObject => ({ + operation: { + op: JsonPatchOperationType.remove, + path, + value: null, + }, + timeCompleted: 2, + }); + + const pathRemoveOperation = (onSectionId = sectionId): JsonPatchOperationObject => + removeOperationOn(`/sections/${onSectionId}/${DatashareUploadFromPathService.PATH_FIELD}`); + + const collectEmissions = (): boolean[] => { + const emitted: boolean[] = []; + service.isPendingForSubmission(submissionId).subscribe((pending: boolean) => emitted.push(pending)); + return emitted; + }; + + beforeEach(() => { + submission$ = new BehaviorSubject(submissionWithSectionData({})); + sectionOperations$ = new BehaviorSubject(pendingOperations({ [sectionId]: [] })); + + const submissionServiceMock = { + getSubmissionObject: () => submission$.asObservable(), + }; + const storeMock = { + select: () => sectionOperations$.asObservable(), + }; + + service = new DatashareUploadFromPathService(submissionServiceMock as any, storeMock as any); + }); + + it('should emit true when the submission already carries a non-empty path value', () => { + submission$.next(submissionWithSectionData({ + 'dc.title': [{ value: 'A dataset' }], + [DatashareUploadFromPathService.PATH_FIELD]: [{ value: serverPath }], + })); + + expect(collectEmissions()).toEqual([true]); + }); + + it('should emit true while an unsaved patch operation carries a non-empty path value', () => { + sectionOperations$.next(pendingOperations({ + [sectionId]: [pathOperation([{ value: serverPath }])], + })); + + expect(collectEmissions()).toEqual([true]); + }); + + it('should emit false when the path field is absent', () => { + submission$.next(submissionWithSectionData({ 'dc.title': [{ value: 'A dataset' }] })); + + expect(collectEmissions()).toEqual([false]); + }); + + it('should emit false when the path field holds only blank values', () => { + submission$.next(submissionWithSectionData({ + [DatashareUploadFromPathService.PATH_FIELD]: [{ value: ' ' }], + })); + sectionOperations$.next(pendingOperations({ + [sectionId]: [pathOperation([{ value: '' }])], + })); + + expect(collectEmissions()).toEqual([false]); + }); + + it('should emit false when a pending remove clears a path that is still in the saved section data', () => { + // Section data is only refreshed from a server response, so it still holds the old path while the + // administrator is clearing the field. The save will ingest nothing, and must not be labelled as + // an ingest. A value can be in the field without this feature having put it there - an import or + // the item-edit page can set it too. + submission$.next(submissionWithSectionData({ + [DatashareUploadFromPathService.PATH_FIELD]: [{ value: serverPath }], + })); + sectionOperations$.next(pendingOperations({ + [sectionId]: [pathRemoveOperation()], + })); + + expect(collectEmissions()).toEqual([false]); + }); + + it('should still emit true when the saved path is left untouched by the pending operations', () => { + submission$.next(submissionWithSectionData({ + [DatashareUploadFromPathService.PATH_FIELD]: [{ value: serverPath }], + })); + sectionOperations$.next(pendingOperations({ + [sectionId]: [operationOn(`/sections/${sectionId}/dc.title`, [{ value: 'A dataset' }])], + })); + + expect(collectEmissions()).toEqual([true]); + }); + + it('should emit false when a later remove cancels the add of a path', () => { + sectionOperations$.next(pendingOperations({ + [sectionId]: [pathOperation([{ value: serverPath }]), pathRemoveOperation()], + })); + + expect(collectEmissions()).toEqual([false]); + }); + + it('should emit true when a path is added again after being removed', () => { + sectionOperations$.next(pendingOperations({ + [sectionId]: [ + pathOperation([{ value: serverPath }]), + pathRemoveOperation(), + pathOperation([{ value: serverPath }]), + ], + })); + + expect(collectEmissions()).toEqual([true]); + }); + + it('should emit false when a later operation replaces the path with whitespace', () => { + sectionOperations$.next(pendingOperations({ + [sectionId]: [pathOperation([{ value: serverPath }]), pathOperation([{ value: ' \t ' }])], + })); + + expect(collectEmissions()).toEqual([false]); + }); + + it('should not let a remove on another field cancel the path', () => { + sectionOperations$.next(pendingOperations({ + [sectionId]: [ + pathOperation([{ value: serverPath }]), + removeOperationOn(`/sections/${sectionId}/dc.title`), + ], + })); + + expect(collectEmissions()).toEqual([true]); + }); + + it('should ignore pending operations belonging to a section of another submission', () => { + sectionOperations$.next(pendingOperations({ + [otherSectionId]: [pathOperation([{ value: serverPath }], otherSectionId)], + })); + + expect(collectEmissions()).toEqual([false]); + }); + + it('should ignore pending operations on other metadata fields', () => { + sectionOperations$.next(pendingOperations({ + [sectionId]: [operationOn(`/sections/${sectionId}/dc.title`, [{ value: serverPath }])], + })); + + expect(collectEmissions()).toEqual([false]); + }); + + it('should not re-emit when an unrelated change occurs', () => { + const emitted = collectEmissions(); + + submission$.next(submissionWithSectionData({ 'dc.title': [{ value: 'A dataset' }] })); + sectionOperations$.next(pendingOperations({ + [sectionId]: [operationOn(`/sections/${sectionId}/dc.title`, [{ value: 'A dataset' }])], + })); + + expect(emitted).toEqual([false]); + }); + + it('should emit once per change of the pending state', () => { + const emitted = collectEmissions(); + + sectionOperations$.next(pendingOperations({ + [sectionId]: [pathOperation([{ value: serverPath }])], + })); + submission$.next(submissionWithSectionData({ + [DatashareUploadFromPathService.PATH_FIELD]: [{ value: serverPath }], + })); + sectionOperations$.next(pendingOperations({ [sectionId]: [] })); + + expect(emitted).toEqual([false, true]); + }); + +}); diff --git a/src/app/datashare/datashare-upload-from-path.service.ts b/src/app/datashare/datashare-upload-from-path.service.ts new file mode 100644 index 00000000000..1d58b20cb9e --- /dev/null +++ b/src/app/datashare/datashare-upload-from-path.service.ts @@ -0,0 +1,180 @@ +import { Injectable } from '@angular/core'; +import { Store } from '@ngrx/store'; +import { + combineLatest as observableCombineLatest, + Observable, +} from 'rxjs'; +import { + distinctUntilChanged, + map, + startWith, +} from 'rxjs/operators'; + +import { CoreState } from '../core/core-state.model'; +import { JsonPatchOperationType } from '../core/json-patch/json-patch.model'; +import { + JsonPatchOperationObject, + JsonPatchOperationsResourceEntry, +} from '../core/json-patch/json-patch-operations.reducer'; +import { jsonPatchOperationsByResourceType } from '../core/json-patch/selectors'; +import { hasValue } from '../shared/empty.util'; +import { + SubmissionObjectEntry, + SubmissionSectionEntry, +} from '../submission/objects/submission-objects.reducer'; +import { SubmissionService } from '../submission/submission.service'; + +/** + * The JSON PATCH resource type under which every submission section stores its unsaved operations. + */ +const SECTIONS_RESOURCE_TYPE = 'sections'; + +/** + * Tells the submission UI whether the next (or currently running) save will make the server ingest a + * bitstream from a path on its own filesystem, so that the user can be shown an honest long-running + * indicator instead of the ordinary "Saving" one. + * + * The value is read from two places, because neither on its own covers the whole window during which + * the indicator has to be shown: + *
    + *
  • the unsaved JSON PATCH operations, which hold the path from the moment it is typed until the + * PATCH response arrives — this is the window in which the ingest actually happens;
  • + *
  • the submission object in the store, whose section data only ever holds server-supplied values, + * and therefore only carries the path if a previous ingest did not clear it.
  • + *
+ */ +@Injectable({ + providedIn: 'root', +}) +export class DatashareUploadFromPathService { + + /** + * Metadata field whose presence means that a save will trigger a server-side ingest. + */ + public static readonly PATH_FIELD = 'local.bitstream.redirectToURL'; + + constructor(private submissionService: SubmissionService, + private store: Store) { + } + + /** + * Emits true while the given submission carries a non-empty {@link PATH_FIELD} value, either + * already saved on the server or waiting in an unsaved JSON PATCH operation. + * + * @param submissionId the id of the submission being edited + * @return an observable that emits only when the pending state changes + */ + public isPendingForSubmission(submissionId: string): Observable { + const sections$: Observable = this.submissionService.getSubmissionObject(submissionId).pipe( + map((submission: SubmissionObjectEntry) => hasValue(submission) && hasValue(submission.sections) ? submission.sections : {}), + startWith({} as SubmissionSectionEntry), + ); + + const operations$: Observable = + this.store.select(jsonPatchOperationsByResourceType(SECTIONS_RESOURCE_TYPE)); + + return observableCombineLatest([sections$, operations$]).pipe( + map(([sections, operations]: [SubmissionSectionEntry, JsonPatchOperationsResourceEntry]) => { + // Section data is only ever refreshed from a server response, so it still holds the old value + // while the user is clearing the field. A pending operation therefore has the final say, and + // the saved value is consulted only when nothing pending touches the field at all. + const pending: boolean = this.pendingOutcome(sections, operations); + return hasValue(pending) ? pending : this.isInSectionData(sections); + }), + distinctUntilChanged(), + ); + } + + /** + * Does any section of the submission already hold a saved path value? + * + * @param sections the submission's sections as held in the store + * @return true when at least one section's data carries a non-blank path + */ + private isInSectionData(sections: SubmissionSectionEntry): boolean { + return Object.keys(sections) + .map((sectionId: string) => sections[sectionId]?.data) + .some((data: any) => hasValue(data) && this.isNonBlankValue(data[DatashareUploadFromPathService.PATH_FIELD])); + } + + /** + * What do the unsaved JSON PATCH operations of the submission do to the path field? + * + * The JSON PATCH state is keyed by section id only, so it is scoped back to the submission by + * keeping just the sections the submission actually owns. + * + * @param sections the submission's sections as held in the store + * @param operations the JSON PATCH operations pending for every submission section + * @return true when they leave a non-blank path, false when they clear it, and undefined when + * nothing pending touches the field + */ + private pendingOutcome(sections: SubmissionSectionEntry, operations: JsonPatchOperationsResourceEntry): boolean { + if (!hasValue(operations) || !hasValue(operations.children)) { + return undefined; + } + const outcomes: boolean[] = Object.keys(operations.children) + .filter((sectionId: string) => hasValue(sections[sectionId])) + .map((sectionId: string) => this.pathOutcome(operations.children[sectionId]?.body ?? [])) + .filter((outcome: boolean) => hasValue(outcome)); + + if (outcomes.length === 0) { + return undefined; + } + return outcomes.some((outcome: boolean) => outcome); + } + + /** + * Replay a section's pending operations in order and report the state the path field is left in. + * + * The operations have to be folded rather than tested one by one: a user who types a path and then + * clears the field again leaves both the add and the remove in the body, and only the last of them + * describes what the save will send. + * + * @param body the section's pending JSON PATCH operations, in the order they were dispatched + * @return true when the last operation touching the path field leaves a non-blank value, false when + * it clears it, and undefined when no operation touches the field + */ + private pathOutcome(body: JsonPatchOperationObject[]): boolean { + return body.reduce((leftBehind: boolean, entry: JsonPatchOperationObject) => { + if (!this.isPathFieldOperation(entry)) { + return leftBehind; + } + if (entry.operation.op === JsonPatchOperationType.remove) { + return false; + } + return this.isNonBlankValue(entry.operation.value); + }, undefined); + } + + /** + * Does this operation target the path field of a section, whatever it does to it? + * + * @param entry a pending JSON PATCH operation + * @return true when the operation's path addresses the path field + */ + private isPathFieldOperation(entry: JsonPatchOperationObject): boolean { + const path: string = entry?.operation?.path; + if (!hasValue(path)) { + return false; + } + // Paths are '/sections//' for a whole field and '/sections///' for one value. + return path.split('/')[3] === DatashareUploadFromPathService.PATH_FIELD; + } + + /** + * Is this a metadata value, or list of metadata values, carrying actual text? + * + * @param value a raw string, a metadata value object, or a list of either + * @return true when at least one of the values is a non-blank string + */ + private isNonBlankValue(value: any): boolean { + const candidates: any[] = Array.isArray(value) ? value : [value]; + return candidates.some((candidate: any) => { + if (typeof candidate === 'string') { + return candidate.trim().length > 0; + } + return hasValue(candidate) && typeof candidate === 'object' && this.isNonBlankValue(candidate.value); + }); + } + +} diff --git a/src/app/submission/form/footer/submission-form-footer.component.html b/src/app/submission/form/footer/submission-form-footer.component.html index 6ba0d4b9682..b8dbf4d3ab0 100644 --- a/src/app/submission/form/footer/submission-form-footer.component.html +++ b/src/app/submission/form/footer/submission-form-footer.component.html @@ -1,12 +1,3 @@ - - - - -