:param uri: Download URL. It is rewritten using registered URL rewrite rules before download. :param destFolderPath: Folder to download the file into. :param name: File name that will be downloaded. :param checksum: Checksum formatted as `` : `` to verify
(self, uri, destFolderPath, name, checksum=None, forceDownload=False)
| 1131 | return url |
| 1132 | |
| 1133 | def downloadFile(self, uri, destFolderPath, name, checksum=None, forceDownload=False): |
| 1134 | """ |
| 1135 | :param uri: Download URL. It is rewritten using registered URL rewrite rules before download. |
| 1136 | :param destFolderPath: Folder to download the file into. |
| 1137 | :param name: File name that will be downloaded. |
| 1138 | :param checksum: Checksum formatted as ``<algo>:<digest>`` to verify the downloaded file. For example, ``SHA256:cc211f0dfd9a05ca3841ce1141b292898b2dd2d3f08286affadf823a7e58df93``. |
| 1139 | :param forceDownload: If True then the file is always downloaded even if it already exists in the destination folder. |
| 1140 | """ |
| 1141 | |
| 1142 | uri = self.rewriteUrl(uri) |
| 1143 | |
| 1144 | self.downloadPercent = 0 |
| 1145 | filePath = destFolderPath + "/" + name |
| 1146 | (algo, digest) = extractAlgoAndDigest(checksum) |
| 1147 | if forceDownload or not os.path.exists(filePath) or os.stat(filePath).st_size == 0: |
| 1148 | import urllib.request, urllib.parse, urllib.error |
| 1149 | |
| 1150 | self.logMessage(_("Requesting download {name} from {uri} ...").format(name=name, uri=uri)) |
| 1151 | try: |
| 1152 | urllib.request.urlretrieve(uri, filePath, self.reportHook) |
| 1153 | self.logMessage(_("Download finished")) |
| 1154 | except OSError as e: |
| 1155 | self.logMessage("\t" + _("Download failed: {errorMessage}").format(errorMessage=e), logging.ERROR) |
| 1156 | raise ValueError(_("Failed to download {uri} to {filePath}").format(uri=uri, filePath=filePath)) |
| 1157 | |
| 1158 | if algo is not None: |
| 1159 | self.logMessage(_("Verifying checksum")) |
| 1160 | current_digest = computeChecksum(algo, filePath) |
| 1161 | if current_digest != digest: |
| 1162 | self.logMessage( |
| 1163 | _("Checksum verification failed. Computed checksum {currentChecksum} different from expected checksum {expectedChecksum}").format( |
| 1164 | currentChecksum=current_digest, expectedChecksum=digest)) |
| 1165 | qt.QFile(filePath).remove() |
| 1166 | else: |
| 1167 | self.downloadPercent = 100 |
| 1168 | self.logMessage(_("Checksum OK")) |
| 1169 | else: |
| 1170 | if algo is not None: |
| 1171 | self.logMessage(_("Verifying checksum")) |
| 1172 | current_digest = computeChecksum(algo, filePath) |
| 1173 | if current_digest != digest: |
| 1174 | self.logMessage(_("File already exists in cache but checksum is different - re-downloading it.")) |
| 1175 | qt.QFile(filePath).remove() |
| 1176 | return self.downloadFile(uri, destFolderPath, name, checksum) |
| 1177 | else: |
| 1178 | self.downloadPercent = 100 |
| 1179 | self.logMessage(_("File already exists and checksum is OK - reusing it.")) |
| 1180 | else: |
| 1181 | self.downloadPercent = 100 |
| 1182 | self.logMessage(_("File already exists in cache - reusing it.")) |
| 1183 | return filePath |
| 1184 | |
| 1185 | def loadScene(self, uri, fileProperties={}): |
| 1186 | """Returns True is scene loading was successful, False if failed.""" |