public override Stream LoadFile(string fileID, bool isZipFile) { var context = new AsyncContext { WaitHandle = new AutoResetEvent(false) }; LoadFileAsync(fileID, context); context.WaitHandle.WaitOne(); if (context.Error != null) { Thread.MemoryBarrier(); throw context.Error; } if (!isZipFile) { return context.Stream; } Stream source = UnZip(context.Stream); var buffer = new byte[4096]; var memoryStream = new MemoryStream(); StreamUtils.Copy(source, memoryStream, buffer); memoryStream.Position = 0L; return memoryStream; }
private async void LoadFileAsync(string fileId, AsyncContext context) { try { var sdCardStorage = (await ExternalStorage.GetExternalStorageDevicesAsync()).FirstOrDefault(); if (sdCardStorage == null) { context.Error = new Exception("There are no external storage devices found."); context.WaitHandle.Set(); return; } var file = await sdCardStorage.GetFileAsync(fileId); var stream = await file.OpenForReadAsync(); context.Stream = CopyStream(stream); } catch (Exception exception) { context.Error = exception; } finally { context.WaitHandle.Set(); } }
public override Stream LoadFile(string pathFile, bool isZipFile) { var context = new AsyncContext { WaitHandle = new AutoResetEvent(false) }; LoadFileAsync(pathFile, context); context.WaitHandle.WaitOne(); if (context.Error != null) { throw context.Error; } return context.Stream; }
private async void LoadFileAsync(string fileID, AsyncContext context) { try { LiveConnectClient skyDrive = await _liveLogin.Login(); if (skyDrive == null) { context.Error = new Exception("Login Required"); return; } LiveOperationResult fileData = await skyDrive.GetAsync(fileID); string path = FixSkyDriveUrl((string) fileData.Result["source"]); LiveDownloadOperationResult downloadResult = await skyDrive.DownloadAsync(path); var buffer = new byte[4096]; var memoryStream = new MemoryStream(); StreamUtils.Copy(downloadResult.Stream, memoryStream, buffer); memoryStream.Position = 0L; context.Stream = memoryStream; } catch (WebException e) { if (e.Status == WebExceptionStatus.RequestCanceled) { context.Error = new RestartException(e.Message, e); return; } context.Error = e; } catch (TaskCanceledException e) { context.Error = new RestartException(e.Message, e); } catch (Exception e) { context.Error = e; } finally { context.WaitHandle.Set(); } }
public override Stream LoadFile(string pathFile, bool isZipFile) { var asyncContext = new AsyncContext { Stream = new MemoryStream(), WaitHandle = new AutoResetEvent(false), IsZip = isZipFile }; LoadFileAsync(pathFile, asyncContext); asyncContext.WaitHandle.WaitOne(); if (asyncContext.Error != null) { throw asyncContext.Error; } return asyncContext.Stream; }
public override Stream LoadFile(string uri, bool isZipFile) { var asyncContext = new AsyncContext { Stream = new MemoryStream(), WaitHandle = new AutoResetEvent(false), IsZip = isZipFile }; var webClient = new WebClient(); webClient.OpenReadCompleted += WebClientOnOpenReadCompleted; webClient.OpenReadAsync(new Uri(uri), asyncContext); asyncContext.WaitHandle.WaitOne(); if (asyncContext.Error != null) throw asyncContext.Error; return asyncContext.Stream; }
private async void LoadFileAsync(string pathFile, AsyncContext context) { try { HttpClient httpClient = CreateHttpClient(); HttpContent httpContent = CreateHttpContent(pathFile); HttpResponseMessage response = await httpClient.PostAsync(pathFile.Split('?')[0], httpContent); var stream = await response.Content.ReadAsStreamAsync(); if (context.IsZip) { stream = UnZip(stream); } stream.CopyTo(context.Stream); } catch (WebException e) { if (e.Status == WebExceptionStatus.RequestCanceled) { context.Error = new RestartException(e.Message, e); return; } context.Error = e; } catch (TaskCanceledException e) { context.Error = new RestartException(e.Message, e); } catch (Exception e) { context.Error = e; } finally { context.WaitHandle.Set(); } }
private async void LoadFileAsync(string fileId, AsyncContext context) { try { var file = await SharedStorageAccessManager.CopySharedFileAsync(ApplicationData.Current.LocalFolder, fileId + ".tmp", NameCollisionOption.ReplaceExisting, fileId); using (var stream = await file.OpenStreamForReadAsync()) { context.Stream = CopyStream(stream); } await file.DeleteAsync(StorageDeleteOption.PermanentDelete); } catch (Exception exception) { context.Error = exception; } finally { context.WaitHandle.Set(); } }
public override Stream LoadFile(string pathFile, bool isZipFile) { var context = new AsyncContext { WaitHandle = new AutoResetEvent(false), IsZip = isZipFile }; LoadFileAsync(pathFile, context); context.WaitHandle.WaitOne(); if (context.Error != null) { throw context.Error; } if (!context.IsZip) { return context.Stream; } Stream source = UnZip(context.Stream); return CopyStream(source); }