• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

geonetwork / geonetwork-ui / 14087991718

26 Mar 2025 04:04PM UTC coverage: 84.077% (+2.2%) from 81.86%
14087991718

Pull #1187

github

web-flow
Merge 90dcec225 into 4de378079
Pull Request #1187: [Editor] Image upload component UI error state

1574 of 2115 branches covered (74.42%)

Branch coverage included in aggregate %.

8 of 18 new or added lines in 1 file covered. (44.44%)

11 existing lines in 1 file now uncovered.

5037 of 5748 relevant lines covered (87.63%)

10.21 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

53.51
/libs/ui/elements/src/lib/image-input/image-input.component.ts
1
import { CommonModule } from '@angular/common'
1✔
2
import { HttpClient } from '@angular/common/http'
1✔
3
import {
1✔
4
  ChangeDetectionStrategy,
5
  ChangeDetectorRef,
6
  Component,
7
  EventEmitter,
8
  Input,
9
  Output,
10
} from '@angular/core'
11
import { MatProgressSpinnerModule } from '@angular/material/progress-spinner'
1✔
12
import { marker } from '@biesbjerg/ngx-translate-extract-marker'
1✔
13
import {
1✔
14
  ButtonComponent,
15
  FilesDropDirective,
16
  TextInputComponent,
17
  UrlInputComponent,
18
} from '@geonetwork-ui/ui/inputs'
19
import { downgradeImage, megabytesToBytes } from '@geonetwork-ui/util/shared'
1✔
20
import {
1✔
21
  NgIconComponent,
22
  provideIcons,
23
  provideNgIconsConfig,
24
} from '@ng-icons/core'
25
import {
1✔
26
  iconoirBin,
27
  iconoirFramePlusIn,
28
  iconoirLink,
29
  iconoirMediaImage,
30
  iconoirMediaImageXmark,
31
  iconoirPlus,
32
} from '@ng-icons/iconoir'
33
import { TranslateModule } from '@ngx-translate/core'
1✔
34
import { firstValueFrom } from 'rxjs'
1✔
35
import { ImageOverlayPreviewComponent } from '../image-overlay-preview/image-overlay-preview.component'
1✔
36

