All files / app/features/metadata/metadata-entry metadata-entry.component.ts

98.61% Statements 71/72
84.61% Branches 11/13
100% Functions 22/22
98.61% Lines 71/72

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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225                                    1x   31x 31x 31x 31x 31x 31x 31x 31x   31x     31x 31x 31x 31x 31x 31x       31x       31x                       34x                 33x             161x       139x       3x       1x       2x       1x       2x 2x             10x 10x       13x 13x       1x 1x       2x 2x       1x 1x 1x       1x       1x       2x       1x 1x 1x       1x       10x 9x 9x         9x 9x 9x     8x                   8x   8x         1x       10x       5x 1x     4x 1x 1x     3x 3x   3x 3x 3x               3x         2x   1x   3x      
import { Component, OnInit } from '@angular/core';
import { CommonModule } from '@angular/common';
import { ReactiveFormsModule, FormBuilder, FormGroup, FormArray, Validators } from '@angular/forms';
import { Router } from '@angular/router';
import { MetadataService } from '../../../core/services/metadata/metadata.service';
import { SignatureService } from '../../../core/services/signature.service';
import { CasService } from '../../../core/services/cas.service';
import { ContentPreviewService } from '../../../core/services/content-preview.service';
import { createMetadataContent, AuthorRole } from '../../../core/domain/interfaces/metadata-entry';
import { ContentHash } from '../../../core/domain/interfaces/content.interface';
import { ContentSelectionModalComponent } from '../../../shared/components/content-selection-modal/content-selection-modal.component';
 
@Component({
  selector: 'app-metadata-entry',
  standalone: true,
  imports: [CommonModule, ReactiveFormsModule, ContentSelectionModalComponent],
  templateUrl: './metadata-entry.component.html'
})
export class MetadataEntryComponent implements OnInit {
  metadataForm!: FormGroup;
  keyPair: { publicKey: string; privateKey: string } | null = null;
  error: string = '';
  submitting = false;
  showHashSelectionModal = false;
  showAuthorHashModal = false;
  showPreviousVersionModal = false;
  currentReferenceIndex = -1;
  currentAuthorIndex = -1;
 
  authorRoles = Object.values(AuthorRole);
 
  constructor(
    private fb: FormBuilder,
    private metadataService: MetadataService,
    private signatureService: SignatureService,
    private casService: CasService,
    private contentPreviewService: ContentPreviewService,
    private router: Router
  ) {}
 
  ngOnInit() {
    this.initializeForm();
  }
 
  private initializeForm() {
    this.metadataForm = this.fb.group({
      references: this.fb.array([this.createReferenceGroup()]),
      authors: this.fb.array([this.createAuthorGroup()]),
      version: this.fb.group({
        version: ['1.0.0', Validators.required],
        previousVersion: [''],
        changeDescription: ['']
      })
    });
  }
 
  private createReferenceGroup(): FormGroup {
    return this.fb.group({
      hash: ['', Validators.required],
      mimeType: ['', Validators.required],
      mimeTypeSource: ['manual', Validators.required],
      relationship: ['']
    });
  }
 
  private createAuthorGroup(): FormGroup {
    return this.fb.group({
      authorHash: ['', Validators.required],
      role: [AuthorRole.CREATOR, Validators.required]
    });
  }
 
  get references(): FormArray {
    return this.metadataForm.get('references') as FormArray;
  }
 
  get authors(): FormArray {
    return this.metadataForm.get('authors') as FormArray;
  }
 
  addReference() {
    this.references.push(this.createReferenceGroup());
  }
 
  removeReference(index: number) {
    this.references.removeAt(index);
  }
 
  addAuthor() {
    this.authors.push(this.createAuthorGroup());
  }
 
  removeAuthor(index: number) {
    this.authors.removeAt(index);
  }
 
  async generateKeyPair() {
    try {
      this.keyPair = await this.signatureService.generateKeyPair();
    } catch (error) {
      this.error = 'Failed to generate key pair';
    }
  }
 
  openHashSelector(referenceIndex: number) {
    this.currentReferenceIndex = referenceIndex;
    this.showHashSelectionModal = true;
  }
 
  closeHashSelector() {
    this.showHashSelectionModal = false;
    this.currentReferenceIndex = -1;
  }
 
  openAuthorHashSelector(authorIndex: number) {
    this.currentAuthorIndex = authorIndex;
    this.showAuthorHashModal = true;
  }
 
  closeAuthorHashSelector() {
    this.showAuthorHashModal = false;
    this.currentAuthorIndex = -1;
  }
 
  onAuthorHashSelected(hash: ContentHash) {
    if (this.currentAuthorIndex >= 0) {
      const authorControl = this.authors.at(this.currentAuthorIndex);
      authorControl.patchValue({
        authorHash: hash.value
      });
    }
    this.closeAuthorHashSelector();
  }
 
  openPreviousVersionSelector() {
    this.showPreviousVersionModal = true;
  }
 
  closePreviousVersionSelector() {
    this.showPreviousVersionModal = false;
  }
 
  onPreviousVersionSelected(hash: ContentHash) {
    const versionControl = this.metadataForm.get('version');
    if (versionControl) {
      versionControl.patchValue({
        previousVersion: hash.value
      });
    }
    this.closePreviousVersionSelector();
  }
 
  async onHashSelected(hash: ContentHash) {
    if (this.currentReferenceIndex >= 0) {
      const referenceControl = this.references.at(this.currentReferenceIndex);
      referenceControl.patchValue({
        hash: hash.value
      });
      
      // Try to detect MIME type from the selected content
      try {
        const content = await this.casService.retrieve(hash);
        const detectedType = this.contentPreviewService.detectContentType(content.data);
        
        // Map detected content types to standard MIME types
        const mimeTypeMap: { [key: string]: string } = {
          'image/png': 'image/png',
          'image/jpeg': 'image/jpeg',
          'image/gif': 'image/gif',
          'application/pdf': 'application/pdf',
          'application/json': 'application/json',
          'text/plain': 'text/plain',
          'application/octet-stream': 'application/octet-stream'
        };
        
        const mimeType = mimeTypeMap[detectedType] || 'application/octet-stream';
        
        referenceControl.patchValue({
          mimeType: mimeType,
          mimeTypeSource: 'detected'
        });
      } catch (error) {
        console.error('Failed to detect MIME type:', error);
        // Keep manual entry if detection fails
      }
    }
    this.closeHashSelector();
  }
 
  async onSubmit() {
    if (!this.metadataForm.valid) {
      return;
    }
 
    if (!this.keyPair) {
      this.error = 'Please generate a key pair first';
      return;
    }
 
    this.submitting = true;
    this.error = '';
 
    try {
      const formValue = this.metadataForm.value;
      const metadata = createMetadataContent({
        references: formValue.references,
        authors: formValue.authors,
        version: formValue.version.version,
        previousVersion: formValue.version.previousVersion || undefined,
        changeDescription: formValue.version.changeDescription || undefined
      });
 
      const entry = await this.metadataService.createMetadataEntry(
        metadata,
        this.keyPair.privateKey
      );
 
      await this.router.navigate(['/metadata/view', entry.id]);
    } catch (error: any) {
      this.error = error.message || 'Failed to create metadata entry';
    } finally {
      this.submitting = false;
    }
  }
}