예제 #1
0
 protected void OnFailed(DownloaderError error, GeotaggingResult.Result geotaggingResult)
 {
     DebugUtil.Log(() => "PictureSyncManager: OnFailed" + error);
     Failed?.Invoke(error, geotaggingResult);
 }
예제 #2
0
        private async Task DownloadToSave(DownloadRequest req)
        {
            DebugUtil.LogSensitive(() => "Download picture: {0}", 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 e)
                {
                    req.Error?.Invoke(DownloaderError.Network, geoResult);
                    req.CompletionSource?.TrySetException(e);
                    return;
                }

                if (req.CancellationTokenSource?.IsCancellationRequested ?? false)
                {
                    req.CompletionSource?.TrySetCanceled(req.CancellationTokenSource.Token);
                    req.Error?.Invoke(DownloaderError.Cancelled, 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;
                        }
                    }
                }

                if (req.CancellationTokenSource?.IsCancellationRequested ?? false)
                {
                    req.CompletionSource?.TrySetCanceled(req.CancellationTokenSource.Token);
                    req.Error?.Invoke(DownloaderError.Cancelled, geoResult);
                    return;
                }

                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:
                        req.CompletionSource?.TrySetException(new NotSupportedException(req.Mediatype + " is not supported"));
                        return;
                    }

                    var folder = await rootFolder.CreateFolderAsync(DIRECTORY_NAME, CreationCollisionOption.OpenIfExists);

                    var filename = string.Format(req.NameBase + "_{0:yyyyMMdd_HHmmss}" + req.FileExtension, DateTime.Now);
                    var file     = await folder.CreateFileAsync(filename, CreationCollisionOption.GenerateUniqueName);

                    using (var outStream = await file.OpenStreamForWriteAsync())
                    {
                        if (req.CancellationTokenSource == null)
                        {
                            await imageStream.CopyToAsync(outStream);
                        }
                        else
                        {
                            await imageStream.CopyToAsync(outStream, 81920, req.CancellationTokenSource.Token);
                        }
                    }

                    req.Completed?.Invoke(folder, file, geoResult);
                    req.CompletionSource?.TrySetResult(true);
                    return;
                }
            }
            catch (Exception e)
            {
                DebugUtil.Log(() => e.Message);
                DebugUtil.Log(() => e.StackTrace);
                req.Error?.Invoke(DownloaderError.Unknown, GeotaggingResult.Result.NotRequested); // TODO
                req.CompletionSource?.TrySetException(e);
            }
        }
예제 #3
0
 protected void OnFetched(StorageFolder folder, StorageFile file, GeotaggingResult.Result geotaggingResult)
 {
     DebugUtil.Log(() => "PictureSyncManager: OnFetched");
     Fetched?.Invoke(folder, file, geotaggingResult);
 }
예제 #4
0
 private void Geolocator_PositionChanged(Geolocator sender, PositionChangedEventArgs args)
 {
     DebugUtil.Log(() => { return("Location updated: " + args.Position.Coordinate.Longitude + " , " + args.Position.Coordinate.Latitude); });
     LatestPosition = args.Position;
 }