public static async Task <string> AddVaultApp(long appId, string version, string url, bool forceUpdate) { Impersonator imp = null; var folder = GetVaultAppFolder(); if (NeedImpersonation(folder)) { imp = GetImpersonator(); } try { if (!Directory.Exists(folder)) { Directory.CreateDirectory(folder); } var destPath = GetAppPath(appId, version); //Path.Combine(folder, appId + "-" + version + ".zip"); if (File.Exists(destPath) && !forceUpdate) { return(destPath); } //if (!Directory.Exists(destPath)) Directory.CreateDirectory(destPath); return(await DownloadApp(url, destPath)); } finally { if (imp != null) { imp.Dispose(); } } }
internal static async Task <bool> DownloadToPath(string url, string filePath) { Log.InfoFormat("压缩文件路径({0}):{1}", url, filePath); var content = await DownloadToStream(url); if (content == null) { return(false); } Impersonator imp = null; if (NeedImpersonation(filePath)) { imp = GetImpersonator(); } try { using (var fs = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None)) { content.CopyTo(fs); //await content.CopyToAsync(fs); fs.Flush(); fs.Close(); } } finally { if (imp != null) { imp.Dispose(); } } return(true); }
public static async Task <string> AddTemplate(long templateId, string version, string url, bool forceUpdate) { Log.Info("开始添加模板: "); var tempFolder = Path.Combine(RootPath, TemplateFolder); Impersonator imp = null; if (NeedImpersonation(tempFolder)) { imp = GetImpersonator(); } try { try { if (!Directory.Exists(tempFolder)) { Directory.CreateDirectory(tempFolder); } } catch (Exception ex) { Log.Error("创建文件夹失败:" + tempFolder, ex); return(String.Empty); } var destPath = Path.Combine(tempFolder, templateId.ToString(), version); try { if (!Directory.Exists(destPath)) { Directory.CreateDirectory(destPath); } else { //含有Index.xml文件 if (Directory.GetFiles(destPath, "*.xml").Length > 0 && !forceUpdate) { return(destPath); } } } catch (Exception ex) { Log.Error(String.Format("创建文件夹({0})失败:{1}", destPath, ex.Message), ex); return(String.Empty); } //Log.Info("TemplatePath: " + destPath); var zipPath = Path.Combine(destPath, templateId + "_" + version + ".zip"); if (!File.Exists(zipPath)) { var ok = await DownloadToPath(url, zipPath); if (!ok) { Log.Error("下载模版失败:" + url); return(String.Empty); } } try { ZipUtils.ExtractTemplate(zipPath, destPath); try { File.Delete(zipPath); } catch (Exception) { } } catch (Exception ex) { var ex0 = ex; if (ex.InnerException != null) { ex0 = ex.InnerException; } Log.Error("ZIP解压失败:" + zipPath + " # Error: " + ex0.Message, ex0); return(String.Empty); } return(destPath); } finally { if (imp != null) { imp.Dispose(); } } }