Search code examples
c#uwponedrive

Custom Library in UWP


i am planning to develop an ebook reader app on UWP. But i have a little strugle implementing the storage concept. So i want to archive the following.

  • The user can setup a custom folder as ebook library. This folder will contain all ebooks (So far no problem)
  • Like in Groove i want the ability to "stream" a ebook (downloading it only temporary), or download it permanently.

For the mobile platform this works fine, but when it comes to Desktop there is a problem:

Typically the user will have a synced ondrive folder. So the folder may be permanent avaible. Using the ondrive api, the App will have no knowledge of this folder and cant use it. Using the same caching algorithm would lead to duplicates.

The workaround i came along with is, to ask the user on Desktop to select the folder from the file tree manually and dont store anything local. But this could lead to a problem, if the user decided not to sync the ebook library.

So is there a "smother" way to archive that?


Solution

  • The toolkit I referred to in my comment has now been released.

    See this blog post for a summary:

    OneDrive service. Roam user files through the new OneDrive service with a simplified API model (similar to StorageFolder). The new service makes it easy to authenticate OneDrive users and more in the same consistent way you have come to expect from the toolkit services.

    Or you can go directly to the documentation to see how you can leverage the functionality in your UWP app.

    At its simplest, you can access the root of the current user's OneDrive using:

    var folder = await OneDriveService.Instance.RootFolderAsync();
    

    An example of downloading the contents of a file would be:

    var remoteFile=await parentFolder.GetFile("File.docx"); 
    
    using (var remoteStream = await remoteFile.OpenAsync())
     {
         byte[] buffer = new byte[remoteStream.Size];
         var localBuffer = await remoteStream.ReadAsync(buffer.AsBuffer(), (uint)remoteStream.Size, InputStreamOptions.ReadAhead);
         var localFolder = ApplicationData.Current.LocalFolder;
         var myLocalFile = await localFolder.CreateFileAsync($"{oneDriveFile.Name}", CreationCollisionOption.GenerateUniqueName);
         using (var localStream = await myLocalFile.OpenAsync(FileAccessMode.ReadWrite))
         {
             await localStream.WriteAsync(localBuffer);
             await localStream.FlushAsync();
         }
     }