37
@Component({
38
  selector: 'gn-ui-image-input',
39
  templateUrl: './image-input.component.html',
40
  styleUrls: ['./image-input.component.css'],
41
  changeDetection: ChangeDetectionStrategy.OnPush,
42
  standalone: true,
43
  imports: [
44
    CommonModule,
45
    ButtonComponent,
46
    FilesDropDirective,
47
    MatProgressSpinnerModule,
48
    TranslateModule,
49
    UrlInputComponent,
50
    TextInputComponent,
51
    NgIconComponent,
52
    ImageOverlayPreviewComponent,
53
  ],
54
  providers: [
55
    provideIcons({
56
      iconoirMediaImage,
57
      iconoirFramePlusIn,
58
      iconoirMediaImageXmark,
59
      iconoirBin,
60
      iconoirPlus,
61
      iconoirLink,
62
    }),
63
    provideNgIconsConfig({
64
      size: '1.5rem',
65
    }),
66
  ],
67
})
68
export class ImageInputComponent {
1✔
69
  @Input() maxSizeMB: number
70
  @Input() previewUrl?: string
71
  @Input() altText?: string
72
  @Input() uploadProgress?: number
73
  @Input() uploadError?: boolean
74
  @Input() disabled?: boolean = false
6✔
75
  @Output() fileChange: EventEmitter<File> = new EventEmitter()
6✔
76
  @Output() urlChange: EventEmitter<string> = new EventEmitter()
6✔
77
  @Output() uploadCancel: EventEmitter<void> = new EventEmitter()
6✔
78
  @Output() delete: EventEmitter<void> = new EventEmitter()
6✔
79
  @Output() altTextChange: EventEmitter<string> = new EventEmitter()
6✔
80

81
  dragFilesOver = false
6✔
82
  showUrlInput = false
6✔
83
  imageFileError = this.uploadError
6✔
84
  showAltTextInput = false
6✔
85

86
  get isUploadInProgress() {
87
    return this.uploadProgress !== undefined
36✔
88
  }
89

90
  constructor(
91
    private http: HttpClient,
6!
92
    private cd: ChangeDetectorRef
6✔
93
  ) {}
94

95
  getIsActionBlocked() {
96
    return this.isUploadInProgress || this.imageFileError || this.disabled
12✔
97
  }
98

99
  getPrimaryText() {
100
    if (this.imageFileError) {
6!
101
      return marker('input.image.uploadErrorLabel')
×
102
    }
103
    if (this.uploadProgress) {
6!
104
      return marker('input.image.uploadProgressLabel')
×
105
    }
106
    return marker('input.image.selectFileLabel')
6✔
107
  }
108

109
  getSecondaryText() {
110
    if (this.imageFileError) {
6!
111
      return marker('input.image.uploadErrorRetry')
×
112
    }
113
    if (this.uploadProgress) {
6!
114
      return marker('input.image.uploadProgressCancel')
×
115
    }
116
    return marker('input.image.dropFileLabel')
6✔
117
  }
118

119
  handleDragFilesOver(dragFilesOver: boolean) {
120
    if (!this.showUrlInput) {
×
121
      this.dragFilesOver = dragFilesOver
×
122
      this.cd.markForCheck()
×
123
    }
124
  }
125

126
  handleDropFiles(files: File[]) {
NEW
127
    this.resetErrors()
×
128
    const validFiles = this.filterTypeImage(files)
×
129
    if (validFiles.length > 0) {
×
130
      this.showUrlInput = false
×
131
      this.resizeAndEmit(validFiles[0])
×
132
    } else {
NEW
133
      this.imageFileError = true
×
134
    }
135
  }
136

137
  handleFileInput(event: Event) {
NEW
138
    this.resetErrors()
×
139
    const inputFiles = Array.from((event.target as HTMLInputElement).files)
×
140
    const validFiles = this.filterTypeImage(inputFiles)
×
141
    if (validFiles.length > 0) {
×
142
      this.resizeAndEmit(validFiles[0])
×
143
    } else {
NEW
144
      this.imageFileError = true
×
145
    }
146
  }
147

148
  displayUrlInput() {
149
    this.uploadCancel.emit()
×
150
    this.showUrlInput = true
×
151
  }
152

153
  async downloadUrl(url: string) {
154
    this.resetErrors()
4✔
155
    const name = url.split('/').pop()
4✔
156
    try {
4✔
157
      const response = await firstValueFrom(
4✔
158
        this.http.head(url, { observe: 'response' })
159
      )
160
      if (
2✔
161
        response.headers.get('content-type')?.startsWith('image/') &&
4✔
162
        parseInt(response.headers.get('content-length')) <
163
          megabytesToBytes(this.maxSizeMB)
164
      ) {
165
        this.http.get(url, { responseType: 'blob' }).subscribe({
2✔
166
          next: (blob) => {
167
            this.cd.markForCheck()
1✔
168
            const file = new File([blob], name)
1✔
169
            this.fileChange.emit(file)
1✔
170
          },
171
          error: () => {
172
            this.imageFileError = true
1✔
173
            this.cd.markForCheck()
1✔
174
            this.urlChange.emit(url)
1✔
175
          },
176
        })
177
      }
178
    } catch {
NEW
179
      this.imageFileError = true
×
180
      this.cd.markForCheck()
×
181
      return
×
182
    }
183
  }
184

185
  handleSecondaryTextClick(event: Event) {
NEW
186
    if (this.imageFileError) {
×
NEW
187
      this.handleRetryUpload()
×
UNCOV
188
    } else if (this.uploadProgress) {
×
NEW
UNCOV
189
      this.handleCancelUpload()
×
UNCOV
190
      event.preventDefault()
×
191
    }
192
  }
193

194
  handleCancelUpload() {
UNCOV
195
    this.uploadCancel.emit()
×
196
  }
197

198
  handleRetryUpload() {
NEW
UNCOV
199
    this.resetErrors()
×
NEW
200
    this.cd.markForCheck()
×
201
  }
202

203
  handleDelete() {
204
    this.delete.emit()
×
205
  }
206

207
  resetErrors() {
208
    this.imageFileError = false
4✔
209
    this.uploadError = false
4✔
210
  }
211

212
  toggleAltTextInput() {
UNCOV
213
    this.showAltTextInput = !this.showAltTextInput
×
214
  }
215

216
  handleAltTextChange(altText: string) {
UNCOV
217
    this.altTextChange.emit(altText)
×
218
  }
219

220
  private filterTypeImage(files: File[]) {
221
    return files.filter((file) => {
1✔
222
      return file.type.startsWith('image/')
2✔
223
    })
224
  }
225

226
  private resizeAndEmit(imageToResize: File) {
UNCOV
227
    const maxSizeBytes = megabytesToBytes(this.maxSizeMB)
×
UNCOV
228
    downgradeImage(imageToResize, maxSizeBytes).then((resizedImage) => {
×
UNCOV
229
      const fileToEmit = new File([resizedImage], imageToResize.name)
×
UNCOV
230
      this.fileChange.emit(fileToEmit)
×
231
    })
232
  }
233
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc