private void ModuleDownloadComplete(Uri url, string filename, Exception error) { if (error != null) { log.Info(error.ToString()); } else { // Cache if this download succeeded try { CkanModule module = modules.First(m => m.download == url); cache.Store(module, filename, module.StandardName()); File.Delete(filename); } catch (InvalidModuleFileKraken kraken) { User.RaiseError(kraken.ToString()); } catch (FileNotFoundException e) { log.WarnFormat("cache.Store(): FileNotFoundException: {0}", e.Message); } } }
internal SelectionReason ReasonFor(CkanModule mod) { if (!ModList().Contains(mod)) { throw new ArgumentException("Mod " + mod.StandardName() + " is not in the list"); } return(reasons[mod]); }
/// <summary> /// Try to add a file to the module cache. /// Throws exceptions if the file doesn't match the metadata. /// </summary> /// <param name="module">The module object corresponding to the download</param> /// <param name="path">Path to the file to add</param> /// <param name="description">Description of the file</param> /// <param name="move">True to move the file, false to copy</param> /// <returns> /// Name of the new file in the cache /// </returns> public string Store(CkanModule module, string path, string description = null, bool move = false) { // Check file exists FileInfo fi = new FileInfo(path); if (!fi.Exists) { throw new FileNotFoundKraken(path); } // Check file size if (module.download_size > 0 && fi.Length != module.download_size) { throw new InvalidModuleFileKraken(module, path, $"{module}: {path} has length {fi.Length}, should be {module.download_size}"); } // Check valid CRC string invalidReason; if (!NetFileCache.ZipValid(path, out invalidReason)) { throw new InvalidModuleFileKraken(module, path, $"{module}: {path} is not a valid ZIP file: {invalidReason}"); } // Some older metadata doesn't have hashes if (module.download_hash != null) { // Check SHA1 match string sha1 = GetFileHashSha1(path); if (sha1 != module.download_hash.sha1) { throw new InvalidModuleFileKraken(module, path, $"{module}: {path} has SHA1 {sha1}, should be {module.download_hash.sha1}"); } // Check SHA256 match string sha256 = GetFileHashSha256(path); if (sha256 != module.download_hash.sha256) { throw new InvalidModuleFileKraken(module, path, $"{module}: {path} has SHA256 {sha256}, should be {module.download_hash.sha256}"); } } // If no exceptions, then everything is fine return(cache.Store(module.download, path, description ?? module.StandardName(), move)); }
/// <summary> /// Download the given mod. Returns the filename it was saved to. /// /// If no filename is provided, the standard_name() will be used. /// /// </summary> /// <param name="filename">Filename.</param> public string Download(CkanModule module, string filename = null) { // Generate a temporary file if none is provided. if (filename == null) { filename = module.StandardName(); } Console.WriteLine(" * Downloading " + filename + "..."); string fullPath = Path.Combine(KSP.DownloadCacheDir(), filename); log.DebugFormat("Downloading {0} to {1}", module.download, fullPath); WebClient agent = new WebClient(); try { agent.DownloadFile(module.download, fullPath); } catch (Exception ex) { // Clean up our file, it's unlikely to be complete. // It's okay if this fails. try { log.DebugFormat("Removing {0} after web error failure", fullPath); File.Delete(fullPath); } catch { // Apparently we need a catch, even if we do nothing. } if (ex is System.Net.WebException && Regex.IsMatch(ex.Message, "authentication or decryption has failed")) { Console.WriteLine("\nOh no! Our download failed!\n"); Console.WriteLine("\t{0}\n", ex.Message); Console.WriteLine("If you're on Linux, try running:\n"); Console.WriteLine("\tmozroots --import --ask-remove\n"); Console.WriteLine("on the command-line to update your certificate store, and try again.\n"); // TODO: Throw an exception that signals we need to exit, rather than // stopping all other code from tidying up. (We do this for now, so // we don't have ugly stack-traces on things we kinda expect.) Environment.Exit(MainClass.EXIT_ERROR); } throw; } return(fullPath); }
/// <summary> /// Returns the path to a cached copy of a module if it exists, or downloads /// and returns the downloaded copy otherwise. /// /// If no filename is provided, the module's standard name will be used. /// Chcecks provided cache first. /// </summary> public static string CachedOrDownload(string identifier, Version version, Uri url, NetFileCache cache, string filename = null) { if (filename == null) { filename = CkanModule.StandardName(identifier, version); } string full_path = cache.GetCachedZip(url); if (full_path == null) { return(Download(url, filename, cache)); } log.DebugFormat("Using {0} (cached)", filename); return(full_path); }
/// <summary> /// Returns the path to a cached copy of a module if it exists, or downloads /// and returns the downloaded copy otherwise. /// /// If no filename is provided, the module's standard name will be used. /// Chcecks provided cache first. /// </summary> public static string CachedOrDownload(CkanModule module, NetModuleCache cache, string filename = null) { if (filename == null) { filename = CkanModule.StandardName(module.identifier, module.version); } string full_path = cache.GetCachedZip(module); if (full_path == null) { return(Download(module, filename, cache)); } log.DebugFormat("Using {0} (cached)", filename); return(full_path); }
public string CachedOrDownload(CkanModule module, string filename = null) { if (filename == null) { filename = module.StandardName(); } string fullPath = CachePath(filename); if (File.Exists(fullPath)) { Console.WriteLine(" * Using {0} (cached)", filename); return(fullPath); } return(Download(module, filename)); }