Skip to content

Commit

Permalink
fix(me): Fixed overview and online resources upload -> check if file …
Browse files Browse the repository at this point in the history
…name already exist and rename if that s the case before uploading
  • Loading branch information
Romuald Caplier committed Sep 27, 2024
1 parent be95197 commit df10494
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 19 deletions.
2 changes: 1 addition & 1 deletion libs/common/domain/src/lib/platform.service.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { Organization } from './model/record/organization.model'
import { Keyword, UserFeedback } from './model/record'
import { KeywordType } from './model/thesaurus'

interface RecordAttachment {
export interface RecordAttachment {
url: URL
fileName: string
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,32 @@ import { FormFieldOnlineLinkResourcesComponent } from './form-field-online-link-
import { aSetOfLinksFixture } from '@geonetwork-ui/common/fixtures'
import { MockBuilder, MockProvider } from 'ng-mocks'
import { TranslateModule } from '@ngx-translate/core'
import { PlatformServiceInterface } from '@geonetwork-ui/common/domain/platform.service.interface'
import {
PlatformServiceInterface,
RecordAttachment,
} from '@geonetwork-ui/common/domain/platform.service.interface'
import { NotificationsService } from '@geonetwork-ui/feature/notifications'
import { Subject } from 'rxjs'
import { BehaviorSubject, Subject } from 'rxjs'
import { MatDialog, MatDialogRef } from '@angular/material/dialog'
import { OnlineLinkResource } from '@geonetwork-ui/common/domain/model/record'
import { ModalDialogComponent } from '@geonetwork-ui/ui/layout'
import { ChangeDetectorRef } from '@angular/core'

let uploadSubject: Subject<any>

const recordAttachments = new BehaviorSubject<RecordAttachment[]>([
{
url: new URL('https://www.fakedomain.com/test.txt'),
fileName: 'test.txt',
},
])

class PlatformServiceInterfaceMock {
attachFileToRecord = jest.fn(() => {
uploadSubject = new Subject()
return uploadSubject
})
getRecordAttachments = jest.fn(() => recordAttachments)
}
export class MatDialogMock {
_subject = new Subject()
Expand Down Expand Up @@ -44,8 +57,13 @@ describe('FormFieldOnlineLinkResourcesComponent', () => {
PlatformServiceInterfaceMock,
'useClass'
),
MockProvider(NotificationsService),
MockProvider(NotificationsService, {
showNotification: jest.fn(),
}),
MockProvider(MatDialogRef),
MockProvider(ChangeDetectorRef, {
detectChanges: jest.fn(),
}),
MockProvider(MatDialog, MatDialogMock, 'useClass'),
],
}).compileComponents()
Expand Down Expand Up @@ -124,7 +142,7 @@ describe('FormFieldOnlineLinkResourcesComponent', () => {
const file = new File([''], 'test-file.txt')
expect(component.uploadProgress).toBeUndefined()
component.handleFileChange(file)
uploadSubject.error(new Error('something went wrong'))
uploadSubject.error({ error: { message: 'something went wrong' } })
expect(notificationsService.showNotification).toHaveBeenCalledWith({
type: 'error',
closeMessage: 'editor.record.onlineResourceError.closeMessage',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ import {
import { NotificationsService } from '@geonetwork-ui/feature/notifications'
import { TranslateModule, TranslateService } from '@ngx-translate/core'
import { PlatformServiceInterface } from '@geonetwork-ui/common/domain/platform.service.interface'
import { Subscription } from 'rxjs'
import { Subscription, switchMap } from 'rxjs'
import { MatDialog } from '@angular/material/dialog'
import { MAX_UPLOAD_SIZE_MB } from '../../../../fields.config'
import { map } from 'rxjs/operators'

@Component({
selector: 'gn-ui-form-field-online-link-resources',
Expand Down Expand Up @@ -61,7 +62,7 @@ export class FormFieldOnlineLinkResourcesComponent {

private allResources: OnlineResource[] = []
linkResources: OnlineLinkResource[] = []
uploadProgress = undefined
uploadProgress?: number = undefined
uploadSubscription: Subscription = null

protected MAX_UPLOAD_SIZE_MB = MAX_UPLOAD_SIZE_MB
Expand All @@ -77,7 +78,26 @@ export class FormFieldOnlineLinkResourcesComponent {
handleFileChange(file: File) {
this.uploadProgress = 0
this.uploadSubscription = this.platformService
.attachFileToRecord(this.metadataUuid, file)
.getRecordAttachments(this.metadataUuid)
.pipe(
map((recordAttachement) => recordAttachement.map((r) => r.fileName)),
switchMap((fileNames) => {
let fileToUpload = file

if (fileNames.includes(file.name)) {
const fileNameParts = file.name.split('.')
const extension = fileNameParts.pop()
const baseName = fileNameParts.join('.')
const newFileName = `${baseName}_${Date.now()}.${extension}`

fileToUpload = new File([file], newFileName, { type: file.type })
}
return this.platformService.attachFileToRecord(
this.metadataUuid,
fileToUpload
)
})
)
.subscribe({
next: (event) => {
if (event.type === 'progress') {
Expand All @@ -94,7 +114,7 @@ export class FormFieldOnlineLinkResourcesComponent {
this.valueChange.emit([...this.allResources, newResource])
}
},
error: (error: Error) => this.handleError(error.message),
error: (error: any) => this.handleError(error.error.message),
})
}

Expand All @@ -115,7 +135,7 @@ export class FormFieldOnlineLinkResourcesComponent {
}
this.valueChange.emit([...this.allResources, newLink])
} catch (e) {
this.handleError((e as Error).message)
this.handleError((e as any).error.message)
}
}

Expand All @@ -134,6 +154,7 @@ export class FormFieldOnlineLinkResourcesComponent {

private handleError(error: string) {
this.uploadProgress = undefined
this.cd.detectChanges()
this.notificationsService.showNotification({
type: 'error',
title: this.translateService.instant(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
import { ComponentFixture, TestBed } from '@angular/core/testing'
import { TranslateModule } from '@ngx-translate/core'
import { FormFieldOverviewsComponent } from './form-field-overviews.component'
import { Subject } from 'rxjs'
import { BehaviorSubject, Subject } from 'rxjs'
import { NotificationsService } from '@geonetwork-ui/feature/notifications'
import { MockBuilder, MockProvider } from 'ng-mocks'
import { PlatformServiceInterface } from '@geonetwork-ui/common/domain/platform.service.interface'
import {
PlatformServiceInterface,
RecordAttachment,
} from '@geonetwork-ui/common/domain/platform.service.interface'

let uploadSubject: Subject<any>

const recordAttachments = new BehaviorSubject<RecordAttachment[]>([
{
url: new URL('https://www.fakedomain.com/test.txt'),
fileName: 'test.txt',
},
])

class PlatformServiceInterfaceMock {
attachFileToRecord = jest.fn(() => {
uploadSubject = new Subject()
return uploadSubject
})
getRecordAttachments = jest.fn(() => recordAttachments)
}

describe('FormFieldOverviewsComponent', () => {
Expand Down Expand Up @@ -106,7 +118,7 @@ describe('FormFieldOverviewsComponent', () => {
const file = new File([''], 'test-file.txt')
expect(component.uploadProgress).toBeUndefined()
component.handleFileChange(file)
uploadSubject.error(new Error('something went wrong'))
uploadSubject.error({ error: { message: 'something went wrong' } })
expect(notificationsService.showNotification).toHaveBeenCalledWith({
type: 'error',
closeMessage: 'editor.record.resourceError.closeMessage',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import { ImageInputComponent } from '@geonetwork-ui/ui/inputs'
import { PlatformServiceInterface } from '@geonetwork-ui/common/domain/platform.service.interface'
import { NotificationsService } from '@geonetwork-ui/feature/notifications'
import { TranslateService } from '@ngx-translate/core'
import { Subscription } from 'rxjs'
import { Subscription, switchMap } from 'rxjs'
import { MAX_UPLOAD_SIZE_MB } from '../../../../fields.config'
import { map } from 'rxjs/operators'

@Component({
selector: 'gn-ui-form-field-overviews',
Expand Down Expand Up @@ -53,7 +54,26 @@ export class FormFieldOverviewsComponent {
handleFileChange(file: File) {
this.uploadProgress = 0
this.uploadSubscription = this.platformService
.attachFileToRecord(this.metadataUuid, file)
.getRecordAttachments(this.metadataUuid)
.pipe(
map((recordAttachement) => recordAttachement.map((r) => r.fileName)),
switchMap((fileNames) => {
let fileToUpload = file

if (fileNames.includes(file.name)) {
const fileNameParts = file.name.split('.')
const extension = fileNameParts.pop()
const baseName = fileNameParts.join('.')
const newFileName = `${baseName}_${Date.now()}.${extension}`

fileToUpload = new File([file], newFileName, { type: file.type })
}
return this.platformService.attachFileToRecord(
this.metadataUuid,
fileToUpload
)
})
)
.subscribe({
next: (event) => {
if (event.type === 'progress') {
Expand All @@ -68,7 +88,7 @@ export class FormFieldOverviewsComponent {
})
}
},
error: this.errorHandle,
error: (error: any) => this.handleError(error.error.message),
})
}

Expand All @@ -87,7 +107,7 @@ export class FormFieldOverviewsComponent {
description: filename,
})
} catch (e) {
this.errorHandle(e)
this.handleError((e as any).error.message)
}
}

Expand All @@ -106,14 +126,15 @@ export class FormFieldOverviewsComponent {
this.valueChange.emit(overView ? [overView] : [])
}

private errorHandle = (error) => {
private handleError = (error: string) => {
this.uploadProgress = undefined
this.cd.markForCheck()
this.notificationsService.showNotification({
type: 'error',
title: this.translateService.instant('editor.record.resourceError.title'),
text: `${this.translateService.instant(
'editor.record.resourceError.body'
)} ${error.message}`,
)} ${error}`,
closeMessage: this.translateService.instant(
'editor.record.resourceError.closeMessage'
),
Expand Down

0 comments on commit df10494

Please sign in to comment.