/// <summary> /// Downloads (if it hasn't already been downloaded) and opens the specified file. /// </summary> private async Task OpenFileAsync(MoodleFile file) { if (DownloadState == DownloadState.Downloading) { return; } if (!(await _storage.IsStoredAsync(file))) { DownloadState = DownloadState.Downloading; try { var bytes = await _downloader.DownloadAsync(file); await _storage.StoreFileAsync(file, bytes); DownloadState = DownloadState.None; } catch { DownloadState = DownloadState.Error; } } if (DownloadState == DownloadState.None) { await _storage.OpenFileAsync(file); } }
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()); }
/// <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 Task OpenFileAsync(MoodleFile file) { return(Task.FromResult(0)); }
public Task <bool> IsStoredAsync(MoodleFile file) { return(Task.FromResult(false)); }
public Task StoreFileAsync(MoodleFile file, byte[] content) { return(Task.FromResult(0)); }
/// <summary> /// Asynchronously opens the specified Moodle file. /// </summary> public async Task OpenFileAsync(MoodleFile moodleFile) { var file = await GetFileAsync(moodleFile, false); await Launcher.LaunchFileAsync(file); }
/// <summary> /// Asynchronously indicates whether the specified Moodle file is stored on the device. /// </summary> public async Task <bool> IsStoredAsync(MoodleFile moodleFile) { return(await GetFileAsync(moodleFile, false) != null); }