private const string DefaultExtension = "txt"; // for files without one /// <summary> /// Asynchronously stores the specified Moodle file with the specified content. /// </summary> public async Task StoreFileAsync( MoodleFile moodleFile, byte[] content ) { var file = await GetFileAsync( moodleFile, true ); using ( var stream = await file.OpenStreamForWriteAsync() ) { stream.Write( content, 0, content.Length ); } }
/// <summary> /// Asynchronously downloads the specified Moodle file. /// </summary> public async Task<byte[]> DownloadAsync( MoodleFile file ) { var client = new HttpClient(); // not the PocketCampus HTTP client, the .NET one client.DefaultRequestHeaders.Add( SessionHeaderName, _serverSettings.Session ); var postParams = new Dictionary<string, string> { { ActionKey, ActionValue }, { FilePathKey, file.DownloadUrl } }; string downloadUrl = string.Format( DownloadUrlFormat, _serverSettings.Configuration.Protocol, _serverSettings.Configuration.Address, _serverSettings.Configuration.Port ); var response = await client.PostAsync( downloadUrl, new FormUrlEncodedContent( postParams ) ); return await response.Content.ReadAsByteArrayAsync(); }
private static async Task<StorageFile> GetFileAsync( MoodleFile file, bool create ) { // TODO: Find a way to export docs to the Documents library or something var folder = ApplicationData.Current.LocalCacheFolder; foreach ( string name in file.PathComponents ) { folder = await folder.CreateFolderAsync( FixName( name, Path.GetInvalidPathChars() ), CreationCollisionOption.OpenIfExists ); } string extension = string.IsNullOrWhiteSpace( file.Extension ) ? DefaultExtension : file.Extension; string fileName = FixName( file.Name + NameExtensionSeparator + extension, Path.GetInvalidFileNameChars() ); // GetFileAsync throws an exception if the file doesn't exist, so we have to use GetFilesAsync... var storageFile = ( await folder.GetFilesAsync() ).FirstOrDefault( f => f.Name == fileName ); if ( storageFile == null && create ) { storageFile = await folder.CreateFileAsync( fileName, CreationCollisionOption.ReplaceExisting ); } return storageFile; }
/// <summary> /// Gets a file. Optionally creates it if it doesn't exist. /// </summary> private static async Task<StorageFile> GetFileAsync( MoodleFile file, bool create ) { var folder = ApplicationData.Current.LocalFolder; foreach ( string name in file.PathComponents ) { folder = await folder.CreateFolderAsync( FixName( name, Path.GetInvalidPathChars() ), CreationCollisionOption.OpenIfExists ); } string extension = string.IsNullOrWhiteSpace( file.Extension ) ? DefaultExtension : file.Extension; string fileName = FixName( file.Name, Path.GetInvalidFileNameChars() ) + NameExtensionSeparator + extension; // GetFileAsync throws an exception if the file doesn't exist // and there's no API to check for a file's existence // so we have to use GetFilesAsync... var storageFile = ( await folder.GetFilesAsync( CommonFileQuery.DefaultQuery ) ) .FirstOrDefault( f => f.Name == fileName ); if ( storageFile == null && create ) { storageFile = await folder.CreateFileAsync( fileName ); } return storageFile; }
public Task<byte[]> DownloadAsync( MoodleFile file ) { return Task.FromResult( new byte[0] ); }
public async Task OpenFileAsync( MoodleFile moodleFile ) { var file = await GetFileAsync( moodleFile, false ); await Launcher.LaunchFileAsync( file ); }
public async Task<bool> IsStoredAsync( MoodleFile moodleFile ) { return await GetFileAsync( moodleFile, false ) != null; }
private const string DefaultExtension = "txt"; // for files without one public async Task StoreFileAsync( MoodleFile moodleFile, byte[] content ) { var file = await GetFileAsync( moodleFile, true ); await FileIO.WriteBytesAsync( file, content ); }
public Task StoreFileAsync( MoodleFile file, byte[] content ) { return Task.FromResult( 0 ); }
public Task OpenFileAsync( MoodleFile file ) { return Task.FromResult( 0 ); }
public Task<bool> IsStoredAsync( MoodleFile file ) { return Task.FromResult( false ); }