public async Task <bool> FullfillRequestAsync(PDFRemoteFileRequest request, bool raiseErrors = true)
        {
            if (!request.IsCompleted)
            {
                try
                {
                    if (Uri.IsWellFormedUriString(request.FilePath, UriKind.Absolute))
                    {
                        await this.FullfillUriRequestAsync(request);
                    }
                    else
                    {
                        this.FullfillFileRequest(request);
                    }
                }
                catch (Exception ex)
                {
                    request.CompleteRequest(false, ex);
                }

                if (request.IsCompleted == false)
                {
                    throw new InvalidOperationException("Could not complete the request for a remote file");
                }

                else if (request.IsSuccessful == false && raiseErrors)
                {
                    throw request.Error ?? new InvalidOperationException("The request for the '" + request.FilePath + "' could not be completed");
                }
            }
            return(request.IsCompleted);
        }
        /// <summary>
        /// Creates file stream and callsback the owner of the request, then completes.
        /// </summary>
        /// <param name="fileRequest"></param>
        /// <returns>The result of the callback to the owner</returns>
        protected virtual bool FullfillFileRequest(PDFRemoteFileRequest fileRequest)
        {
            using (var stream = File.OpenRead(fileRequest.FilePath))
            {
                var success = fileRequest.Callback(this._owner, fileRequest, stream);
                fileRequest.CompleteRequest(success);

                return(success);
            }
        }
        /// <summary>
        /// Creates file stream and callsback the owner of the request, then completes.
        /// </summary>
        /// <param name="fileRequest"></param>
        /// <returns>The result of the callback to the owner</returns>
        protected async virtual Task <bool> FullfillFileRequestAsync(PDFRemoteFileRequest fileRequest)
        {
            var content = await File.ReadAllBytesAsync(fileRequest.FilePath);

            using (var stream = new MemoryStream(content))
            {
                var success = fileRequest.Callback(this.Owner, fileRequest, stream);
                fileRequest.CompleteRequest(success);

                return(success);
            }
        }
        /// <summary>
        /// Creates an http client
        /// </summary>
        /// <param name="urlRequest"></param>
        /// <returns></returns>
        protected async virtual Task <bool> FullfillUriRequestAsync(PDFRemoteFileRequest urlRequest)
        {
            var client = this.GetHttpClient();

            using (var stream = await client.GetStreamAsync(urlRequest.FilePath))
            {
                var success = urlRequest.Callback(this.Owner, urlRequest, stream);
                urlRequest.CompleteRequest(success);

                return(success);
            }
        }
        /// <summary>
        /// Creates an http client
        /// </summary>
        /// <param name="urlRequest"></param>
        /// <returns></returns>
        protected virtual bool FullfillUriRequest(PDFRemoteFileRequest urlRequest)
        {
            var client = this.GetHttpClient();

            using (var stream = client.GetStreamAsync(urlRequest.FilePath).Result)
            {
                var success = urlRequest.Callback(this._owner, urlRequest, stream);
                urlRequest.CompleteRequest(success);

                return(success);
            }
        }
Beispiel #6
0
 public RemoteFileRequestEventArgs(PDFRemoteFileRequest request)
 {
     this.Request = request ?? throw new ArgumentNullException(nameof(request));
 }
 public virtual bool AddRequest(PDFRemoteFileRequest request)
 {
     this.Requests.Add(request ?? throw new ArgumentNullException(nameof(request)));
     return(true);
 }