import { download } from '../export'; import { HttpErrorResponse } from '@angular/common/http'; import { Component, inject, input, model, output, signal } from '@angular/core'; import { FormsModule } from '@angular/forms'; import { MatButtonModule } from '@angular/material/button'; import { MatFormFieldModule } from '@angular/material/form-field'; import { MatInputModule } from '@angular/material/input'; import { MatSelectModule } from '@angular/material/select'; import { Api } from '../api/api'; import { exportModels, type ExportModels$Params } from '../api/fn/models/export-models'; import { importModels } from '../api/fn/models/import-models'; import type { Domain } from '../api/models/domain'; import { errorText } from './run-monitor'; export async function verifiedImport( file: Blob, expected: string, upload: (sha256: string) => Promise, ): Promise { const digest = await crypto.subtle.digest('SHA-256', await file.arrayBuffer()); const sha256 = Array.from(new Uint8Array(digest), (byte) => byte.toString(16).padStart(2, '0'), ).join(''); if (expected.trim().toLowerCase() !== sha256) { throw new Error('El SHA-256 del archivo no coincide con el esperado.'); } return upload(sha256); } export async function exportErrorText(error: unknown): Promise { if (!(error instanceof HttpErrorResponse && error.error instanceof Blob)) return errorText(error); const text = await error.error.text(); try { const detail = JSON.parse(text).detail; return typeof detail === 'string' ? detail : text; } catch { return text; } } @Component({ selector: 'app-governance-bundles', imports: [FormsModule, MatButtonModule, MatFormFieldModule, MatInputModule, MatSelectModule], template: `

Bundles

Versiones En producción Últimas Todas
@if (exportedHash()) {

SHA-256 {{ exportedHash() }}

}
@for (file of zipFiles(); track $index) { {{ file.name }} } SHA-256 esperado
@if (message()) {

{{ message() }}

}
`, }) export class Bundles { private readonly api = inject(Api); readonly domain = input.required(); readonly disabled = input(false); readonly started = output(); protected readonly selection = signal>('champion'); protected readonly exportedHash = signal(''); protected readonly zipFiles = signal(null); protected readonly hash = signal(''); protected readonly message = signal(''); protected readonly exporting = signal(false); readonly importing = model(false); protected async export(): Promise { this.exporting.set(true); this.message.set(''); this.exportedHash.set(''); const domain = this.domain(); try { const response = await this.api.invoke$Response(exportModels, { domain, versions: this.selection(), }); const sha256 = response.headers.get('X-Bundle-Sha256') ?? ''; this.exportedHash.set(sha256); download(response.body, `${domain}-models-${sha256.slice(0, 12)}.zip`); } catch (error) { this.message.set(await exportErrorText(error)); } finally { this.exporting.set(false); } } protected async import(input: HTMLInputElement): Promise { const file = input.files?.[0]; if (!file || this.importing() || this.disabled()) return; this.importing.set(true); this.message.set(''); const domain = this.domain(); try { const { run_id } = await verifiedImport(file, this.hash(), (sha256) => this.api.invoke(importModels, { domain, sha256, body: { file: file as unknown as string }, }), ); input.value = ''; this.zipFiles.set(null); if (domain === this.domain()) this.started.emit(run_id); } catch (error) { this.message.set(errorText(error)); } finally { this.importing.set(false); } } }