components_tasks_FontDownloadTask.bs

' bsc-disable-file print-locations — legacy print() sites; migration to m.log.* tracked by tech-debt.md#legacy-print-statements
import "pkg:/source/api/baseRequest.bs"
import "pkg:/source/roku_modules/rr/Requests.brs"
import "pkg:/source/utils/misc.bs"

sub init()
  m.top.functionName = "downloadFallbackFont"
end sub

' Downloads fallback font from the server to tmp:/font
sub downloadFallbackFont()
  print "FontDownloadTask: Starting fallback font download..."

  authArgs = { headers: { Authorization: buildAuthHeader() }, timeout: 10000, useCache: false }

  ' Check if server supports fallback fonts
  encodingUrl = buildURL("/system/configuration/encoding")
  if not isValid(encodingUrl)
    print "FontDownloadTask: Cannot build URL — server URL not set"
    m.top.errorMessage = "Server URL not configured"
    m.top.isFontDownloadSuccess = false
    m.top.isFontDownloadCompleted = true
    return
  end if
  r = rr_Requests().get(encodingUrl, authArgs)
  if not r.ok or not isValid(r.json) or not r.json.EnableFallbackFont
    print "FontDownloadTask: Server does not support fallback fonts"
    m.top.errorMessage = "Server does not support fallback fonts"
    m.top.isFontDownloadSuccess = false
    m.top.isFontDownloadCompleted = true
    return
  end if

  ' Get list of available fonts
  re = CreateObject("roRegex", "Name.:.(.*?).,.Size", "s")
  fontsUrl = buildURL("FallbackFont/Fonts")
  if not isValid(fontsUrl)
    print "FontDownloadTask: Cannot build URL — server URL not set"
    m.top.errorMessage = "Server URL not configured"
    m.top.isFontDownloadSuccess = false
    m.top.isFontDownloadCompleted = true
    return
  end if
  r = rr_Requests().get(fontsUrl, authArgs)
  if not r.ok or r.text = ""
    print "FontDownloadTask: Failed to get font list from server"
    m.top.errorMessage = "Failed to get font list from server"
    m.top.isFontDownloadSuccess = false
    m.top.isFontDownloadCompleted = true
    return
  end if

  ' Extract font filename from response
  filename = re.match(r.text)
  if not isValid(filename) or filename.count() = 0
    print "FontDownloadTask: Could not parse font filename from server response"
    m.top.errorMessage = "Could not parse font filename from server response"
    m.top.isFontDownloadSuccess = false
    m.top.isFontDownloadCompleted = true
    return
  end if

  filename = filename[1]
  print "FontDownloadTask: Font filename: " + filename

  ' Download the font file directly to final location using roUrlTransfer.
  ' rr_Requests cannot write binary data to a file; roUrlTransfer.GetToFile() is required.
  fontFileReq = APIRequest("FallbackFont/Fonts/" + filename)
  if not isValid(fontFileReq)
    print "FontDownloadTask: Failed to create API request for font file"
    m.top.errorMessage = "Failed to create API request for font file"
    m.top.isFontDownloadSuccess = false
    m.top.isFontDownloadCompleted = true
    return
  end if

  fontFileReq.gettofile("tmp:/font")

  ' Wait a moment for file to be fully written
  sleep(100)

  ' Verify the file was written successfully
  fs = CreateObject("roFileSystem")
  if not fs.Exists("tmp:/font")
    print "FontDownloadTask: Font file was not created successfully"
    m.top.errorMessage = "Font file was not created successfully"
    m.top.isFontDownloadSuccess = false
    m.top.isFontDownloadCompleted = true
    return
  end if

  print "FontDownloadTask: Fallback font downloaded successfully"
  m.top.isFontDownloadSuccess = true
  m.top.isFontDownloadCompleted = true
end sub