LCOV - code coverage report
Current view: top level - src/scripts/metadata - flac.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 91.1 % 45 41
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import { readFileAsArrayBuffer } from '@/utils/fileUtil'
       2              : 
       3            2 : export function getFromFlacBuffer(buffer: ArrayBuffer): Record<string, string> {
       4            2 :   const dataView = new DataView(buffer)
       5              : 
       6              :   // Verify the FLAC signature
       7              :   const signature = String.fromCharCode(...new Uint8Array(buffer, 0, 4))
       8            2 :   if (signature !== 'fLaC') {
       9            0 :     console.error('Not a valid FLAC file')
      10            0 :     return {}
      11              :   }
      12              : 
      13              :   // Parse metadata blocks
      14            2 :   let offset = 4
      15            2 :   let vorbisComment = null
      16            2 :   while (offset < dataView.byteLength) {
      17            6 :     const isLastBlock = dataView.getUint8(offset) & 0x80
      18            6 :     const blockType = dataView.getUint8(offset) & 0x7f
      19            6 :     const blockSize = dataView.getUint32(offset, false) & 0xffffff
      20            6 :     offset += 4
      21              : 
      22            6 :     if (blockType === 4) {
      23              :       // Vorbis Comment block type
      24            2 :       vorbisComment = parseVorbisComment(
      25              :         new DataView(buffer, offset, blockSize)
      26              :       )
      27              :     }
      28              : 
      29            6 :     offset += blockSize
      30            6 :     if (isLastBlock) break
      31              :   }
      32              : 
      33            2 :   return vorbisComment ?? {}
      34              : }
      35              : 
      36            2 : export async function getFromFlacFile(
      37              :   file: File
      38              : ): Promise<Record<string, string>> {
      39            2 :   const buffer = await readFileAsArrayBuffer(file)
      40            2 :   if (!buffer) return {}
      41              : 
      42            2 :   try {
      43            2 :     return getFromFlacBuffer(buffer)
      44              :   } catch (e) {
      45            0 :     console.error('Parser: Error parsing FLAC metadata:', e)
      46            0 :     return {}
      47              :   }
      48              : }
      49              : 
      50              : // Function to parse the Vorbis Comment block
      51            2 : function parseVorbisComment(dataView: DataView): Record<string, string> {
      52            2 :   let offset = 0
      53            2 :   const vendorLength = dataView.getUint32(offset, true)
      54            2 :   offset += 4 + vendorLength
      55              : 
      56            2 :   const userCommentListLength = dataView.getUint32(offset, true)
      57            2 :   offset += 4
      58            2 :   const comments: Record<string, string> = {}
      59            2 :   for (let i = 0; i < userCommentListLength; i++) {
      60            5 :     const commentLength = dataView.getUint32(offset, true)
      61            5 :     offset += 4
      62            5 :     const comment = getString(dataView, offset, commentLength)
      63            5 :     offset += commentLength
      64              : 
      65            5 :     const ind = comment.indexOf('=')
      66            5 :     const key = comment.substring(0, ind)
      67              : 
      68            5 :     comments[key] = comment.substring(ind + 1)
      69              :   }
      70              : 
      71            2 :   return comments
      72              : }
      73              : 
      74            5 : function getString(dataView: DataView, offset: number, length: number): string {
      75            5 :   let string = ''
      76            5 :   for (let i = 0; i < length; i++) {
      77          248 :     string += String.fromCharCode(dataView.getUint8(offset + i))
      78              :   }
      79            5 :   return string
      80              : }
        

Generated by: LCOV version 2.0-1