All files / app/features/content/content-upload content-upload.component.ts

100% Statements 39/39
85.71% Branches 6/7
100% Functions 9/9
100% Lines 36/36

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                                                                                                                                                                                                                                                                                        1x 16x   16x 16x 16x 16x     16x 16x       2x 2x 2x 2x 2x         6x 1x 1x     5x 5x   5x 5x 4x   4x 3x   2x   5x         5x 5x 5x 5x 5x         12x 11x 11x 11x 11x       1x    
import { Component, EventEmitter, Output } from '@angular/core';
import { CommonModule } from '@angular/common';
import { SharedModule } from '../../../shared/shared-module';
import { CasService } from '../../../core/services/cas.service';
import { ContentHash } from '../../../core/domain/interfaces/content.interface';
import { Router } from '@angular/router';
 
@Component({
  selector: 'app-content-upload',
  standalone: true,
  imports: [CommonModule, SharedModule],
  template: `
    <div class="content-upload">
      <h2>Upload Content to CAS</h2>
      
      <div class="upload-area">
        <input 
          type="file" 
          #fileInput
          (change)="onFileSelected($event)"
          [disabled]="isUploading"
          class="file-input"
        />
        
        <div *ngIf="selectedFile" class="file-info">
          <p>Selected: {{ selectedFile.name }}</p>
          <p>Size: {{ formatFileSize(selectedFile.size) }}</p>
          <p>Type: {{ selectedFile.type || 'Unknown' }}</p>
        </div>
 
        <button 
          (click)="uploadFile()"
          [disabled]="!selectedFile || isUploading"
          class="upload-button"
        >
          {{ isUploading ? 'Uploading...' : 'Upload to CAS' }}
        </button>
 
        <div *ngIf="errorMessage" class="error-message">
          {{ errorMessage }}
        </div>
 
        <div *ngIf="uploadedHash" class="success-message">
          <h3>Content Stored Successfully!</h3>
          <p>Algorithm: {{ uploadedHash.algorithm }}</p>
          <p>Hash: <code>{{ uploadedHash.value }}</code></p>
          <button (click)="navigateToContentList()" class="view-content-button">
            View All Content
          </button>
        </div>
      </div>
    </div>
  `,
  styles: [`
    .content-upload {
      padding: 20px;
      max-width: 600px;
      margin: 0 auto;
    }
 
    .upload-area {
      border: 2px dashed #ccc;
      border-radius: 8px;
      padding: 30px;
      text-align: center;
      background-color: #f9f9f9;
    }
 
    .file-input {
      margin-bottom: 20px;
    }
 
    .file-info {
      margin: 20px 0;
      padding: 15px;
      background-color: #e9ecef;
      border-radius: 4px;
      text-align: left;
    }
 
    .file-info p {
      margin: 5px 0;
    }
 
    .upload-button {
      background-color: #007bff;
      color: white;
      border: none;
      padding: 10px 20px;
      border-radius: 4px;
      cursor: pointer;
      font-size: 16px;
    }
 
    .upload-button:disabled {
      background-color: #6c757d;
      cursor: not-allowed;
    }
 
    .error-message {
      color: #dc3545;
      margin-top: 15px;
      padding: 10px;
      background-color: #f8d7da;
      border-radius: 4px;
    }
 
    .success-message {
      color: #155724;
      margin-top: 15px;
      padding: 15px;
      background-color: #d4edda;
      border-radius: 4px;
      text-align: left;
    }
 
    .success-message code {
      background-color: #e9ecef;
      padding: 2px 4px;
      border-radius: 3px;
      font-family: monospace;
      word-break: break-all;
    }
 
    .view-content-button {
      background-color: #28a745;
      color: white;
      border: none;
      padding: 10px 20px;
      border-radius: 4px;
      cursor: pointer;
      font-size: 16px;
      margin-top: 15px;
    }
 
    .view-content-button:hover {
      background-color: #218838;
    }
  `]
})
export class ContentUploadComponent {
  @Output() contentStored = new EventEmitter<ContentHash>();
 
  selectedFile: File | null = null;
  uploadedHash: ContentHash | null = null;
  isUploading = false;
  errorMessage = '';
 
  constructor(
    private casService: CasService,
    private router: Router
  ) {}
 
  onFileSelected(event: Event): void {
    const input = event.target as HTMLInputElement;
    if (input.files && input.files.length > 0) {
      this.selectedFile = input.files[0];
      this.errorMessage = '';
      this.uploadedHash = null;
    }
  }
 
  async uploadFile(): Promise<void> {
    if (!this.selectedFile) {
      this.errorMessage = 'Please select a file';
      return;
    }
 
    this.isUploading = true;
    this.errorMessage = '';
 
    try {
      const fileData = await this.readFileAsArrayBuffer(this.selectedFile);
      const content = { data: new Uint8Array(fileData) };
      
      this.uploadedHash = await this.casService.store(content);
      this.contentStored.emit(this.uploadedHash);
    } catch (error) {
      this.errorMessage = `Upload failed: ${error instanceof Error ? error.message : 'Unknown error'}`;
    } finally {
      this.isUploading = false;
    }
  }
 
  private readFileAsArrayBuffer(file: File): Promise<ArrayBuffer> {
    return new Promise((resolve, reject) => {
      const reader = new FileReader();
      reader.onload = () => resolve(reader.result as ArrayBuffer);
      reader.onerror = () => reject(new Error('Failed to read file'));
      reader.readAsArrayBuffer(file);
    });
  }
 
  formatFileSize(bytes: number): string {
    if (bytes === 0) return '0 Bytes';
    const k = 1024;
    const sizes = ['Bytes', 'KB', 'MB', 'GB'];
    const i = Math.floor(Math.log(bytes) / Math.log(k));
    return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
  }
 
  navigateToContentList(): void {
    this.router.navigate(['/content']);
  }
}