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

geonetwork / geonetwork-ui / 14068807028

25 Mar 2025 07:49PM UTC coverage: 83.976% (+0.7%) from 83.256%
14068807028

Pull #1187

github

web-flow
Merge 1ab8675b0 into c2edb114e
Pull Request #1187: [Editor] Image upload comp UI error state

1571 of 2118 branches covered (74.17%)

Branch coverage included in aggregate %.

5 of 13 new or added lines in 1 file covered. (38.46%)

5027 of 5739 relevant lines covered (87.59%)

10.21 hits per line

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

46.61
/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
  lastUploadType?: 'file' | 'url'
87
  lastUploadContent?: string | File
88
  lastUrl?: string
89

90
  get isUploadInProgress() {
91
    return this.uploadProgress !== undefined
36✔
92
  }
93

94
  constructor(
95
    private http: HttpClient,
6!
96
    private cd: ChangeDetectorRef
6✔
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[]) {
127
    if (!this.showUrlInput) {
×
128
      const validFiles = this.filterTypeImage(files)
×
129
      if (validFiles.length > 0) {
×
130
        this.resizeAndEmit(validFiles[0])
×
131
      } else {
NEW
132
        this.imageFileError = true
×
133
      }
134
    }
135
  }
136

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

145
  displayUrlInput() {
146
    this.uploadCancel.emit()
×
147
    this.showUrlInput = true
×
148
  }
149

150
  onUrlValueChange(url: string) {
NEW
151
    this.lastUrl = url
×
152
  }
153

154
  async downloadUrl(url: string) {
155
    this.imageFileError = false
4✔
156
    const name = url.split('/').pop()
4✔
157

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

187
  handleSecondaryTextClick(event: Event) {
188
    if (this.uploadError) {
×
NEW
189
      this.handleRetryUpload()
×
190
    } else if (this.uploadProgress) {
×
NEW
191
      this.handleCancelUpload()
×
192
      event.preventDefault()
×
NEW
193
    } else if (this.imageFileError && this.lastUrl) {
×
NEW
194
      this.handleRetrySendImgUrl()
×
195
    }
196
  }
197

198
  handleRetrySendImgUrl() {
NEW
199
    this.downloadUrl(this.lastUrl)
×
200
  }
201

202
  handleCancelUpload() {
203
    this.uploadCancel.emit()
×
204
  }
205

206
  handleRetryUpload() {
207
    switch (this.lastUploadType) {
×
208
      case 'file':
209
        this.fileChange.emit(this.lastUploadContent as File)
×
210
        break
×
211
      case 'url':
212
        this.urlChange.emit(this.lastUploadContent as string)
×
213
        break
×
214
    }
215
  }
216

217
  handleDelete() {
218
    this.delete.emit()
×
219
  }
220

221
  toggleAltTextInput() {
222
    this.showAltTextInput = !this.showAltTextInput
×
223
  }
224

225
  handleAltTextChange(altText: string) {
226
    this.altTextChange.emit(altText)
×
227
  }
228

229
  private filterTypeImage(files: File[]) {
230
    return files.filter((file) => {
1✔
231
      return file.type.startsWith('image/')
2✔
232
    })
233
  }
234

235
  private resizeAndEmit(imageToResize: File) {
236
    const maxSizeBytes = megabytesToBytes(this.maxSizeMB)
×
237
    downgradeImage(imageToResize, maxSizeBytes).then((resizedImage) => {
×
238
      const fileToEmit = new File([resizedImage], imageToResize.name)
×
239
      this.fileChange.emit(fileToEmit)
×
240
    })
241
  }
242
}
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