Every program, that create temporary files, must delete own garbage
    
    
    import os, httpclient
    
    type Download = ref object
      status: string
      temp: string
      done: bool
    
    proc finalizeDownload*[Download](o: Download) =
      if not o.done and o.temp.existsFile:
        # remove temp file when exception occurs"
        o.temp.removeFile
    
    proc downloadFileExt*(url, path: string): Download  =
      new(result, finalizeDownload)
      result.done = false
      result.temp = path & ".part"
      newHttpClient().downloadFile(url, result.temp)
      result.status = "200" # inflexible httpclient
      result.temp.moveFile(path)
      result.done = true
    
    proc main =
      let url = "http://www.some.com/file.mp4";
      let dl = downloadFileExt(url, url.extractFilename)
      echo "downloaded " & url & " with status " & dl.status
    
    try:
      main()
    finally:
      GC_fullCollect()
    
    
    
    Run

Reply via email to