Skip to content
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

Access single file using BclStorageFolder #17276

Open
dmirmilshteyn opened this issue Oct 14, 2024 · 7 comments · May be fixed by #17771
Open

Access single file using BclStorageFolder #17276

dmirmilshteyn opened this issue Oct 14, 2024 · 7 comments · May be fixed by #17771
Labels
enhancement feature help-wanted A contribution from the community would be most welcome.

Comments

@dmirmilshteyn
Copy link

Is your feature request related to a problem? Please describe.

There doesn't seem to be a way to access a single item (file/folder) using the storage folder API. I realize I can find that file using GetItems, but in a folder with many files, it would need to enumerate each one before finding the correct file. This has performance problems that only grow with the number of files in a folder.

Describe the solution you'd like

I already have the file name I'm looking for, so an addition to the storage folder api that allows me to access a single item with just the name. Something along the lines of Task<IStorageItem> GetItem(string name) which does not enumerate through each item in the underlying directory.

Describe alternatives you've considered

There's a workaround where I can access an individual file using IStorageProvider.TryGetFileFromPathAsync, but I need to manually build a full path and use the storage provider directly instead of the folder. It's also likely not as portable, so not as ideal as having this built-in.

Additional context

No response

@maxkatz6
Copy link
Member

maxkatz6 commented Oct 14, 2024

Keeping it consistent with UWP, where we initially got these APIs: https://learn.microsoft.com/en-us/uwp/api/windows.storage.storagefolder.getitemasync?view=winrt-26100

interface IStorageFolder
{
     Task<IStorageItem> GetItemAsync(string name);
}

@maxkatz6 maxkatz6 added help-wanted A contribution from the community would be most welcome. feature labels Oct 14, 2024
@timunie
Copy link
Contributor

timunie commented Oct 15, 2024

@dmirmilshteyn if you want to add this functionality, a PR is welcome 👐

@nickodei
Copy link
Contributor

nickodei commented Dec 9, 2024

I would be down to implement this but there are some open questions I have because it needs to be implemented for each targeted platform.

For Desktop its simple and the name can even be a relative path like mentioned in the UWP docs.
But for Mobile and Browser it gets more difficult (especially the relative path thing).
So should it work with relative paths? e.g:

Android uses there AndroidUri and you can't just get a File by the Filename (at least what I saw) without iteration over the directory because it abstracts the Filesystem away (items could come for a server, etc). This nullifies the performance gains expected from this issue.

The Browser can give you the Item by name getFileHandle or getDirectoryHandle but I dont know if it can handle relative paths.

I didnt look into IOS.

@maxkatz6
Copy link
Member

On browser this new API should map to:
FileSystemDirectoryHandle.getDirectoryHandle()
FileSystemDirectoryHandle.ƒgetFileHandle()

Which gives an idea, that we should consider having two different APIs instead of one shared:

interface IStorageFolder
{
     Task<IStorageFolder> GetFolderAsync(string name);
     Task<IStorageFile> GetFileAsync(string name);
}

@maxkatz6
Copy link
Member

On iOS I don't see any dedicated FileManager API. But typical FileInfo C# apis should work there too.

On Android, I am not an expert, but seems like the same Query API, as we use for GetItemsAsync(), is recommended there too.

@maxkatz6
Copy link
Member

So should it work with relative paths? e.g:

This proposal is specifically about retrieving single file/folder from the known folder.

@nickodei
Copy link
Contributor

Thank you. I will make an initial implementation with "two different APIs" approach.

@nickodei nickodei linked a pull request Dec 14, 2024 that will close this issue
7 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement feature help-wanted A contribution from the community would be most welcome.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants