|
| 1 | +namespace AngleSharp.Io.Dom |
| 2 | +{ |
| 3 | + using AngleSharp.Dom; |
| 4 | + using AngleSharp.Html; |
| 5 | + using AngleSharp.Html.Dom; |
| 6 | + using System; |
| 7 | + using System.IO; |
| 8 | + using System.Threading; |
| 9 | + using System.Threading.Tasks; |
| 10 | + |
| 11 | + /// <summary> |
| 12 | + /// Extensions for DOM elements. |
| 13 | + /// </summary> |
| 14 | + public static class ElementExtensions |
| 15 | + { |
| 16 | + /// <summary> |
| 17 | + /// Appends a file to the input element. |
| 18 | + /// Requires the input element to be of type "file". |
| 19 | + /// </summary> |
| 20 | + /// <typeparam name="TElement">The type of element.</typeparam> |
| 21 | + /// <param name="input">The input to append to.</param> |
| 22 | + /// <param name="file">The file to append.</param> |
| 23 | + /// <returns>The input itself for chaining.</returns> |
| 24 | + public static TElement AppendFile<TElement>(this TElement input, InputFile file) |
| 25 | + where TElement : class, IHtmlInputElement |
| 26 | + { |
| 27 | + input = input ?? throw new ArgumentNullException(nameof(input)); |
| 28 | + |
| 29 | + if (input.Type == InputTypeNames.File) |
| 30 | + { |
| 31 | + input.Files.Add(file ?? throw new ArgumentNullException(nameof(file))); |
| 32 | + } |
| 33 | + |
| 34 | + return input; |
| 35 | + } |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// Appends a file to the input element. |
| 39 | + /// Requires the input element to be of type "file". |
| 40 | + /// </summary> |
| 41 | + /// <typeparam name="TElement">The type of element.</typeparam> |
| 42 | + /// <param name="input">The input to append to.</param> |
| 43 | + /// <param name="filePath">The path to the file, which should be appended.</param> |
| 44 | + /// <returns>The input itself for chaining.</returns> |
| 45 | + public static TElement AppendFile<TElement>(this TElement input, String filePath) |
| 46 | + where TElement : class, IHtmlInputElement |
| 47 | + { |
| 48 | + filePath = filePath ?? throw new ArgumentNullException(nameof(filePath)); |
| 49 | + var name = Path.GetFileName(filePath); |
| 50 | + var ext = Path.GetExtension(filePath); |
| 51 | + var type = MimeTypeNames.FromExtension(ext); |
| 52 | + var stream = File.OpenRead(filePath); |
| 53 | + var modified = File.GetLastWriteTimeUtc(filePath); |
| 54 | + var file = new InputFile(name, type, stream, modified); |
| 55 | + return input.AppendFile(file); |
| 56 | + } |
| 57 | + |
| 58 | + /// <summary> |
| 59 | + /// Appends a file to the input element. |
| 60 | + /// Requires the input element to be of type "file". |
| 61 | + /// </summary> |
| 62 | + /// <typeparam name="TElement">The type of element.</typeparam> |
| 63 | + /// <param name="input">The input to append to.</param> |
| 64 | + /// <param name="fileName">The name to the file, which should be appended.</param> |
| 65 | + /// <param name="content">The content to the file, which should be appended.</param> |
| 66 | + /// <param name="mimeType"> |
| 67 | + /// The MIME type of the file, which should be appended. |
| 68 | + /// If not given the default value is maps to an unknown binary (octet stream). |
| 69 | + /// </param> |
| 70 | + /// <returns>The input itself for chaining.</returns> |
| 71 | + public static TElement AppendFile<TElement>(this TElement input, String fileName, Stream content, String mimeType = null) |
| 72 | + where TElement : class, IHtmlInputElement |
| 73 | + { |
| 74 | + fileName = fileName ?? throw new ArgumentNullException(nameof(fileName)); |
| 75 | + content = content ?? throw new ArgumentNullException(nameof(content)); |
| 76 | + var type = mimeType ?? MimeTypeNames.Binary; |
| 77 | + var file = new InputFile(fileName, type, content); |
| 78 | + return input.AppendFile(file); |
| 79 | + } |
| 80 | + |
| 81 | + /// <summary> |
| 82 | + /// Downloads the content from to the hyper reference given by the provided |
| 83 | + /// element. |
| 84 | + /// </summary> |
| 85 | + /// <typeparam name="TElement">The type of element.</typeparam> |
| 86 | + /// <param name="element">The element referencing the link to follow.</param> |
| 87 | + /// <param name="cancellationToken">The token to cancel the download.</param> |
| 88 | + /// <returns>The task eventually resulting in the response.</returns> |
| 89 | + public static Task<IResponse> DownloadAsync<TElement>(this TElement element, CancellationToken cancellationToken = default(CancellationToken)) |
| 90 | + where TElement : class, IUrlUtilities, IElement |
| 91 | + { |
| 92 | + var context = element?.Owner.Context ?? throw new InvalidOperationException("The element needs to be inside a browsing context."); |
| 93 | + var loader = context.GetService<IDocumentLoader>() ?? throw new InvalidOperationException("A document loader is required. Check your configuration."); |
| 94 | + var download = loader.FetchAsync(new DocumentRequest(new Url(element.Href))); |
| 95 | + cancellationToken.Register(download.Cancel); |
| 96 | + return download.Task; |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments