All files / app/components/image-upload image-upload.component.ts

19.04% Statements 4/21
0% Branches 0/2
0% Functions 0/6
15.78% Lines 3/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52                    1x 4x 4x                                                                              
import { Component, inject } from '@angular/core';
import { ImageService } from '../../services/image.service';
import { ImageStateService } from '../../services/image-state.service';
 
@Component({
  selector: 'app-image-upload',
  imports: [],
  templateUrl: './image-upload.component.html',
  styleUrl: './image-upload.component.scss'
})
export class ImageUploadComponent {
  private imageService = inject(ImageService);
  private imageState = inject(ImageStateService);
 
  selectedFile?: File;
 
  onFileSelected(event: Event): void {
    const input = event.target as HTMLInputElement;
    this.selectedFile = input.files?.[0];
  }
 
  uploadImage(): void {
    const file = this.selectedFile;
    Iif (!file) {
      console.warn('[UPLOAD] No file selected.');
      return;
    }
 
    console.log('[UPLOAD] Starting upload for file:', file.name, 'type:', file.type, 'size:', file.size);
 
    this.imageService.generateUploadUrl(file.name, file.type, file.size)
    .subscribe({
      next: (metadata) => {
        fetch(metadata.url, { method: 'PUT', body: file, headers: { 'Content-Type': file.type } })
          .then(res => {
            Iif (!res.ok) throw new Error(`S3 upload failed`);
            this.imageState.addImage(metadata);
            alert('✅ Upload successful!');
          })
          .catch(err => {
            console.error(err);
            alert('Upload failed.');
          });
      },
      error: (err) => {
        console.error(err);
        alert('Failed to generate upload URL.');
      }
    });
  }
}