private static async Task Main(string[] args) { if (!ParseParameters(args)) { Logger.WriteWarning(ContentId, 0, "Aspose preview generator process arguments are not correct."); return; } if (!await InitializeAsync()) { return; } try { //TODO: use the cancellation token to stop image generation manually var tokenSource = new CancellationTokenSource(); await GenerateImagesAsync(tokenSource.Token); } catch (Exception ex) { if (AsposeTools.ContentNotFound(ex)) { return; } Logger.WriteError(ContentId, 0, ex: ex, startIndex: StartIndex, version: Version); await SetPreviewStatusAsync(-3); // PreviewStatus.Error } }
private static Task <int> UploadImageAsync(Stream imageStream, int previewsFolderId, string imageName, CancellationToken cancellationToken) { return(Retrier.RetryAsync(REQUEST_RETRY_COUNT, 50, async() => { imageStream.Seek(0, SeekOrigin.Begin); var image = await Content.UploadAsync(previewsFolderId, imageName, imageStream, "PreviewImage").ConfigureAwait(false); return image.Id; }, (result, count, ex) => { if (ex == null) { return true; } if (count == 1 || AsposeTools.ContentNotFound(ex)) { throw ex; } cancellationToken.ThrowIfCancellationRequested(); return false; })); }
private static async Task <Stream> GetBinaryAsync() { var documentStream = new MemoryStream(); try { // download the whole file from the server await RESTCaller.GetStreamResponseAsync(ContentId, Version, async response => { if (response == null) { throw new ClientException($"Content {ContentId} {Version} not found.", HttpStatusCode.NotFound); } await response.Content.CopyToAsync(documentStream).ConfigureAwait(false); documentStream.Seek(0, SeekOrigin.Begin); }, CancellationToken.None).ConfigureAwait(false); } catch (ClientException ex) { if (AsposeTools.ContentNotFound(ex)) { return(null); } Logger.WriteError(ContentId, 0, "Error during remote file access.", ex, StartIndex, Version); // We need to throw the error further to let the main catch block // log the error and set the preview status to 'Error'. throw; } return(documentStream); }
private static Task <dynamic> GetResponseJsonAsync(ODataRequest request, HttpMethod method = null, object body = null) { return(Retrier.RetryAsync(REQUEST_RETRY_COUNT, 50, async() => await RESTCaller.GetResponseJsonAsync(request, method: method ?? HttpMethod.Get, postData: body), (result, count, ex) => { if (ex == null) { return true; } // last try: throw the exception if (count == 1 || AsposeTools.ContentNotFound(ex)) { throw ex; } // failed, but give them a new chance return false; })); }
private static async Task SaveImageStreamAsync(Stream imageStream, string previewName, int page, int previewsFolderId, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); imageStream.Seek(0, SeekOrigin.Begin); try { var imageId = await UploadImageAsync(imageStream, previewsFolderId, previewName, cancellationToken); cancellationToken.ThrowIfCancellationRequested(); // set initial preview image properties (CreatedBy, Index, etc.) await PostAsync(imageId, "SetInitialPreviewProperties"); } catch (ClientException ex) { // a 404 must be handled by the caller if (AsposeTools.ContentNotFound(ex)) { throw; } // node is out of date is not an error if (ex.ErrorData?.ExceptionType?.Contains("NodeIsOutOfDateException") ?? false) { return; } Logger.WriteError(ContentId, page, ex.Message, ex, StartIndex, Version); // in case of status 500, we still have to terminate the process after logging the error if (AsposeTools.IsTerminatorError(ex)) { throw; } } }