-
-
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
base: master
Are you sure you want to change the base?
Changes from 2 commits
4385e10
6f8b7e9
285df06
e6e94d6
3ff164d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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,92 @@ 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(); | ||||||
public static void DownloadLanguageTags() | ||||||
{ | ||||||
CreateSldrCacheDirectory(); | ||||||
string cachedAllTagsPath; | ||||||
cachedAllTagsPath = Path.Combine(SldrCachePath, "langtags.json"); | ||||||
string etagPath; | ||||||
etagPath = Path.Combine(SldrCachePath, "langtags.json.etag"); | ||||||
string etag; | ||||||
string currentEtag; | ||||||
try | ||||||
{ | ||||||
if (_offlineMode) | ||||||
throw new WebException("Test mode: SLDR offline so accessing cache", WebExceptionStatus.ConnectFailure); | ||||||
// 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.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 (File.Exists(etagPath)) | ||||||
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. You'll also have to check for the existence of |
||||||
{ | ||||||
etag = File.ReadAllText(etagPath); | ||||||
webRequest.Headers.Set(etag, "If-None-Match"); | ||||||
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. This is backwards.
Suggested change
|
||||||
currentEtag = webResponse.Headers.Get("Etag"); | ||||||
if (!etag.Equals(currentEtag)) | ||||||
{ | ||||||
//File.WriteAllText(etagPath, currentEtag); | ||||||
//webRequest.Headers.Set(etag, "If-None-Match"); | ||||||
if (webResponse.StatusCode != HttpStatusCode.NotModified) | ||||||
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. The status code should be checked directly after calling In all cases I saw in my testing we got a |
||||||
{ | ||||||
File.WriteAllText(etagPath, currentEtag); | ||||||
using Stream output = File.OpenWrite(cachedAllTagsPath); | ||||||
using var input = webResponse.GetResponseStream(); | ||||||
input.CopyTo(output); | ||||||
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. It's possible that
Suggested change
|
||||||
} | ||||||
} | ||||||
} | ||||||
else | ||||||
{ | ||||||
currentEtag = webResponse.Headers.Get("Etag"); | ||||||
File.WriteAllText(etagPath, currentEtag); | ||||||
if (webResponse.StatusCode != HttpStatusCode.NotModified) | ||||||
{ | ||||||
using Stream output = File.OpenWrite(cachedAllTagsPath); | ||||||
using var input = webResponse.GetResponseStream(); | ||||||
input.CopyTo(output); | ||||||
} | ||||||
} | ||||||
catch (WebException) | ||||||
{ | ||||||
} | ||||||
catch (UnauthorizedAccessException) | ||||||
{ | ||||||
} | ||||||
catch (IOException) | ||||||
{ | ||||||
} | ||||||
} | ||||||
_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 | ||||||
|
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.
These two lines can go directly below
CreateSldrCacheDirectory()
.