Skip to content
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

Use SHA256 for archive.org fallback if SHA1 absent #4216

Merged
merged 1 commit into from
Oct 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 22 additions & 20 deletions Core/Types/CkanModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -691,22 +691,16 @@ public string StandardName()
=> StandardName(identifier, version);

public static string StandardName(string identifier, ModuleVersion version)
{
// Versions can contain ALL SORTS OF WACKY THINGS! Colons, friggin newlines,
// slashes, and heaven knows what use mod authors try to smoosh into them.
// We'll reduce this down to "friendly" characters, replacing everything else with
// dashes. This doesn't change look-ups, as we use the hash prefix for that.
string version_string = Regex.Replace(version.ToString(), "[^A-Za-z0-9_.-]", "-");

return identifier + "-" + version_string + ".zip";
}
=> $"{identifier}-{sanitizerPattern.Replace(version.ToString(), "-")}.zip";

public override string ToString()
=> string.Format("{0} {1}", identifier, version);

public string DescribeInstallStanzas(IGame game)
=> install == null ? ModuleInstallDescriptor.DefaultInstallStanza(game, identifier).DescribeMatch()
: string.Join(", ", install.Select(mid => mid.DescribeMatch()));
=> install == null
? ModuleInstallDescriptor.DefaultInstallStanza(game, identifier)
.DescribeMatch()
: string.Join(", ", install.Select(mid => mid.DescribeMatch()));

/// <summary>
/// Return an archive.org URL for this download, or null if it's not there.
Expand All @@ -717,23 +711,31 @@ public Uri? InternetArchiveDownload
=> !license.Any(l => l.Redistributable)
? null
: InternetArchiveURL(
Truncate(bucketExcludePattern.Replace(identifier + "-"
+ version.ToString()
.Replace(' ', '_')
.Replace(':', '-'),
""),
Truncate(bucketExcludePattern.Replace(identifier
+ "-"
+ version.ToString()
.Replace(' ', '_')
.Replace(':', '-'),
""),
100),
// Some alternate registry repositories don't set download_hash
download_hash?.sha1);
download_hash?.sha1 ?? download_hash?.sha256);

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

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

// Versions can contain ALL SORTS OF WACKY THINGS! Colons, friggin newlines,
// slashes, and heaven knows what else mod authors try to smoosh into them.
// We'll reduce this down to "friendly" characters, replacing everything else with
// dashes. This doesn't change look-ups, as we use the hash prefix for that.
private static readonly Regex sanitizerPattern = new Regex("[^A-Za-z0-9_.-]",
RegexOptions.Compiled);

// InternetArchive says:
// Bucket names should be valid archive identifiers;
Expand Down