James1337

_FTP_ProgressDownload

Sep 21st, 2013
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 6.07 KB | None | 0 0
  1. ; #FUNCTION# ====================================================================================================================
  2. ; Name...........: _FTP_ProgressDownload
  3. ; Description ...: Downloads a file in Binary Mode and shows  a Progress window or by Calling a User defined Function.
  4. ; Syntax.........: _FTP_ProgressDownload($l_FTPSession, $s_LocalFile, $s_RemoteFile[, $FunctionToCall = ""])
  5. ; Parameters ....: $l_FTPSession    - as returned by _FTP_Connect().
  6. ;                  $s_LocalFile     - The local file to create.
  7. ;                  $s_RemoteFile    - The remote source file.
  8. ;                  $FunctionToCall  - [Optional] A function which can update a Progressbar and
  9. ;                                      react on UserInput like Click on Abort or Close App.
  10. ;                                      (More info in the end of this comment)
  11. ; Return values .: Success - 1
  12. ;                  Error: 0 and @error:
  13. ;                           -1 -> Local file couldn't be created
  14. ;                           -2 -> Unable to get RemoteFile size
  15. ;                           -3 -> Open RemoteFile failed
  16. ;                           -4 -> Read from Remotefile failed
  17. ;                           -5 -> Close RemoteFile failed
  18. ;                           -6 -> Download aborted by PercentageFunc, Return of Called Function
  19. ;                           -7 -> Local file write failed
  20. ; Author ........: limette, Prog@ndy, James (autoit.de)
  21. ; Modified.......: jchd
  22. ; Remarks .......:
  23. ; Information about $FunctionToCall:
  24. ;   Parameter: $Percentage - The Percentage of Progress
  25. ;   Return Values: Continue Download - 1
  26. ;                  Abort Download    - 0 Or negative
  27. ;                       These Return Values are returned by _FTP_ProgressDownload(), too,
  28. ;                       so you can react on different Actions like Aborting by User, closing App or TimeOut of whatever
  29. ;~   Examples:
  30. ;~                   Func _UpdateProgress($Percentage)
  31. ;~                      ProgressSet($percent,$percent &"%")
  32. ;~                      If _IsPressed("77") Then Return 0 ; Abort on F8
  33. ;~                      Return 1 ; bei 1 Fortsetzten
  34. ;~                   Endfunc
  35. ;
  36. ;~                   Func _UpdateProgress($Percentage)
  37. ;~                      GUICtrlSetData($ProgressBarCtrl,$percent)
  38. ;~                      Switch GUIGetMsg()
  39. ;~                         Case $GUI_EVENT_CLOSE
  40. ;~                            Return -1 ; _FTP_DownloadProgress Aborts with -1, so you can exit you app afterwards
  41. ;~                        Case $Btn_Cancel
  42. ;~                           Return 0 ; Just Cancel, without special Return value
  43. ;~                      EndSwitch
  44. ;~                      Return 1 ; Otherwise contine Download
  45. ;~                   Endfunc
  46. ; Related .......:
  47. ; Link ..........:
  48. ; Example .......:
  49. ; ===============================================================================================================================
  50. Func _FTP_ProgressDownload($l_FTPSession, $s_LocalFile, $s_RemoteFile, $FunctionToCall = "")
  51.     If $__ghWinInet_FTP = -1 Then Return SetError(-2, 0, 0)
  52.  
  53.     Local $fhandle = FileOpen($s_LocalFile, 18)
  54.     If $fhandle < 0 Then Return SetError(-1, 0, 0)
  55.  
  56.     Local $ai_ftpopenfile = DllCall($__ghWinInet_FTP, 'handle', 'FtpOpenFileW', 'handle', $l_FTPSession, 'wstr', $s_RemoteFile, 'dword', $GENERIC_READ, 'dword', $FTP_TRANSFER_TYPE_BINARY, 'dword_ptr', 0)
  57.     If @error Or $ai_ftpopenfile[0] = 0 Then
  58.         FileClose($fhandle) ; <-- !!!
  59.         Return SetError(-3, _WinAPI_GetLastError(), 0)
  60.     EndIf
  61.  
  62.     Local $ai_FTPGetFileSize = DllCall($__ghWinInet_FTP, 'dword', 'FtpGetFileSize', 'handle', $ai_ftpopenfile[0], 'dword*', 0)
  63.     If @error Then
  64.         FileClose($fhandle) ; <-- !!!
  65.         Return SetError(-2, _WinAPI_GetLastError(), 0)
  66.     EndIf
  67.  
  68.     If $FunctionToCall = "" Then ProgressOn("FTP Download", "Downloading " & $s_LocalFile)
  69.  
  70.     Local $glen = _WinAPI_MakeQWord($ai_FTPGetFileSize[0], $ai_FTPGetFileSize[2]) ;FileGetSize($s_RemoteFile)
  71.     Local Const $ChunkSize = 256 * 1024
  72.     Local $last = Mod($glen, $ChunkSize)
  73.  
  74.     Local $parts = Ceiling($glen / $ChunkSize)
  75.     Local $buffer = DllStructCreate("byte[" & $ChunkSize & "]")
  76.  
  77.     Local $ai_InternetCloseHandle, $ai_FTPread, $out, $ret, $lasterror
  78.     Local $x = $ChunkSize
  79.     Local $done = 0
  80.     For $i = 1 To $parts
  81.         If $i = $parts And $last > 0 Then
  82.             $x = $last
  83.         EndIf
  84.  
  85.         $ai_FTPread = DllCall($__ghWinInet_FTP, 'bool', 'InternetReadFile', 'handle', $ai_ftpopenfile[0], 'struct*', $buffer, 'dword', $x, 'dword*', $out)
  86.         If @error Or $ai_FTPread[0] = 0 Then
  87.             $lasterror = _WinAPI_GetLastError()
  88.             $ai_InternetCloseHandle = DllCall($__ghWinInet_FTP, 'bool', 'InternetCloseHandle', 'handle', $ai_ftpopenfile[0])
  89.             ; No need to test @error.
  90.             FileClose($fhandle)
  91.             If $FunctionToCall = "" Then ProgressOff()
  92.             Return SetError(-4, $lasterror, 0)
  93.         EndIf
  94.         $ret = FileWrite($fhandle, BinaryMid(DllStructGetData($buffer, 1), 1, $ai_FTPread[4]))
  95.         If Not $ret Then
  96.             $lasterror = _WinAPI_GetLastError()
  97.             $ai_InternetCloseHandle = DllCall($__ghWinInet_FTP, 'bool', 'InternetCloseHandle', 'handle', $ai_ftpopenfile[0])
  98.             ; No need to test @error.
  99.             FileClose($fhandle)
  100.             FileDelete($s_LocalFile)
  101.             If $FunctionToCall = "" Then ProgressOff()
  102.             Return SetError(-7, $lasterror, 0)
  103.         EndIf
  104.         $done += $ai_FTPread[4]
  105.  
  106.         If $FunctionToCall = "" Then
  107.             ProgressSet(($done / $glen) * 100)
  108.         Else
  109.             $ret = Call($FunctionToCall, ($done / $glen) * 100)
  110.             If $ret <= 0 Then
  111.                 $lasterror = @error
  112.                 $ai_InternetCloseHandle = DllCall($__ghWinInet_FTP, 'bool', 'InternetCloseHandle', 'handle', $ai_ftpopenfile[0])
  113.                 ; No need to test @error.
  114.                 FileClose($fhandle)
  115.                 FileDelete($s_LocalFile)
  116.                 If $FunctionToCall = "" Then ProgressOff()
  117.                 Return SetError(-6, $lasterror, $ret)
  118.             EndIf
  119.         EndIf
  120.         Sleep(10)
  121.     Next
  122.  
  123.     FileClose($fhandle)
  124.  
  125.     If $FunctionToCall = "" Then ProgressOff()
  126.  
  127.     $ai_InternetCloseHandle = DllCall($__ghWinInet_FTP, 'bool', 'InternetCloseHandle', 'handle', $ai_ftpopenfile[0])
  128.     If @error Or $ai_InternetCloseHandle[0] = 0 Then
  129.         Return SetError(-5, _WinAPI_GetLastError(), 0)
  130.     EndIf
  131.  
  132.     Return 1
  133. EndFunc   ;==>_FTP_ProgressDownload
Add Comment
Please, Sign In to add comment