Skip to content

Commit

Permalink
Merge #4043 Use fully sanitized archive.org bucket names
Browse files Browse the repository at this point in the history
  • Loading branch information
HebaruSan committed Feb 28, 2024
2 parents d1387c3 + 1b75925 commit 726adac
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ All notable changes to this project will be documented in this file.
- [Netkan] Improve netkan relationship error message (#4020, #4021 by: HebaruSan)
- [Core] Get KSP2 version from game assembly (#4034 by: HebaruSan)
- [Multiple] Build nuget package, support netstandard2.0 build (#4039 by: HebaruSan)
- [Core] Use fully sanitized archive.org bucket names (#4043 by: HebaruSan)

## v1.34.4 (Niven)

Expand Down
39 changes: 28 additions & 11 deletions Core/Types/CkanModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -755,17 +755,34 @@ public string DescribeInstallStanzas(IGame game)
/// Here it's the first 8 characters of the SHA1 of the DOWNLOADED FILE, not the URL!
/// </summary>
public Uri InternetArchiveDownload
{
get
{
string verStr = version.ToString().Replace(' ', '_').Replace(':', '-');
// Some alternate registry repositories don't set download_hash
return (download_hash?.sha1 != null && license.All(l => l.Redistributable))
? new Uri(
$"https://archive.org/download/{identifier}-{verStr}/{download_hash.sha1.Substring(0, 8)}-{identifier}-{verStr}.zip")
: null;
}
}
=> !license.Any(l => l.Redistributable)
? null
: InternetArchiveURL(
Truncate(bucketExcludePattern.Replace(identifier + "-"
+ version.ToString()
.Replace(' ', '_')
.Replace(':', '-'),
""),
100),
// Some alternate registry repositories don't set download_hash
download_hash?.sha1);

private static string Truncate(string s, int len)
=> s.Length <= len ? s
: s.Substring(0, len);

private static Uri InternetArchiveURL(string bucket, string sha1)
=> string.IsNullOrEmpty(sha1)
? null
: new Uri($"https://archive.org/download/{bucket}/{sha1.Substring(0, 8)}-{bucket}.zip");

// InternetArchive says:
// Bucket names should be valid archive identifiers;
// try someting matching this regular expression:
// ^[a-zA-Z0-9][a-zA-Z0-9_.-]{4,100}$
// (We enforce everything except the minimum of 4 characters)
private static readonly Regex bucketExcludePattern = new Regex(@"^[^a-zA-Z0-9]+|[^a-zA-Z0-9._-]",
RegexOptions.Compiled);

private const double K = 1024;

Expand Down

0 comments on commit 726adac

Please sign in to comment.