public static FileInfo ConvertUrl(this ConvertApi convertApi, string url, string toFile, params ConvertApiBaseParam[] parameters)
        {
            var outputExtension = GetPlainExtension(toFile);
            var task            = convertApi.ConvertAsync("web", outputExtension, JoinParameters(new ConvertApiParam("url", url), parameters));

            return(task.WaitAndUnwrapException().Files[0].SaveFileAsync(toFile).WaitAndUnwrapException());
        }
Ejemplo n.º 2
0
        private static async Task <ConvertApiUpload> Upload(Stream fileStream, string fileName)
        {
            HttpResponseMessage responseMessage;

            using (var content = new StreamContent(fileStream))
            {
                content.Headers.ContentType        = new MediaTypeHeaderValue("application/octet-stream");
                content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
                {
                    FileNameStar = Path.GetFileName(fileName)
                };

                var url = new UriBuilder(ConvertApi.ApiBaseUri)
                {
                    Path = "/upload",
                };

                responseMessage = await ConvertApi.GetClient().PostAsync(url.Uri, ConvertApiConstants.UploadTimeoutInSeconds, content);
            }

            var result = await responseMessage.Content.ReadAsStringAsync();

            if (responseMessage.StatusCode != HttpStatusCode.OK)
            {
                throw new ConvertApiException(responseMessage.StatusCode, $"Unable to upload file. {responseMessage.ReasonPhrase}", result);
            }

            return(JsonConvert.DeserializeObject <ConvertApiUpload>(result));
        }
Ejemplo n.º 3
0
        public static void Convert(this ConvertApi convertApi, string fromFile, string toFile)
        {
            var toExt = Path.GetExtension(toFile).Replace(".", "");
            var task  = BindConvertApi(convertApi, fromFile, toExt);

            TaskResult(task).AsFileAsync(0, toFile).Wait();
        }
Ejemplo n.º 4
0
        private static Task <ConvertApiResponse> BindConvertApi(ConvertApi convertApi, string fromFile, string outputExtension)
        {
            var fromExt = Path.GetExtension(fromFile).Replace(".", "");
            var task    = convertApi.ConvertAsync(fromExt, outputExtension, new[] { new ConvertApiParam("File", File.OpenRead(fromFile)) });

            return(task);
        }
        public static IEnumerable <FileInfo> ConvertFile(this ConvertApi convertApi, string fromFile, string outputExtension, string outputDirectory, params ConvertApiBaseParam[] parameters)
        {
            var task          = BindFile(convertApi, fromFile, outputExtension, parameters);
            var parallelQuery = task.WaitAndUnwrapException().Files.AsParallel().WithDegreeOfParallelism(6)
                                .Select(s => s.SaveFileAsync(Path.Combine(outputDirectory, s.FileName)).WaitAndUnwrapException());

            var l = new List <FileInfo>();

            l.AddRange(parallelQuery);

            return(l);
        }
        /// <summary>
        /// Delete files from the ConvertAPI server, and if left, they automatically will be deleted after 3 hours.
        /// </summary>
        /// <param name="processedFile">Files to delete.</param>
        /// <returns>Returns deleted files count.</returns>
        public static async Task <int> DeleteFilesAsync(this IEnumerable <ConvertApiFiles> processedFile)
        {
            var httpClient = ConvertApi.GetClient().Client;
            var count      = 0;

            foreach (var file in processedFile)
            {
                var httpResponseMessage = await httpClient.DeleteAsync(file.Url);

                if (httpResponseMessage.StatusCode == HttpStatusCode.OK)
                {
                    count += 1;
                }
            }

            return(count);
        }
Ejemplo n.º 7
0
        private static async Task <ConvertApiUpload> Upload(Uri remoteFileUrl)
        {
            var url = new UriBuilder(ConvertApi.ApiBaseUri)
            {
                Path  = "/upload",
                Query = $"url={remoteFileUrl}"
            };

            var responseMessage = await ConvertApi.GetClient().PostAsync(url.Uri, ConvertApiConstants.UploadTimeoutInSeconds, null);

            var result = await responseMessage.Content.ReadAsStringAsync();

            if (responseMessage.StatusCode != HttpStatusCode.OK)
            {
                throw new ConvertApiException(responseMessage.StatusCode, $"Unable to upload file. {responseMessage.ReasonPhrase}", result);
            }

            return(JsonConvert.DeserializeObject <ConvertApiUpload>(result));
        }
        public static FileInfo ConvertRemoteFile(this ConvertApi convertApi, string fileUrl, string toFile, params ConvertApiBaseParam[] parameters)
        {
            var task = BindFile(convertApi, new Uri(fileUrl), GetPlainExtension(toFile), parameters);

            return(task.WaitAndUnwrapException().Files[0].SaveFileAsync(toFile).WaitAndUnwrapException());
        }
 private static Task <ConvertApiResponse> BindFile(ConvertApi convertApi, Uri fileUrl, string outputExtension, IEnumerable <ConvertApiBaseParam> parameters)
 {
     return(convertApi.ConvertAsync("*", outputExtension, JoinParameters(new ConvertApiFileParam(fileUrl), parameters)));
 }
 private static Task <ConvertApiResponse> BindFile(ConvertApi convertApi, string fromFile, string outputExtension, IEnumerable <ConvertApiBaseParam> parameters)
 {
     return(convertApi.ConvertAsync(GetPlainExtension(fromFile), outputExtension, JoinParameters(new ConvertApiFileParam(fromFile), parameters)));
 }
        private static async Task <Stream> AsStreamAsync(Uri url)
        {
            var httpResponseMessage = await ConvertApi.GetClient().GetAsync(url, ConvertApiConstants.DownloadTimeoutInSeconds);

            return(await httpResponseMessage.Content.ReadAsStreamAsync());
        }
Ejemplo n.º 12
0
        public static void Convert(this ConvertApi convertApi, string fromFile, string outputExtension, string outputDirectory)
        {
            var task = BindConvertApi(convertApi, fromFile, outputExtension);

            TaskResult(task).SaveFiles(outputDirectory);
        }