escape invalid characters in release zip paths

This commit is contained in:
Jesse Plamondon-Willard 2017-10-08 02:22:12 -04:00
parent ca58da37cd
commit 0e5982bf9c
1 changed files with 15 additions and 7 deletions

View File

@ -45,18 +45,17 @@ namespace StardewModdingAPI.ModBuildConfig.Tasks
{ {
try try
{ {
// create output path if needed // get names
Directory.CreateDirectory(this.OutputFolderPath); string zipName = this.EscapeInvalidFilenameCharacters($"{this.ModName}-{this.GetManifestVersion()}.zip");
string folderName = this.EscapeInvalidFilenameCharacters(this.ModName);
// get zip filename string zipPath = Path.Combine(this.OutputFolderPath, zipName);
string fileName = $"{this.ModName}-{this.GetManifestVersion()}.zip";
// clear old zip file if present // clear old zip file if present
string zipPath = Path.Combine(this.OutputFolderPath, fileName);
if (File.Exists(zipPath)) if (File.Exists(zipPath))
File.Delete(zipPath); File.Delete(zipPath);
// create zip file // create zip file
Directory.CreateDirectory(this.OutputFolderPath);
using (Stream zipStream = new FileStream(zipPath, FileMode.Create, FileAccess.Write)) using (Stream zipStream = new FileStream(zipPath, FileMode.Create, FileAccess.Write))
using (ZipArchive archive = new ZipArchive(zipStream, ZipArchiveMode.Create)) using (ZipArchive archive = new ZipArchive(zipStream, ZipArchiveMode.Create))
{ {
@ -64,7 +63,7 @@ namespace StardewModdingAPI.ModBuildConfig.Tasks
{ {
// get file info // get file info
string filePath = file.ItemSpec; string filePath = file.ItemSpec;
string entryName = this.ModName + '/' + file.GetMetadata("RecursiveDir") + file.GetMetadata("Filename") + file.GetMetadata("Extension"); string entryName = folderName + '/' + file.GetMetadata("RecursiveDir") + file.GetMetadata("Filename") + file.GetMetadata("Extension");
if (new FileInfo(filePath).Directory.Name.Equals("i18n", StringComparison.InvariantCultureIgnoreCase)) if (new FileInfo(filePath).Directory.Name.Equals("i18n", StringComparison.InvariantCultureIgnoreCase))
entryName = Path.Combine("i18n", entryName); entryName = Path.Combine("i18n", entryName);
@ -138,5 +137,14 @@ namespace StardewModdingAPI.ModBuildConfig.Tasks
IDictionary<string, object> data = (IDictionary<string, object>)new JavaScriptSerializer().DeserializeObject(json); IDictionary<string, object> data = (IDictionary<string, object>)new JavaScriptSerializer().DeserializeObject(json);
return MakeCaseInsensitive(data); return MakeCaseInsensitive(data);
} }
/// <summary>Get a copy of a filename with all invalid filename characters substituted.</summary>
/// <param name="name">The filename.</param>
private string EscapeInvalidFilenameCharacters(string name)
{
foreach (char invalidChar in Path.GetInvalidFileNameChars())
name = name.Replace(invalidChar, '.');
return name;
}
} }
} }