-
Notifications
You must be signed in to change notification settings - Fork 850
Add LoadFromAsync and SaveToAsync helper methods to DataContent #7159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Copilot
wants to merge
16
commits into
main
Choose a base branch
from
copilot/add-helper-methods-data-content
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
983cc24
Initial plan
Copilot 145a994
Add LoadFromAsync and SaveToAsync methods to DataContent with MediaTy…
Copilot ffcb4c1
Fix collection expression syntax for consistency in tests
Copilot 2e3be2b
Address review feedback: move MediaTypeMap to polyfills, update DataC…
Copilot 3ec4410
Add C# 14 extension member polyfill for File.ReadAllBytesAsync
Copilot ecf0aa6
Remove name parameter from LoadFromAsync and use MemoryMarshal.TryGet…
Copilot ced14b3
Switch extension/MIME type mappings to use MediaTypeMap polyfill
Copilot 2866279
Change return types to ValueTask and add WriteAllBytesAsync polyfill
Copilot 1cceb2f
Add FilePolyfills to test project for net462 support
Copilot 031c3c2
Fix path traversal in SaveToAsync, simplify IngestionDocumentReader, …
Copilot cc9326c
Simplify extension inference by removing unnecessary null check
Copilot e86d9f8
Use DataContent.LoadFromAsync in MarkItDownMcpReader
Copilot 909b12b
Simplify MarkItDownMcpReader by passing mediaType directly to LoadFro…
Copilot 731acc6
Pre-initialize MemoryStream capacity when stream is seekable
Copilot bd2f87a
Address PR feedback, plus some cleanup and more tests
stephentoub faa7318
Add Microsoft.Extensions.AI.Abstractions reference to MarkItDown project
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| #if !NET9_0_OR_GREATER | ||
|
|
||
| #pragma warning disable CA1835 // Prefer the 'Memory'-based overloads for 'ReadAsync' and 'WriteAsync' | ||
|
|
||
| using System.Diagnostics.CodeAnalysis; | ||
| using System.Runtime.InteropServices; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace System.IO; | ||
|
|
||
| /// <summary> | ||
| /// Provides polyfill extension members for <see cref="File"/> for older frameworks. | ||
| /// </summary> | ||
| [ExcludeFromCodeCoverage] | ||
| internal static class FilePolyfills | ||
| { | ||
| extension(File) | ||
| { | ||
| #if !NET | ||
| /// <summary> | ||
| /// Asynchronously reads all bytes from a file. | ||
| /// </summary> | ||
| /// <param name="path">The file to read from.</param> | ||
| /// <param name="cancellationToken">The token to monitor for cancellation requests.</param> | ||
| /// <returns>A task that represents the asynchronous read operation, which wraps the byte array containing the contents of the file.</returns> | ||
| public static async Task<byte[]> ReadAllBytesAsync(string path, CancellationToken cancellationToken = default) | ||
| { | ||
| using var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 4096, useAsync: true); | ||
| byte[] data = new byte[stream.Length]; | ||
| int totalRead = 0; | ||
| while (totalRead < data.Length) | ||
| { | ||
| int read = await stream.ReadAsync(data, totalRead, data.Length - totalRead, cancellationToken).ConfigureAwait(false); | ||
| if (read == 0) | ||
| { | ||
| break; | ||
| } | ||
|
|
||
| totalRead += read; | ||
| } | ||
|
|
||
| return data; | ||
| } | ||
| #endif | ||
|
|
||
| /// <summary> | ||
| /// Asynchronously writes all bytes to a file. | ||
| /// </summary> | ||
| /// <param name="path">The file to write to.</param> | ||
| /// <param name="bytes">The bytes to write to the file.</param> | ||
| /// <param name="cancellationToken">The token to monitor for cancellation requests.</param> | ||
| /// <returns>A task that represents the asynchronous write operation.</returns> | ||
| public static async Task WriteAllBytesAsync(string path, ReadOnlyMemory<byte> bytes, CancellationToken cancellationToken = default) | ||
| { | ||
| // Try to avoid ToArray() if the data is backed by a byte[] with offset 0 and matching length | ||
| byte[] byteArray; | ||
| if (MemoryMarshal.TryGetArray(bytes, out ArraySegment<byte> segment) && | ||
| segment.Offset == 0 && | ||
| segment.Count == segment.Array!.Length) | ||
| { | ||
| byteArray = segment.Array; | ||
| } | ||
| else | ||
| { | ||
| byteArray = bytes.ToArray(); | ||
| } | ||
|
|
||
| #if NET | ||
| await File.WriteAllBytesAsync(path, byteArray, cancellationToken).ConfigureAwait(false); | ||
| #else | ||
| using var stream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None, bufferSize: 4096, useAsync: true); | ||
| await stream.WriteAsync(byteArray, 0, byteArray.Length, cancellationToken).ConfigureAwait(false); | ||
stephentoub marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| #endif | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #endif | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| # About FilePolyfills | ||
|
|
||
| This folder contains C# 14 extension member polyfills for `System.IO.File` methods | ||
| that are not available on older frameworks. | ||
|
|
||
| - `File.ReadAllBytesAsync` - Added in .NET Core 2.0, not available in .NET Framework 4.6.2 or .NET Standard 2.0 | ||
|
|
||
| The polyfill uses C# 14 extension members so the call site can use `File.ReadAllBytesAsync` naturally | ||
stephentoub marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| and it will use the real one on supported platforms and the polyfill elsewhere. | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.