import path from "path" ; import { ExifToolTask } from "./ExifToolTask" ; import { Maybe } from "./Maybe" ; import { notBlank } from "./String" ; /**
 * Task that returns an error string (to prevent retries), or undefined if
 * everything seems to have worked.
 */ export class BinaryToBufferTask extends ExifToolTask < Buffer | Error > { private constructor ( readonly tagname , args ) { super ( args ) ; } static for ( tagname , imgSrc ) { const args = [ "-json" , "-b" , "-" + tagname , path . resolve ( imgSrc ) ] ; return new BinaryToBufferTask ( tagname , args ) ; } parse ( data , err ? ) { try { const obj = JSON . parse ( data ) ?. [ 0 ] ; { const result = decode ( obj [ this . tagname ] ) ; if ( result != null ) return result ; } for ( const k of Object . keys ( obj ) ) { if ( k . toLowerCase ( ) === this . tagname . toLowerCase ( ) ) { const result = decode ( obj [ k ] ) ; if ( result != null ) return result ; } } } catch ( caught ) { err ??= notBlank ( data ) ? new Error ( data ) : caught instanceof Error ? caught : new Error ( String ( caught ) ) ; } return err ?? new Error ( this . tagname + " not found" ) ; } } const B64Prefix = "base64:" ; function decode ( data ) { return data == null || ! data . startsWith ( B64Prefix ) ? undefined : Buffer . from ( data . substring ( B64Prefix . length ) , "base64" ) ; }