public static void SavePreviewAndThumbnail(Stream imageStream, int page, int previewsFolderId) { // save main preview image SaveImageStream(imageStream, GetPreviewNameFromPageNumber(page), page, Common.PREVIEW_WIDTH, Common.PREVIEW_HEIGHT, previewsFolderId); var progress = ((page - StartIndex) * 2 - 1) * 100 / MaxPreviewCount / 2; _generatingPreviewSubtask.Progress(progress, 100, progress + 10, 110); // save smaller image for thumbnail SaveImageStream(imageStream, GetThumbnailNameFromPageNumber(page), page, Common.THUMBNAIL_WIDTH, Common.THUMBNAIL_HEIGHT, previewsFolderId); progress = ((page - StartIndex) * 2) * 100 / MaxPreviewCount / 2; _generatingPreviewSubtask.Progress(progress, 100, progress + 10, 110); }
public static async Task SavePreviewAndThumbnailAsync(Stream imageStream, int page, int previewsFolderId, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); // save main preview image await SaveImageStreamAsync(imageStream, GetPreviewNameFromPageNumber(page), page, Common.PREVIEW_WIDTH, Common.PREVIEW_HEIGHT, previewsFolderId, cancellationToken); var progress = ((page - StartIndex) * 2 - 1) * 100 / MaxPreviewCount / 2; _generatingPreviewSubtask.Progress(progress, 100, progress + 10, 110); cancellationToken.ThrowIfCancellationRequested(); // save smaller image for thumbnail await SaveImageStreamAsync(imageStream, GetThumbnailNameFromPageNumber(page), page, Common.THUMBNAIL_WIDTH, Common.THUMBNAIL_HEIGHT, previewsFolderId, cancellationToken); progress = (page - StartIndex) * 2 * 100 / MaxPreviewCount / 2; _generatingPreviewSubtask.Progress(progress, 100, progress + 10, 110); }
static void Main(string[] args) { var subtask = new SnSubtask("Thinking hard", "Pretending to do something."); subtask.Start(); var count = new Random().Next(10, 15); for (var i = 0; i < count; i++) { Thread.Sleep(1000); subtask.Progress(i, count, i, count, $"Still pretending: {i}..."); } subtask.Finish("Cannot pretend anymore."); }
// ================================================================================================== Preview generation private static async Task GenerateImagesAsync(CancellationToken cancellationToken) { int previewsFolderId; string contentPath; var downloadingSubtask = new SnSubtask("Downloading", "Downloading file and other information"); downloadingSubtask.Start(); try { var fileInfo = await GetFileInfoAsync().ConfigureAwait(false); if (fileInfo == null) { Logger.WriteWarning(ContentId, 0, "Content not found."); downloadingSubtask.Finish(); return; } cancellationToken.ThrowIfCancellationRequested(); previewsFolderId = await GetPreviewsFolderIdAsync(); if (previewsFolderId < 1) { Logger.WriteWarning(ContentId, 0, "Previews folder not found, maybe the content is missing."); downloadingSubtask.Finish(); return; } downloadingSubtask.Progress(10, 100, 2, 110, "File info downloaded."); cancellationToken.ThrowIfCancellationRequested(); contentPath = fileInfo.Path; if (Config.ImageGeneration.CheckLicense) { CheckLicense(contentPath.Substring(contentPath.LastIndexOf('/') + 1)); } } catch (Exception ex) { Logger.WriteError(ContentId, message: "Error during initialization. The process will exit without generating images.", ex: ex, startIndex: StartIndex, version: Version); return; } #region For tests //if (contentPath.EndsWith("freeze.txt", StringComparison.OrdinalIgnoreCase)) // Freeze(); //if (contentPath.EndsWith("overflow.txt", StringComparison.OrdinalIgnoreCase)) // Overflow(); #endregion await using var docStream = await GetBinaryAsync(); if (docStream == null) { Logger.WriteWarning(ContentId, 0, $"Document not found; maybe the content or its version {Version} is missing."); downloadingSubtask.Finish(); return; } downloadingSubtask.Progress(100, 100, 10, 110, "File downloaded."); cancellationToken.ThrowIfCancellationRequested(); if (docStream.Length == 0) { await SetPreviewStatusAsync(0); // PreviewStatus.EmptyDocument downloadingSubtask.Finish(); return; } downloadingSubtask.Finish(); _generatingPreviewSubtask = new SnSubtask("Generating images"); _generatingPreviewSubtask.Start(); var extension = contentPath.Substring(contentPath.LastIndexOf('.')); await PreviewImageGenerator.GeneratePreviewAsync(extension, docStream, new PreviewGenerationContext( ContentId, previewsFolderId, StartIndex, MaxPreviewCount, Config.ImageGeneration.PreviewResolution, Version), cancellationToken).ConfigureAwait(false); _generatingPreviewSubtask.Finish(); }
// ================================================================================================== Preview generation protected static void GenerateImages() { int previewsFolderId; string contentPath; var downloadingSubtask = new SnSubtask("Downloading", "Downloading file and other information"); downloadingSubtask.Start(); try { previewsFolderId = GetPreviewsFolderId(); if (previewsFolderId < 1) { Logger.WriteWarning(ContentId, 0, "Previews folder not found, maybe the content is missing."); downloadingSubtask.Finish(); return; } var fileInfo = GetFileInfo(); if (fileInfo == null) { Logger.WriteWarning(ContentId, 0, "Content not found."); downloadingSubtask.Finish(); return; } downloadingSubtask.Progress(10, 100, 2, 110, "File info downloaded."); contentPath = fileInfo["Path"].Value <string>(); CheckLicense(contentPath.Substring(contentPath.LastIndexOf('/') + 1)); } catch (Exception ex) { Logger.WriteError(ContentId, message: "Error during initialization. The process will exit without generating images.", ex: ex, startIndex: StartIndex, version: Version); return; } //if (contentPath.EndsWith("freeze.txt", StringComparison.OrdinalIgnoreCase)) // Freeze(); //if (contentPath.EndsWith("overflow.txt", StringComparison.OrdinalIgnoreCase)) // Overflow(); using (var docStream = GetBinary()) { if (docStream == null) { Logger.WriteWarning(ContentId, 0, string.Format("Document not found; maybe the content or its version {0} is missing.", Version)); downloadingSubtask.Finish(); return; } downloadingSubtask.Progress(100, 100, 10, 110, "File downloaded."); if (docStream.Length == 0) { SetPreviewStatus(0); // PreviewStatus.EmptyDocument downloadingSubtask.Finish(); return; } downloadingSubtask.Finish(); _generatingPreviewSubtask = new SnSubtask("Generating images"); _generatingPreviewSubtask.Start(); var extension = contentPath.Substring(contentPath.LastIndexOf('.')); PreviewImageGenerator.GeneratePreview(extension, docStream, new PreviewGenerationContext( ContentId, previewsFolderId, StartIndex, MaxPreviewCount, Configuration.PreviewResolution, Version)); _generatingPreviewSubtask.Finish(); } }