downloader.gd 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. extends HTTPRequest
  2. var kinds = {
  3. "topo": "http://imbrium.mit.edu/DATA/SLDEM2015/TILES/FLOAT_IMG/%s",
  4. "color": "https://nextcloud.alfi.li/nextcloud/index.php/s/dMELm8P8sPxbC5f/download?path=%2F&files=%s"
  5. }
  6. var downloads = {}
  7. var downloading = false
  8. onready var UI = get_node("/root/Main/UI/Downloader")
  9. signal file_downloaded
  10. signal downloads_finished
  11. # signal stats_updated -> now via Messenger:statusUpdate
  12. signal downloads_started
  13. export(bool) var blind_mode : bool = false
  14. var _headers : Array = []
  15. var _last_method : int
  16. var _ssl : bool = false
  17. var _file := File.new()
  18. var _timer : float = 0.0
  19. var currentDownload : Download
  20. class Download:
  21. var _file_name : String
  22. var _file_size : float
  23. var _current_url : String
  24. var _kind : String
  25. var _downloaded_percent : float = 0
  26. var _downloaded_size : float = 0
  27. func _init() -> void:
  28. set_process(false)
  29. connect("request_completed", self, "_on_request_completed")
  30. func get_stats() -> Download:
  31. return currentDownload
  32. func _reset() -> void:
  33. currentDownload = Download.new()
  34. func _downloads_done() -> void:
  35. set_process(false)
  36. _update_stats()
  37. _file.close()
  38. emit_signal("downloads_finished", currentDownload)
  39. _reset()
  40. downloading = false
  41. UI.hide()
  42. func _update_stats() -> void:
  43. if blind_mode == false:
  44. _calculate_percentage()
  45. Messenger.emit_signal("statusUpdate",currentDownload)
  46. func _process(delta):
  47. if not downloading:
  48. if len(downloads) == 0:
  49. set_process(false)
  50. return
  51. for kind in downloads:
  52. for file in downloads[kind]:
  53. var dl = downloads[kind][file]
  54. currentDownload = dl
  55. _download()
  56. _timer += delta
  57. if _timer > 1:
  58. _update_stats()
  59. _timer = 0.0
  60. func _download():
  61. ensureFolder(currentDownload._kind)
  62. download_file = currentDownload._file_name
  63. _send_head_request()
  64. print("downloading %s" %currentDownload._current_url)
  65. downloading = true
  66. UI.show()
  67. func ensureFolder(kind):
  68. var dir = Directory.new()
  69. dir.open("user://")
  70. if not dir.dir_exists(kind):
  71. dir.make_dir(kind)
  72. func download(file, kind):
  73. set_process(true)
  74. if not kinds.has(kind):
  75. print("don't know kind %s"%kind)
  76. return
  77. if not downloads.has(kind):
  78. downloads[kind] = {}
  79. if downloads[kind].has(file):
  80. return
  81. var download = Download.new()
  82. download._file_name = "user://%s/%s"%[kind, file]
  83. download._current_url = make_filename(kind, file)
  84. download._kind = kind
  85. downloads[kind][download._file_name] = download
  86. func make_filename(kind, file):
  87. return kinds[kind]%file
  88. func _send_head_request() -> void:
  89. # The HEAD method only gets the head and not the body. Therefore, doesn't
  90. # download the file.
  91. request(currentDownload._current_url, _headers, _ssl, HTTPClient.METHOD_HEAD)
  92. _last_method = HTTPClient.METHOD_HEAD
  93. func _send_get_request() -> void:
  94. var error = request(currentDownload._current_url, _headers, _ssl, HTTPClient.METHOD_GET)
  95. if error == OK:
  96. emit_signal("downloads_started")
  97. _last_method = HTTPClient.METHOD_GET
  98. set_process(true)
  99. elif error == ERR_INVALID_PARAMETER:
  100. print("Given string isn't a valid url ")
  101. elif error == ERR_CANT_CONNECT:
  102. print("Can't connect to host")
  103. func _on_request_completed(p_result,
  104. _p_response_code,
  105. p_headers,
  106. _p_body) -> void:
  107. if p_result == RESULT_SUCCESS:
  108. if _p_response_code != 200:
  109. push_error("Download HTTP Error %s:%s" %[_p_response_code,_p_body])
  110. return
  111. if _last_method == HTTPClient.METHOD_HEAD and blind_mode == false:
  112. var regex = "(?i)content-length: [0-9]*"
  113. var size = _extract_regex_from_header(regex, p_headers.join(' '))
  114. size = size.replace("Content-Length: ", "")
  115. currentDownload._file_size = size.to_float()
  116. _send_get_request()
  117. elif _last_method == HTTPClient.METHOD_HEAD and blind_mode == true:
  118. _send_get_request()
  119. elif _last_method == HTTPClient.METHOD_GET:
  120. print("file download complete %s"%currentDownload._current_url)
  121. _file.close()
  122. emit_signal("file_downloaded", currentDownload)
  123. Messenger.emit_signal("fileDownloaded", currentDownload)
  124. downloading = false
  125. remove_download()
  126. elif p_result == RESULT_DOWNLOAD_FILE_CANT_OPEN:
  127. push_error("Download cannot open file %s" %currentDownload._file_name)
  128. else:
  129. print("HTTP Request error: ", p_result)
  130. func remove_download():
  131. downloads[currentDownload._kind].erase(currentDownload._file_name)
  132. if len(downloads[currentDownload._kind]) == 0:
  133. downloads.erase(currentDownload._kind)
  134. if len(downloads) == 0:
  135. _downloads_done()
  136. func _calculate_percentage() -> void:
  137. var error : int
  138. error = _file.open(download_file, File.READ)
  139. if error == OK and currentDownload._file_size > 0:
  140. currentDownload._downloaded_size = _file.get_len()
  141. currentDownload._downloaded_percent = (currentDownload._downloaded_size / currentDownload._file_size) *100
  142. func _extract_regex_from_header(p_regex : String,
  143. p_header : String) -> String:
  144. var regex = RegEx.new()
  145. regex.compile(p_regex)
  146. var result = regex.search(p_header)
  147. return result.get_string()