-
-
Notifications
You must be signed in to change notification settings - Fork 50
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
Langtag download issue 987 #1232
Open
elisunger
wants to merge
5
commits into
sillsdev:master
Choose a base branch
from
elisunger:langtagDownload-issue-987
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4385e10
Download langtags using etag instead of if-modified-since header
elisunger 6f8b7e9
changes langtagDownloads
elisunger 285df06
Add unit tests for downloading langtags.json
ermshiperete e6e94d6
address code review
elisunger 3ff164d
Merge remote-tracking branch 'upstream/master' into langtagDownload-i…
elisunger 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 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 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 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 | ||||
---|---|---|---|---|---|---|
|
@@ -451,8 +451,9 @@ public static IReadOnlyKeyedCollection<string, SldrLanguageTagInfo> LanguageTags | |||||
} | ||||||
} | ||||||
|
||||||
public static void InitializeLanguageTags() | ||||||
public static void InitializeLanguageTags(bool downloadLangTags = true) | ||||||
{ | ||||||
if (downloadLangTags) DownloadLanguageTags(); | ||||||
LoadLanguageTagsIfNecessary(); | ||||||
} | ||||||
|
||||||
|
@@ -470,55 +471,90 @@ private static void LoadLanguageTagsIfNecessary() | |||||
CreateSldrCacheDirectory(); | ||||||
|
||||||
cachedAllTagsPath = Path.Combine(SldrCachePath, "langtags.json"); | ||||||
string etagPath; | ||||||
etagPath = Path.Combine(SldrCachePath, "langtags.json.etag"); | ||||||
var sinceTime = _embeddedAllTagsTime.ToUniversalTime(); | ||||||
if (File.Exists(cachedAllTagsPath)) | ||||||
{ | ||||||
var fileTime = File.GetLastWriteTimeUtc(cachedAllTagsPath); | ||||||
if (sinceTime > fileTime) | ||||||
{ | ||||||
// delete the old langtags.json file if a newer embedded one is available. | ||||||
// this can happen if the application is upgraded to use a newer version of SIL.WritingSystems | ||||||
// that has an updated embedded langtags.json file. | ||||||
File.Delete(cachedAllTagsPath); | ||||||
File.Delete(etagPath); | ||||||
} | ||||||
else | ||||||
sinceTime = fileTime; | ||||||
} | ||||||
sinceTime += TimeSpan.FromSeconds(1); | ||||||
try | ||||||
{ | ||||||
if (_offlineMode) | ||||||
throw new WebException("Test mode: SLDR offline so accessing cache", WebExceptionStatus.ConnectFailure); | ||||||
|
||||||
} | ||||||
_languageTags = new ReadOnlyKeyedCollection<string, SldrLanguageTagInfo>(ParseAllTagsJson(cachedAllTagsPath)); | ||||||
} | ||||||
|
||||||
// get SLDR langtags.json from the SLDR api compressed | ||||||
// it will throw WebException or have status HttpStatusCode.NotModified if file is unchanged or not get it | ||||||
var langtagsUrl = | ||||||
$"{SldrRepository}index.html?query=langtags&ext=json{StagingParameter}"; | ||||||
var webRequest = (HttpWebRequest) WebRequest.Create(Uri.EscapeUriString(langtagsUrl)); | ||||||
webRequest.UserAgent = UserAgent; | ||||||
webRequest.IfModifiedSince = sinceTime; | ||||||
webRequest.Timeout = 10000; | ||||||
webRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; | ||||||
using var webResponse = (HttpWebResponse) webRequest.GetResponse(); | ||||||
if (webResponse.StatusCode != HttpStatusCode.NotModified) | ||||||
public static void DownloadLanguageTags() | ||||||
{ | ||||||
CreateSldrCacheDirectory(); | ||||||
if (_offlineMode) | ||||||
throw new WebException("Test mode: SLDR offline so accessing cache", WebExceptionStatus.ConnectFailure); | ||||||
|
||||||
string cachedAllTagsPath; | ||||||
cachedAllTagsPath = Path.Combine(SldrCachePath, "langtags.json"); | ||||||
string etagPath; | ||||||
etagPath = Path.Combine(SldrCachePath, "langtags.json.etag"); | ||||||
string etag; | ||||||
string currentEtag; | ||||||
try | ||||||
{ | ||||||
// get SLDR langtags.json from the SLDR api compressed | ||||||
// it will throw WebException or have status HttpStatusCode.NotModified if file is unchanged or not get it | ||||||
var langtagsUrl = | ||||||
$"{SldrRepository}index.html?query=langtags&ext=json{StagingParameter}"; | ||||||
var webRequest = (HttpWebRequest)WebRequest.Create(Uri.EscapeUriString(langtagsUrl)); | ||||||
webRequest.Headers.Set("If-None-Match", etag); | ||||||
webRequest.UserAgent = UserAgent; | ||||||
webRequest.Timeout = 10000; | ||||||
webRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; | ||||||
ermshiperete marked this conversation as resolved.
Show resolved
Hide resolved
ermshiperete marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
using var webResponse = (HttpWebResponse)webRequest.GetResponse(); | ||||||
if (webResponse.StatusCode != "OK") | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
{ | ||||||
return; | ||||||
} | ||||||
|
||||||
if (File.Exists(etagPath) && File.Exists(cachedAllTagsPath)) | ||||||
{ | ||||||
etag = File.ReadAllText(etagPath); | ||||||
currentEtag = webResponse.Headers.Get("Etag"); | ||||||
if (!etag.Equals(currentEtag)) | ||||||
{ | ||||||
File.WriteAllText(etagPath, currentEtag); | ||||||
using Stream output = File.OpenWrite(cachedAllTagsPath); | ||||||
using var input = webResponse.GetResponseStream(); | ||||||
input.CopyTo(output); | ||||||
input?.CopyTo(output); | ||||||
} | ||||||
} | ||||||
catch (WebException) | ||||||
{ | ||||||
} | ||||||
catch (UnauthorizedAccessException) | ||||||
{ | ||||||
} | ||||||
catch (IOException) | ||||||
else | ||||||
{ | ||||||
currentEtag = webResponse.Headers.Get("Etag"); | ||||||
File.WriteAllText(etagPath, currentEtag); | ||||||
using Stream output = File.OpenWrite(cachedAllTagsPath); | ||||||
using var input = webResponse.GetResponseStream(); | ||||||
input?.CopyTo(output); | ||||||
} | ||||||
} | ||||||
_languageTags = new ReadOnlyKeyedCollection<string, SldrLanguageTagInfo>(ParseAllTagsJson(cachedAllTagsPath)); | ||||||
catch (WebException) | ||||||
{ | ||||||
} | ||||||
catch (UnauthorizedAccessException) | ||||||
{ | ||||||
} | ||||||
catch (IOException) | ||||||
{ | ||||||
} | ||||||
} | ||||||
|
||||||
|
||||||
private static IKeyedCollection<string, SldrLanguageTagInfo> ParseAllTagsJson(string cachedAllTagsPath) | ||||||
{ | ||||||
// read in the json file | ||||||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The compiler doesn't like this line and fails with
Error CS0165 : Use of unassigned local variable 'etag'
. You should readetagPath
before this line (you can copy lines 525 and 527).