private static void ValidateUpdateContentArgs(IUpdateContentArgs args) { // web content directory should exist if (!Directory.Exists(args.ContentPath)) { var message = string.Format(Localization.ValidateArgsSourceDirectory, args.ContentPath); throw new IOException(message); } // main web content file should exist var contentPath = Path.Combine(args.ContentPath, Constants.ContentFilename); if (!File.Exists(contentPath)) { var message = string.Format(Localization.ValidateArgsContentFile, contentPath); throw new IOException(message); } }
// adds web content to screensaver private static void InjectContent(IUpdateContentArgs args) { string tmpPath; try { tmpPath = Path.GetTempFileName(); } catch (Exception ex) { var message = string.Format(Localization.InjectContentGetTemp, ex.Message); throw new IOException(message); } try { // create zip from provided web content via temporary path File.Delete(tmpPath); ZipFile.CreateFromDirectory(args.ContentPath, tmpPath, CompressionLevel.Optimal, false); // remove default caption and icon files from zip using (var zip = ZipFile.Open(tmpPath, ZipArchiveMode.Update)) { var caption = zip.GetEntry(Constants.DefaultCaptionFilename); if (caption != null) caption.Delete(); var icon = zip.GetEntry(Constants.DefaultIconFilename); if (icon != null) icon.Delete(); } } catch (Exception ex) { var message = string.Format(Localization.InjectContentCreateArchive, tmpPath, args.ContentPath, ex.Message); throw new IOException(message, ex); } byte[] content; try { // read temporary zip content = File.ReadAllBytes(tmpPath); } catch (Exception ex) { try { File.Delete(tmpPath); } catch { } var message = string.Format(Localization.InjectContentReadArchive, tmpPath, ex.Message); throw new IOException(message, ex); } try { // remove temporary zip File.Delete(tmpPath); } catch (Exception ex) { var message = string.Format(Localization.InjectContentDeleteArchive, tmpPath, ex.Message); throw new IOException(message, ex); } // add read zip to screensaver InjectResource(args.OutputPath, Constants.ContentResourceType, Constants.ContentResourceName, content); }
// adds web content to screensaver private static void InjectContent(IUpdateContentArgs args) { string tmpPath; try { tmpPath = Path.GetTempFileName(); } catch (Exception ex) { var message = string.Format(Localization.InjectContentGetTemp, ex.Message); throw new IOException(message); } try { // create zip from provided web content via temporary path File.Delete(tmpPath); ZipFile.CreateFromDirectory(args.ContentPath, tmpPath, CompressionLevel.Optimal, false); // remove default caption and icon files from zip using (var zip = ZipFile.Open(tmpPath, ZipArchiveMode.Update)) { var caption = zip.GetEntry(Constants.DefaultCaptionFilename); if (caption != null) { caption.Delete(); } var icon = zip.GetEntry(Constants.DefaultIconFilename); if (icon != null) { icon.Delete(); } } } catch (Exception ex) { var message = string.Format(Localization.InjectContentCreateArchive, tmpPath, args.ContentPath, ex.Message); throw new IOException(message, ex); } byte[] content; try { // read temporary zip content = File.ReadAllBytes(tmpPath); } catch (Exception ex) { try { File.Delete(tmpPath); } catch { } var message = string.Format(Localization.InjectContentReadArchive, tmpPath, ex.Message); throw new IOException(message, ex); } try { // remove temporary zip File.Delete(tmpPath); } catch (Exception ex) { var message = string.Format(Localization.InjectContentDeleteArchive, tmpPath, ex.Message); throw new IOException(message, ex); } // add read zip to screensaver InjectResource(args.OutputPath, Constants.ContentResourceType, Constants.ContentResourceName, content); }