11using SRTPluginManager . Properties ;
22using System . Diagnostics ;
33using System . IO ;
4- using System . Windows ;
54using System . IO . Compression ;
65using System . Net . Http ;
7- using System . Reflection ;
86using System . Text . Json ;
97using System . Threading ;
108using System . Threading . Tasks ;
119using System . Windows . Controls ;
1210using System ;
1311using System . Linq ;
14- using System . Collections . Generic ;
1512
1613namespace SRTPluginManager . Core
1714{
@@ -104,59 +101,61 @@ public static void KillSRT()
104101 process . Kill ( ) ;
105102 }
106103
107- public static async Task DownloadManagerAsync ( string fileName , string url , Button button , string destination )
104+ public static async Task DownloadManagerAsync ( string fileName , string url , Button button , string destination , CancellationToken cancellationToken )
108105 {
109106 var file = Path . Combine ( TempFolderPath , fileName ) ;
110107
111108 // Download file.
112- using ( var hc = new HttpClient ( ) )
113- using ( var s = await hc . GetStreamAsync ( url ) )
114- using ( var fs = new FileStream ( file , FileMode . Create , FileAccess . Write , FileShare . ReadWrite | FileShare . Delete ) )
115- await s . CopyToAsync ( fs ) ;
109+ using ( var httpClient = new HttpClient ( ) )
110+ using ( var downloadStream = await httpClient . GetStreamAsync ( url ) )
111+ using ( var fileStream = new FileStream ( file , FileMode . Create , FileAccess . Write , FileShare . ReadWrite | FileShare . Delete ) )
112+ await downloadStream . CopyToAsync ( fileStream ) ;
116113
117114 // Unzip file.
118- UnzipPackage ( file , destination ) ;
115+ await UnzipPackageAsync ( file , destination , cancellationToken ) ;
119116 autoResetEvent . Set ( ) ;
120117 }
121118
122- public static void UnzipPackage ( string file , string destination )
123- {
124- ZipFile . ExtractToDirectory ( file , destination , true ) ;
125- File . Delete ( file ) ;
126- //if (!Directory.Exists(file))
127- //{
128- // ZipFile.ExtractToDirectory(file, TempFolderPath);
129- //}
130- //File.Delete(file); // deletes temp zip
131- }
132-
133- public static async Task DownloadFileAsync ( string pluginName , string fileName , string url , Button button , string destination , bool isSRT )
119+ public static async Task DownloadFileAsync ( string pluginName , string fileName , Uri downloadUri , Button button , string destination , bool isSRT , CancellationToken cancellationToken )
134120 {
135121 var file = Path . Combine ( TempFolderPath , fileName ) ;
136122
137123 // Download file.
138- using ( var hc = new HttpClient ( ) )
139- using ( var s = await hc . GetStreamAsync ( url ) )
140- using ( var fs = new FileStream ( file , FileMode . Create , FileAccess . Write , FileShare . ReadWrite | FileShare . Delete ) )
141- await s . CopyToAsync ( fs ) ;
124+ using ( var httpClient = new HttpClient ( ) )
125+ using ( var downloadStream = await httpClient . GetStreamAsync ( downloadUri , cancellationToken ) )
126+ using ( var fileStream = new FileStream ( file , FileMode . Create , FileAccess . Write , FileShare . ReadWrite | FileShare . Delete ) )
127+ await downloadStream . CopyToAsync ( fileStream , cancellationToken ) ;
142128
143129 // Unzip file.
144- UnzipPackage ( pluginName , file , destination , isSRT ) ;
130+ await UnzipPackageAsync ( pluginName , file , destination , isSRT , cancellationToken ) ;
145131 autoResetEvent . Set ( ) ;
146132 }
147133
148- public static void UnzipPackage ( string pluginName , string file , string destination , bool isSRT )
134+ public static async Task UnzipPackageAsync ( string file , string destination , CancellationToken cancellationToken )
135+ {
136+ try
137+ {
138+ await using ( var zipArchive = ZipFile . Open ( file , ZipArchiveMode . Read ) )
139+ await zipArchive . ExtractToDirectoryAsync ( destination , true , cancellationToken ) ;
140+ }
141+ finally
142+ {
143+ File . Delete ( file ) ;
144+ }
145+ }
146+
147+ public static async Task UnzipPackageAsync ( string pluginName , string file , string destination , bool isSRT , CancellationToken cancellationToken )
149148 {
150149 // Ensure the SRT is closed before we start trying to replace files.
151150 KillSRT ( ) ;
152151
153152 if ( ! isSRT )
154153 {
155- DirectoryInfo pluginDirectory = new DirectoryInfo ( Path . Combine ( destination , pluginName ) ) ;
154+ var pluginDirectory = new DirectoryInfo ( Path . Combine ( destination , pluginName ) ) ;
156155 if ( pluginDirectory . Exists )
157156 {
158157 // Save plugin config file.
159- FileInfo configFile = pluginDirectory . EnumerateFiles ( string . Format ( "{0}.cfg" , pluginName ) , SearchOption . TopDirectoryOnly ) . FirstOrDefault ( ) ;
158+ var configFile = pluginDirectory . EnumerateFiles ( string . Format ( "{0}.cfg" , pluginName ) , SearchOption . TopDirectoryOnly ) . FirstOrDefault ( ) ;
160159
161160 // If the config file exists, copy it to the temp folder until after the delete and unzip completes.
162161 if ( configFile != default && configFile . Exists )
@@ -167,8 +166,15 @@ public static void UnzipPackage(string pluginName, string file, string destinati
167166 }
168167 }
169168
170- ZipFile . ExtractToDirectory ( file , destination , true ) ;
171- File . Delete ( file ) ;
169+ try
170+ {
171+ await using ( var zipArchive = ZipFile . Open ( file , ZipArchiveMode . Read ) )
172+ await zipArchive . ExtractToDirectoryAsync ( destination , true , cancellationToken ) ;
173+ }
174+ finally
175+ {
176+ File . Delete ( file ) ;
177+ }
172178
173179 if ( ! isSRT )
174180 {
@@ -179,83 +185,8 @@ public static void UnzipPackage(string pluginName, string file, string destinati
179185 File . Delete ( Path . Combine ( TempFolderPath , string . Format ( "{0}.cfg" , pluginName ) ) ) ;
180186 }
181187 }
182-
183-
184- //if (!Directory.Exists(file))
185- //{
186- // ZipFile.ExtractToDirectory(file, TempFolderPath);
187- //}
188-
189- //if (isSRT)
190- //{
191- // File.Delete(file);
192- // UpdateSRTPackage(TempFolderPath, destination);
193- //}
194- //else
195- //{
196- // var dirs = Directory.GetDirectories(TempFolderPath);
197- // UpdatePackage(dirs[0], destination);
198- //}
199188 }
200189
201- //public static void UpdateSRTPackage(string source, string destination)
202- //{
203- // var filesSource = Directory.GetFiles(source);
204- // CopyTmpFiles(filesSource, destination);
205- // var directoriesSource = Directory.GetDirectories(source);
206- // if (directoriesSource.Length > 0) CopyTmpFolders(directoriesSource, destination, source);
207- // DeleteTmpFiles();
208- //}
209-
210- //public static void UpdatePackage(string source, string destination)
211- //{
212- // var dest = Path.Combine(destination, Path.GetFileName(source));
213- // if (!Directory.Exists(dest)) Directory.CreateDirectory(dest);
214- // var filesSource = Directory.GetFiles(source);
215- // CopyTmpFiles(filesSource, dest);
216- // var directoriesSource = Directory.GetDirectories(source);
217- // if (directoriesSource.Length > 0) CopyTmpFolders(directoriesSource, dest, source);
218- // DeleteTmpFiles();
219- //}
220-
221- //private static void CopyTmpFolders(string[] folders, string destination, string source)
222- //{
223- // foreach (string folder in folders)
224- // {
225- // var folderPath = Path.Combine(destination, Path.GetFileName(folder));
226- // if (!Directory.Exists(folderPath)) Directory.CreateDirectory(folderPath);
227- // var files = Directory.GetFiles(folder);
228- // CopyTmpFiles(files, folderPath);
229- // }
230- // DeleteDirectories(source);
231- //}
232-
233- //private static void CopyTmpFiles(string[] files, string destination)
234- //{
235- // var i = 1;
236- // foreach (string file in files)
237- // {
238- // File.Copy(file, Path.Combine(destination, Path.GetFileName(file)), true);
239- // File.Delete(file);
240- // i++;
241- // }
242- //}
243-
244- //private static void DeleteTmpFiles()
245- //{
246- // DeleteDirectories(TempFolderPath);
247- // DeleteFiles(TempFolderPath);
248- //}
249-
250- //public static void DeleteDirectories(string source)
251- //{
252- // var dirs = Directory.GetDirectories(source);
253- // foreach (string directory in dirs)
254- // {
255- // Directory.Delete(directory, true);
256- // }
257- //}
258-
259190 public static void UninstallExtension ( string source , string extensionName )
260191 {
261192 var dirs = Directory . GetDirectories ( source ) ;
0 commit comments