Advertisement
DaxSoft

URLDownloadToFile

Feb 21st, 2015
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.94 KB | None | 0 0
  1. #==============================================================================
  2. # • URLDownloadToFile
  3. #==============================================================================
  4. # Autor: Dax
  5. # Versão: 1.0
  6. # Site: www.dax-soft.weebly.com
  7. # Requerimento: Dax Core
  8. #==============================================================================
  9. # • Descrição:
  10. #------------------------------------------------------------------------------
  11. #  Transfere um arquivo que está "linkado" numa url para outro, i.e, baixa
  12. # um arquivo dá internet.
  13. #==============================================================================
  14. # • Como usar:
  15. #------------------------------------------------------------------------------
  16. # URLDownloadToFile.run(url, dstfile, open)
  17. #   url : URL do arquivo.
  18. #   dstfile : Nome final do arquivo.
  19. #   open : Abrir arquivo, logo após baixar? true - sim | false - não.
  20. #
  21. # Exemplo:
  22. #
  23. # url = "http://www.news2news.com/vfp/downloads/w32data.zip"
  24. # dstfile = "w32data.zip"
  25. # URLDownloadToFile.run(url, dstfile)
  26. #==============================================================================
  27. Dax.register(:urldownloadtofile, "Dax", 1.0, "21/02/15")
  28. module URLDownloadToFile
  29.   #----------------------------------------------------------------------------
  30.   # • Extensão dá função do módulo.
  31.   #   Extensão do módulo API.
  32.   #----------------------------------------------------------------------------
  33.   extend API
  34.   extend self
  35.   #----------------------------------------------------------------------------
  36.   # • Constantes.
  37.   #----------------------------------------------------------------------------
  38.   DLL = "Urlmon.dll"
  39.   DLL_SHELL32 = "Shell32.dll"
  40.   S_OK = 0x00000000
  41.   SW_SHOWNORMAL = 1
  42.   #----------------------------------------------------------------------------
  43.   # • Função dá DLL.
  44.   #----------------------------------------------------------------------------
  45.   @@urlDownloadToFile = int("URLDownloadToFile", [:LPCTSTR, :LPCTSTR, :LPCTSTR,
  46.                                                 :int, :int], DLL)
  47.   @@shellExecute = long("ShellExecute", [:LPCTSTR, :LPCTSTR, :LPCTSTR,
  48.                                       :LPCTSTR, :LPCTSTR, :LONG], DLL_SHELL32)
  49.   #----------------------------------------------------------------------------
  50.   # • [BooleanClass] : Baixar o arquivo dá URL para arquivo. (lol)
  51.   #     url : Link dá url.
  52.   #     dstfile : Nome do arquivo final.
  53.   #     open : Abrir arquivo automáticamente? true - sim | false - não.
  54.   #----------------------------------------------------------------------------
  55.   def run(url, dstfile, open=false)
  56.     result = @@urlDownloadToFile.call(NIL, url, dstfile, 0, 0)
  57.     if (result == S_OK)
  58.       return TRUE unless open
  59.       @@shellExecute.call(NIL, "open", dstfile, NIL, NIL, SW_SHOWNORMAL)
  60.       return TRUE
  61.     else
  62.       raise("URLDownloadToFile call failed: %X\n", result)
  63.     end                      
  64.   end
  65. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement