| 182 | |
| 183 | # Get a file content from peer |
| 184 | def getFile(self, site, inner_path, file_size=None, pos_from=0, pos_to=None, streaming=False): |
| 185 | if file_size and file_size > 5 * 1024 * 1024: |
| 186 | max_read_size = 1024 * 1024 |
| 187 | else: |
| 188 | max_read_size = 512 * 1024 |
| 189 | |
| 190 | if pos_to: |
| 191 | read_bytes = min(max_read_size, pos_to - pos_from) |
| 192 | else: |
| 193 | read_bytes = max_read_size |
| 194 | |
| 195 | location = pos_from |
| 196 | |
| 197 | if config.use_tempfiles: |
| 198 | buff = tempfile.SpooledTemporaryFile(max_size=16 * 1024, mode='w+b') |
| 199 | else: |
| 200 | buff = io.BytesIO() |
| 201 | |
| 202 | s = time.time() |
| 203 | while True: # Read in smaller parts |
| 204 | if config.stream_downloads or read_bytes > 256 * 1024 or streaming: |
| 205 | res = self.request("streamFile", {"site": site, "inner_path": inner_path, "location": location, "read_bytes": read_bytes, "file_size": file_size}, stream_to=buff) |
| 206 | if not res or "location" not in res: # Error |
| 207 | return False |
| 208 | else: |
| 209 | self.log("Send: %s" % inner_path) |
| 210 | res = self.request("getFile", {"site": site, "inner_path": inner_path, "location": location, "read_bytes": read_bytes, "file_size": file_size}) |
| 211 | if not res or "location" not in res: # Error |
| 212 | return False |
| 213 | self.log("Recv: %s" % inner_path) |
| 214 | buff.write(res["body"]) |
| 215 | res["body"] = None # Save memory |
| 216 | |
| 217 | if res["location"] == res["size"] or res["location"] == pos_to: # End of file |
| 218 | break |
| 219 | else: |
| 220 | location = res["location"] |
| 221 | if pos_to: |
| 222 | read_bytes = min(max_read_size, pos_to - location) |
| 223 | |
| 224 | if pos_to: |
| 225 | recv = pos_to - pos_from |
| 226 | else: |
| 227 | recv = res["location"] |
| 228 | |
| 229 | self.download_bytes += recv |
| 230 | self.download_time += (time.time() - s) |
| 231 | if self.site: |
| 232 | self.site.settings["bytes_recv"] = self.site.settings.get("bytes_recv", 0) + recv |
| 233 | self.log("Downloaded: %s, pos: %s, read_bytes: %s" % (inner_path, buff.tell(), read_bytes)) |
| 234 | buff.seek(0) |
| 235 | return buff |
| 236 | |
| 237 | # Send a ping request |
| 238 | def ping(self): |