Beispiel #1
0
        private async void Enqueue(Uri uri, string namebase, Mediatype type, Geoposition position, 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,
                    GeoPosition = position,
                    Mediatype   = type,
                    extension   = extension
                };
                DownloadQueue.Enqueue(req);
                QueueStatusUpdated.Raise(DownloadQueue.Count);
                ProcessQueueSequentially();
            });
        }
Beispiel #2
0
        private async Task DownloadToSave(DownloadRequest req)
        {
            DebugUtil.Log("Download picture: " + req.Uri.OriginalString);
            try
            {
                var geoResult = GeotaggingResult.NotRequested;

                var res = await HttpClient.GetAsync(req.Uri, HttpCompletionOption.ResponseContentRead).ConfigureAwait(false);

                switch (res.StatusCode)
                {
                case HttpStatusCode.OK:
                    break;

                case HttpStatusCode.Gone:
                    req.Error.Raise(DownloaderError.Gone, geoResult);
                    return;

                default:
                    req.Error.Raise(DownloaderError.Network, geoResult);
                    return;
                }

                var imageStream = await res.Content.ReadAsStreamAsync().ConfigureAwait(false);

                if (req.Mediatype == Mediatype.Image && req.GeoPosition != null)
                {
                    try
                    {
                        imageStream = await MetaDataOperator.AddGeopositionAsync(imageStream, req.GeoPosition, false);

                        geoResult = Utility.GeotaggingResult.OK;
                    }
                    catch (GpsInformationAlreadyExistsException)
                    {
                        geoResult = GeotaggingResult.GeotagAlreadyExists;
                    }
                    catch
                    {
                        geoResult = GeotaggingResult.UnExpectedError;
                    }
                }

                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 stream = await file.OpenAsync(FileAccessMode.ReadWrite))
                    {
                        var buffer = new byte[BUFFER_SIZE];
                        using (var os = stream.GetOutputStreamAt(0))
                        {
                            int read = 0;
                            while ((read = imageStream.Read(buffer, 0, BUFFER_SIZE)) != 0)
                            {
                                await os.WriteAsync(buffer.AsBuffer(0, read));
                            }
                        }
                    }
                    req.Completed.Raise(folder, file, geoResult);
                    return;
                }
            }
            catch (Exception e)
            {
                DebugUtil.Log(e.Message);
                DebugUtil.Log(e.StackTrace);
                req.Error.Raise(DownloaderError.Unknown, GeotaggingResult.NotRequested); // TODO
            }
        }