private async void Enqueue(Uri uri, string namebase, Mediatype type, string extension = null) { DebugUtil.Log(() => "ContentsDownloader: Enqueue " + uri.AbsolutePath); if (extension == null) { var split = uri.AbsolutePath.Split('.'); if (split.Length > 0) { extension = "." + split[split.Length - 1].ToLower(); DebugUtil.Log(() => "detected file extension: " + extension); } } await SystemUtil.GetCurrentDispatcher().RunAsync(CoreDispatcherPriority.Low, () => { var req = new DownloadRequest { Uri = uri, NameBase = namebase, Completed = OnFetched, Error = OnFailed, Mediatype = type, extension = extension }; DownloadQueue.Enqueue(req); QueueStatusUpdated?.Invoke(DownloadQueue.Count); ProcessQueueSequentially(); }); }
private async Task Enqueue(Uri uri, string namebase, Mediatype type, CancellationTokenSource cts, string extension = null) { DebugUtil.LogSensitive(() => "ContentsDownloader: Enqueue {0}", uri.AbsolutePath); if (extension == null) { var split = uri.AbsolutePath.Split('.'); if (split.Length > 0) { extension = "." + split[split.Length - 1].ToLower(); DebugUtil.Log(() => "detected file extension: " + extension); } } var tcs = new TaskCompletionSource <bool>(); await SystemUtil.GetCurrentDispatcher().RunAsync(CoreDispatcherPriority.Low, () => { var req = new DownloadRequest { Uri = uri, NameBase = namebase, Completed = OnFetched, Error = OnFailed, Mediatype = type, FileExtension = extension, CompletionSource = tcs, CancellationTokenSource = cts, }; DownloadQueue.Enqueue(req); QueueStatusUpdated?.Invoke(DownloadQueue.Count); ProcessQueueSequentially(); }); await tcs.Task; }
private async Task DownloadToSave(DownloadRequest req) { DebugUtil.Log(() => "Download picture: " + req.Uri.OriginalString); try { var geoResult = GeotaggingResult.Result.NotRequested; var task = HttpClient.GetAsync(req.Uri, HttpCompletionOption.ResponseContentRead); HttpResponseMessage res; try { res = await task; } catch (Exception) { req.Error?.Invoke(DownloaderError.Network, geoResult); return; } var imageStream = (await res.Content.ReadAsInputStreamAsync()).AsStreamForRead(); if (req.Mediatype == Mediatype.Image) { if (ApplicationSettings.GetInstance().GeotagEnabled) { var position = await GeolocatorManager.INSTANCE.GetLatestPosition(); if (position == null) { geoResult = GeotaggingResult.Result.FailedToAcquireLocation; } else { var result = await GeopositionUtil.AddGeotag(imageStream, position); imageStream = result.Image; geoResult = result.OperationResult; } } } using (imageStream) { StorageFolder rootFolder; switch (req.Mediatype) { case Mediatype.Image: rootFolder = KnownFolders.PicturesLibrary; break; case Mediatype.Video: rootFolder = KnownFolders.PicturesLibrary; // Use Pictures folder according to the behavior of built-in Camera apps // rootFolder = KnownFolders.VideosLibrary; break; default: return; } var folder = await rootFolder.CreateFolderAsync(DIRECTORY_NAME, CreationCollisionOption.OpenIfExists); var filename = string.Format(req.NameBase + "_{0:yyyyMMdd_HHmmss}" + req.extension, DateTime.Now); var file = await folder.CreateFileAsync(filename, CreationCollisionOption.GenerateUniqueName); using (var outStream = await file.OpenStreamForWriteAsync()) { await imageStream.CopyToAsync(outStream); } req.Completed?.Invoke(folder, file, geoResult); return; } } catch (Exception e) { DebugUtil.Log(() => e.Message); DebugUtil.Log(() => e.StackTrace); req.Error?.Invoke(DownloaderError.Unknown, GeotaggingResult.Result.NotRequested); // TODO } }