-
Notifications
You must be signed in to change notification settings - Fork 54
fix map download and scynchronizaiton #3053
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,69 +1,62 @@ | ||
| using System; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using System.Net; | ||
| using System.Text.RegularExpressions; | ||
| using Newtonsoft.Json; | ||
| using ZkData; | ||
|
|
||
| namespace ZeroKWeb | ||
| { | ||
| public class WebFolderSyncer | ||
| { | ||
| private string urlSource; | ||
|
|
||
| public WebFolderSyncer(string urlSource = "http://api.springfiles.com/files/maps/") | ||
| { | ||
| this.urlSource = urlSource; | ||
| } | ||
|
|
||
| public List<string> GetFileList() | ||
| { | ||
| var ret = new List<string>(); | ||
| using (var wc = new WebClient()) | ||
| { | ||
| var str = wc.DownloadString(urlSource); | ||
| return (from Match m in Regex.Matches(str, "<a href=\"([^\"]+)\">([^<]+)</a>") | ||
| where m.Success && m.Groups[1].Value == m.Groups[2].Value | ||
| select m.Groups[1].Value).ToList(); | ||
| var url = $"{GlobalConst.SpringfilesBaseUrl}json.php?category=map&springname=*&logical=or&nosensitive=on&limit=1000000&offset=0"; | ||
| var json = wc.DownloadString(url); | ||
| var entries = JsonConvert.DeserializeObject<List<SpringfilesEntry>>(json); | ||
| if (entries == null) return ret; | ||
| foreach (var e in entries) | ||
| { | ||
| if (e?.path == "maps" && !string.IsNullOrEmpty(e.filename) && e.mirrors != null && e.mirrors.Length > 0) | ||
| ret.Add(e.filename); | ||
| } | ||
| } | ||
| return ret; | ||
| } | ||
|
|
||
| public void DownloadFile(string targetFolder, string file) | ||
| public bool DownloadFile(string targetFolder, string file) | ||
| { | ||
| if (!Directory.Exists(targetFolder)) Directory.CreateDirectory(targetFolder); | ||
| var targetFile = Path.Combine(targetFolder, file); | ||
| if (!File.Exists(targetFile)) | ||
| if (File.Exists(targetFile)) return true; | ||
|
|
||
| var tempFile = Path.GetTempFileName(); | ||
| using (var wc = new WebClient()) | ||
| { | ||
| using (var wc = new WebClient()) | ||
| try | ||
| { | ||
| var tempFile = Path.GetTempFileName(); | ||
| try | ||
| { | ||
| wc.DownloadFile(urlSource + file, tempFile); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| Trace.TraceWarning("Download of file {0} failed: {1}", file, ex.Message); | ||
| File.Delete(tempFile); | ||
| } | ||
| try | ||
| { | ||
| File.Move(tempFile, targetFile); | ||
| } | ||
| catch { } | ||
| wc.DownloadFile($"{GlobalConst.SpringfilesBaseUrl}files/maps/{file}", tempFile); | ||
| File.Move(tempFile, targetFile); | ||
| return true; | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| Trace.TraceWarning("Springfiles download of {0} failed: {1}", file, ex.Message); | ||
| try { File.Delete(tempFile); } catch { } | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| public void SynchronizeFolders(string targetFolder) | ||
| private class SpringfilesEntry | ||
| { | ||
| if (!Directory.Exists(targetFolder)) Directory.CreateDirectory(targetFolder); | ||
| foreach (var file in GetFileList()) | ||
| { | ||
| DownloadFile(targetFolder, file); | ||
| } | ||
| public string filename { get; set; } | ||
| public string path { get; set; } | ||
| public string[] mirrors { get; set; } | ||
| } | ||
|
|
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.