private async Task ProcessBookAsync(Document document, long chatId, BookFormat desiredFormat) { string fileName = document.FileName; Message loadingMessage = await SendLoadingMessageAsync(); using var tempFiles = new TempFiles(); try { Book originalBook = await DownloadOriginalBookAsync(document); tempFiles.Add(originalBook.FilePath); Book convertedBook = await ConvertBookAsync(desiredFormat, originalBook); tempFiles.Add(convertedBook.FilePath); await DeleteLoadingMessageAsync(loadingMessage); if (convertedBook.Equals(originalBook)) { await this.bot.SendTextMessageAsync(chatId, $"Your book *{originalBook.Title}* is already good!", ParseMode.Markdown); } else { await using Stream output = convertedBook.Content(); await this.bot.SendDocumentAsync(chatId, new InputOnlineFile(output, $"{convertedBook.Title}{convertedBook.Format}"), $"Here you go! Your *{convertedBook.Title}* is ready. Enjoy.", ParseMode.Markdown); } } catch (Exception exception) { Log.Error(exception, $"Exception happened when converting {document.FileId}"); await DeleteLoadingMessageAsync(loadingMessage); await this.bot.SendTextMessageAsync(chatId, $"Sorry, something went wrong with *{fileName}*. Try sending the book again.", ParseMode.Markdown); } Task <Message> SendLoadingMessageAsync() { return(this.bot.SendTextMessageAsync(chatId, $"Wait a bit. My dwarfs are working on *{fileName}*", ParseMode.Markdown)); } Task DeleteLoadingMessageAsync(Message message) { return(message != null ? this.bot.DeleteMessageAsync(chatId, message.MessageId) : Task.CompletedTask); } }
/// <summary> /// Gets the card media files. /// </summary> /// <param name="card">The card.</param> /// <param name="mediafield">The mediafield.</param> /// <returns>The found media files.</returns> /// <remarks>Documented by Dev02, 2008-03-30</remarks> private static IList <MediaFieldFile> GetCardMediaFiles(ICard card, List <MediaField> mediafields) { IList <MediaFieldFile> foundfiles = new List <MediaFieldFile>(); foreach (MediaField mediafield in mediafields) { if (mediafield.Type == MediaField.TypeEnum.AudioField) { IList <IMedia> medialist = null; switch (mediafield.Side) { case MediaField.SideEnum.Question: medialist = card.QuestionMedia; break; case MediaField.SideEnum.Answer: medialist = card.AnswerMedia; break; } if (medialist != null) { foreach (IMedia media in medialist) { if (media.MediaType == EMedia.Audio && ((IAudio)media).Example == mediafield.Example) { string sourceFile = GetTempFile(media.Extension); //write to temp file using (Stream mediaStream = media.Stream) { using (Stream output = new FileStream(sourceFile, FileMode.Create)) { byte[] buffer = new byte[32 * 1024]; int read; while ((read = mediaStream.Read(buffer, 0, buffer.Length)) > 0) { output.Write(buffer, 0, read); } } } FileInfo mediafile = new FileInfo(sourceFile); if (mediafile.Exists) { TempFiles.Add(mediafile); foundfiles.Add(new MediaFieldFile(mediafield, mediafile)); } } } } } else if (mediafield.Type == MediaField.TypeEnum.Silence) { foundfiles.Add(new MediaFieldFile(mediafield)); } } return(foundfiles); }
public UpdateDownloaderMaliciousExistingTempFilesTest() { TempFiles.Add("7324c19ad06ea51117d7ffdd01f7d030ac03651f6ea57472de1765b3dfa3a9e0", new MockTempFile { Exists = true, DataStream = new MemoryStream(Encoding.ASCII.GetBytes("NORA??")) }); }
public string GetTempTmxFile() { string path = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + ".tmx"); TempFiles.Add(path); return(path); }
public override string BuildArguments() { string playlistFileName = Path.Combine(Path.GetTempPath(), Path.GetFileName(InputFile) + $"-playlist.txt"); File.WriteAllLines(playlistFileName, ClipFiles.Select(se => $"file '{se.Replace("'", "'\\''")}'")); //concat requires special escaping TempFiles.Add(playlistFileName); return($"-f concat -safe 0 -i \"{playlistFileName}\" -c copy \"{OutputFile}\""); }
private void ExtractISO(PbpDiscEntry disc, string path, ExtractOptions extractInfo, CancellationToken cancellationToken) { try { disc.ProgressEvent += ProgressEvent; if (!ContinueIfFileExists(extractInfo, path)) { return; } Notify?.Invoke(PopstationEventEnum.Info, $"Writing {path}..."); Notify?.Invoke(PopstationEventEnum.GetIsoSize, disc.IsoSize); Notify?.Invoke(PopstationEventEnum.ExtractStart, disc.Index); var cueFilename = Path.GetFileNameWithoutExtension(path) + ".cue"; var dirPath = Path.GetDirectoryName(path); var cuePath = Path.Combine(dirPath, cueFilename); TempFiles.Add(path); TempFiles.Add(cuePath); using (var isoStream = new FileStream(path, FileMode.Create, FileAccess.Write)) { disc.CopyTo(isoStream, cancellationToken); } if (cancellationToken.IsCancellationRequested) { return; } TempFiles.Remove(path); if (!extractInfo.CreateCuesheet) { return; } var cueFile = TOCtoCUE(disc.TOC, Path.GetFileName(path)); CueFileWriter.Write(cueFile, cuePath); TempFiles.Remove(cuePath); Notify?.Invoke(PopstationEventEnum.ExtractComplete, null); } finally { disc.ProgressEvent -= ProgressEvent; } }
internal override void Initialize() { try { #pragma warning disable CS0436 // Type conflicts with imported type //SqlServerTypes.Utilities.LoadNativeAssemblies(AppDomain.CurrentDomain.BaseDirectory); #pragma warning restore CS0436 // Type conflicts with imported type OnBeforeInitialize(); var od = OriginalSqlDatabase <TDbContext> .GetInstance(() => Version); TempFiles.Add(od.DbFile); TempFiles.Add(od.LogFile); DBName = od.DBName; ConnectionString = (new SqlConnectionStringBuilder() { DataSource = "(localdb)\\MSSQLLocalDB", //sqlCnstr.AttachDBFilename = t; InitialCatalog = od.DBName, IntegratedSecurity = true, ApplicationName = "EntityFramework" }).ToString(); } catch (Exception ex) { WriteLine(ex.ToString()); throw; } }
public virtual Snapshot Save(string Culture) { var oldculture = Thread.CurrentThread.CurrentCulture; var olduiculture = Thread.CurrentThread.CurrentUICulture; var ext = Path.GetExtension(Filename); string filename = LocalFilename; if (!string.IsNullOrEmpty(Culture)) { filename = Path.GetFileNameWithoutExtension(filename) + "." + Culture + ext; var info = new CultureInfo(Culture); Thread.CurrentThread.CurrentCulture = info; Thread.CurrentThread.CurrentUICulture = info; } ext = ext.ToLower(); var sext = Path.GetExtension(Source); if (ext == sext && Scale == 1 && GetValue(WidthProperty) == null && GetValue(HeightProperty) == null && GetValue(TopProperty) == null && GetValue(BottomProperty) == null && GetValue(RightProperty) == null && GetValue(LeftProperty) == null) { if (Source != filename) { File.Copy(Source, filename); } return(this); } if (ext != ".pdf" && Scene.Element is HtmlSource) { Errors.Error("Html sources can only be converted to PDF.", "60", XElement); } if (Ghost || ext == ".eps" || ext == ".ps" || ext == ".pdf") { if (Scene.Element is HtmlSource) { SaveHtml(); } else { TempFiles.Add(XpsTempFile); SaveXpsPage(XpsTempFile); } } else if (ext == ".xps") { SaveXpsPage(filename); } else if (ext == ".xaml") { using (FileLock(filename)) { var xamlsave = false; if (Element == Scene.Element) { if (Scene.XamlPath != null) { DirectoryCopy(Scene.XamlPath, Path.GetDirectoryName(filename)); File.Move(Path.Combine(Path.GetDirectoryName(filename), Path.GetFileName(Scene.XamlFile)), filename); File.WriteAllText(filename, File.ReadAllText(filename).Replace(Scene.XamlPath + "\\", "")); } else if (Scene.XamlFile != null) { File.Copy(Scene.XamlFile, filename); } else if (Scene.Source.ToLower().EndsWith(".svg") || Scene.Source.ToLower().EndsWith(".svgz")) { SvgConvert.ConvertUtility.ConvertSvg(Compiler.MapPath(Scene.Source), filename); } else { xamlsave = true; } } else { xamlsave = true; } if (xamlsave) { var settings = new System.Xml.XmlWriterSettings() { CheckCharacters = true, CloseOutput = true, Encoding = Encoding.UTF8, Indent = true, IndentChars = " " }; using (var w = System.Xml.XmlWriter.Create(filename, settings)) { try { XamlWriter.Save(Element, w); } catch { } } } Errors.Message("Created {0} ({1} MB RAM used)", Path.GetFileName(Filename), System.Environment.WorkingSet / (1024 * 1024)); ImageCreated(); } } else { Bitmaps = Capture.GetBitmaps(this).ToList(); var encoder = CreateEncoder(filename, Quality, Dpi); if (Filmstrip) { encoder.Frames.Add(BitmapFrame.Create(MakeFilmstrip(Bitmaps, Dpi))); } else { foreach (var b in Bitmaps) { encoder.Frames.Add(BitmapFrame.Create(b)); } } using (FileLock(filename)) { encoder.Save(filename); if (encoder is PngBitmapEncoder) // strip gamma { using (var src = new MemoryStream()) { using (var srcf = new FileStream(filename, FileMode.Open, FileAccess.Read)) { srcf.CopyTo(src); src.Seek(0, SeekOrigin.Begin); } using (var dest = new FileStream(filename, FileMode.Create, FileAccess.Write)) { PngHelper.StripGAMA(src, dest); } } } } if (Loop != 1 && ext == ".gif") { string file = Path.GetFileName(filename); var exe = Compiler.BinPath("ImageMagick\\convert.exe"); var args = " -loop " + Loop; if (Pause != 0) { args += " -pause " + Pause; } args += " " + file + " " + file; var process = NewProcess(exe, args, Path.GetDirectoryName(filename)); //var filelock = FileLock(filename); var filename2 = file; var frames = Bitmaps.Count(); process.Exited += (sender, args2) => { try { Errors.Message("Created {0} ({1}{2} MB RAM used)", filename2, (frames != 1) ? frames.ToString() + " frames, " : "", System.Environment.WorkingSet / (1024 * 1024)); //filelock.Dispose(); ImageCreated(); } finally { ExitProcess(process); } }; process.Start(); } else { Errors.Message("Created {0} ({1}{2} MB RAM used)", Path.GetFileName(Filename), (Bitmaps.Count() != 1) ? Bitmaps.Count().ToString() + " frames, " : "", System.Environment.WorkingSet / (1024 * 1024)); ImageCreated(); } } Thread.CurrentThread.CurrentCulture = oldculture; Thread.CurrentThread.CurrentUICulture = olduiculture; return(this); }