aboutsummaryrefslogtreecommitdiff
path: root/src/WixToolset.Core/Bind/ExtractEmbeddedFiles.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/WixToolset.Core/Bind/ExtractEmbeddedFiles.cs')
-rw-r--r--src/WixToolset.Core/Bind/ExtractEmbeddedFiles.cs38
1 files changed, 21 insertions, 17 deletions
diff --git a/src/WixToolset.Core/Bind/ExtractEmbeddedFiles.cs b/src/WixToolset.Core/Bind/ExtractEmbeddedFiles.cs
index 644e5c63..35c8a2f0 100644
--- a/src/WixToolset.Core/Bind/ExtractEmbeddedFiles.cs
+++ b/src/WixToolset.Core/Bind/ExtractEmbeddedFiles.cs
@@ -15,7 +15,7 @@ namespace WixToolset.Core.Bind
15 /// </summary> 15 /// </summary>
16 internal class ExtractEmbeddedFiles 16 internal class ExtractEmbeddedFiles
17 { 17 {
18 private Dictionary<Uri, SortedList<int, string>> filesWithEmbeddedFiles = new Dictionary<Uri, SortedList<int, string>>(); 18 private readonly Dictionary<Uri, SortedList<string, string>> filesWithEmbeddedFiles = new Dictionary<Uri, SortedList<string, string>>();
19 19
20 public IEnumerable<Uri> Uris => this.filesWithEmbeddedFiles.Keys; 20 public IEnumerable<Uri> Uris => this.filesWithEmbeddedFiles.Keys;
21 21
@@ -23,28 +23,28 @@ namespace WixToolset.Core.Bind
23 /// Adds an embedded file index to track and returns the path where the embedded file will be extracted. Duplicates will return the same extract path. 23 /// Adds an embedded file index to track and returns the path where the embedded file will be extracted. Duplicates will return the same extract path.
24 /// </summary> 24 /// </summary>
25 /// <param name="uri">Uri to file containing the embedded files.</param> 25 /// <param name="uri">Uri to file containing the embedded files.</param>
26 /// <param name="embeddedFileIndex">Index of the embedded file to extract.</param> 26 /// <param name="embeddedFileId">Id of the embedded file to extract.</param>
27 /// <param name="tempPath">Path where temporary files should be placed.</param> 27 /// <param name="extractFolder">Folder where extracted files should be placed.</param>
28 /// <returns>The extract path for the embedded file.</returns> 28 /// <returns>The extract path for the embedded file.</returns>
29 public string AddEmbeddedFileIndex(Uri uri, int embeddedFileIndex, string tempPath) 29 public string AddEmbeddedFileToExtract(Uri uri, string embeddedFileId, string extractFolder)
30 { 30 {
31 // If the uri to the file that contains the embedded file does not already have embedded files 31 // If the uri to the file that contains the embedded file does not already have embedded files
32 // being extracted, create the dictionary to track that. 32 // being extracted, create the dictionary to track that.
33 if (!this.filesWithEmbeddedFiles.TryGetValue(uri, out var extracts)) 33 if (!this.filesWithEmbeddedFiles.TryGetValue(uri, out var extracts))
34 { 34 {
35 extracts = new SortedList<int, string>(); 35 extracts = new SortedList<string, string>(StringComparer.OrdinalIgnoreCase);
36 this.filesWithEmbeddedFiles.Add(uri, extracts); 36 this.filesWithEmbeddedFiles.Add(uri, extracts);
37 } 37 }
38 38
39 // If the embedded file is not already tracked in the dictionary of extracts, add it. 39 // If the embedded file is not already tracked in the dictionary of extracts, add it.
40 if (!extracts.TryGetValue(embeddedFileIndex, out var extractPath)) 40 if (!extracts.TryGetValue(embeddedFileId, out var extractPath))
41 { 41 {
42 string localFileNameWithoutExtension = Path.GetFileNameWithoutExtension(uri.LocalPath); 42 var localFileNameWithoutExtension = Path.GetFileNameWithoutExtension(uri.LocalPath);
43 string unique = this.HashUri(uri.AbsoluteUri); 43 var unique = this.HashUri(uri.AbsoluteUri);
44 string extractedName = String.Format(CultureInfo.InvariantCulture, @"{0}_{1}\{2}", localFileNameWithoutExtension, unique, embeddedFileIndex); 44 var extractedName = String.Format(CultureInfo.InvariantCulture, @"{0}_{1}\{2}", localFileNameWithoutExtension, unique, embeddedFileId);
45 45
46 extractPath = Path.Combine(tempPath, extractedName); 46 extractPath = Path.GetFullPath(Path.Combine(extractFolder, extractedName));
47 extracts.Add(embeddedFileIndex, extractPath); 47 extracts.Add(embeddedFileId, extractPath);
48 } 48 }
49 49
50 return extractPath; 50 return extractPath;
@@ -52,35 +52,39 @@ namespace WixToolset.Core.Bind
52 52
53 public IEnumerable<ExpectedExtractFile> GetExpectedEmbeddedFiles() 53 public IEnumerable<ExpectedExtractFile> GetExpectedEmbeddedFiles()
54 { 54 {
55 var files = new List<ExpectedExtractFile>();
56
55 foreach (var uriWithExtracts in this.filesWithEmbeddedFiles) 57 foreach (var uriWithExtracts in this.filesWithEmbeddedFiles)
56 { 58 {
57 foreach (var extracts in uriWithExtracts.Value) 59 foreach (var extracts in uriWithExtracts.Value)
58 { 60 {
59 yield return new ExpectedExtractFile 61 files.Add(new ExpectedExtractFile
60 { 62 {
61 Uri = uriWithExtracts.Key, 63 Uri = uriWithExtracts.Key,
62 EmbeddedFileIndex = extracts.Key, 64 EmbeddedFileId = extracts.Key,
63 OutputPath = extracts.Value, 65 OutputPath = extracts.Value,
64 }; 66 });
65 } 67 }
66 } 68 }
69
70 return files;
67 } 71 }
68 72
69 public IEnumerable<ExpectedExtractFile> GetExtractFilesForUri(Uri uri) 73 public IEnumerable<ExpectedExtractFile> GetExtractFilesForUri(Uri uri)
70 { 74 {
71 if (!this.filesWithEmbeddedFiles.TryGetValue(uri, out var extracts)) 75 if (!this.filesWithEmbeddedFiles.TryGetValue(uri, out var extracts))
72 { 76 {
73 extracts = new SortedList<int, string>(); 77 extracts = new SortedList<string, string>(StringComparer.OrdinalIgnoreCase);
74 } 78 }
75 79
76 return extracts.Select(e => new ExpectedExtractFile() { Uri = uri, EmbeddedFileIndex = e.Key, OutputPath = e.Value }); 80 return extracts.Select(e => new ExpectedExtractFile { Uri = uri, EmbeddedFileId = e.Key, OutputPath = e.Value });
77 } 81 }
78 82
79 private string HashUri(string uri) 83 private string HashUri(string uri)
80 { 84 {
81 using (SHA1 sha1 = new SHA1CryptoServiceProvider()) 85 using (SHA1 sha1 = new SHA1CryptoServiceProvider())
82 { 86 {
83 byte[] hash = sha1.ComputeHash(Encoding.UTF8.GetBytes(uri)); 87 var hash = sha1.ComputeHash(Encoding.UTF8.GetBytes(uri));
84 return Convert.ToBase64String(hash).TrimEnd('=').Replace('+', '-').Replace('/', '_'); 88 return Convert.ToBase64String(hash).TrimEnd('=').Replace('+', '-').Replace('/', '_');
85 } 89 }
86 } 90 }