示例#1
0
        private static async Task DownloadFromUrl(StorageFile file)
        {
            // Try to read .url file.
            string url = await ParseInternetShortcut(file);

            // Parse URL.
            if (Uri.TryCreate(url, UriKind.Absolute, out var uri) &&
                (uri.Scheme == Uri.UriSchemeHttp || uri.Scheme == Uri.UriSchemeHttps)) // This is true if file is online.
            {
                // Check to make sure this URL points to a file and not a webpage.
                if (Path.HasExtension(uri.AbsoluteUri))
                {
                    // Get MIME type of online file.
                    var mimeType = MimeTypeMap.GetMimeType(Path.GetExtension(uri.AbsoluteUri));
                    // Check if MIME type of online file is supported.
                    if (FileTypes.IsSupportedMIME(mimeType))
                    {
                        // Get folder.
                        var folder = await StorageApplicationPermissions.FutureAccessList.GetFolderAsync("gallery");

                        // Download without awaiting so the UI doesn't freeze.
                        OnlineUtil.DownloadFile(uri, folder);
                    }
                }
            }
        }
        private static async Task DownloadFromRedditUriAsync(Uri redditUri)
        {
            // Get JSON version of reddit page.
            var json = await App.HttpClient.GetStringAsync(redditUri.ToString() + "/.json");

            var jsonDatas = JArray.Parse(json);
            var jsonData  = jsonDatas.FirstOrDefault();
            //string imageUrl = jsonData?.data?.children[0]?.data?.url;
            var imageUrl = jsonData?.Value <JToken>("data")?.Value <JArray>("children")?.FirstOrDefault()?.Value <JToken>("data")?.Value <string>("url") ?? string.Empty;

            if (string.IsNullOrEmpty(imageUrl))
            {
                return;
            }

            // Parse URL.
            if (Uri.TryCreate(imageUrl, UriKind.Absolute, out var imageUri) &&
                (imageUri.Scheme == Uri.UriSchemeHttp || imageUri.Scheme == Uri.UriSchemeHttps)) // This is true if file is online.
            {
                // Check to make sure this URL points to a file and not a webpage.
                if (Path.HasExtension(imageUri.AbsoluteUri))
                {
                    await DownloadFromUriAsync(imageUri);
                }
                else // It might be a video, check if it is.
                {
                    string videoUrl = jsonData?.Value <JToken>("data")?.Value <JArray>("children")?.FirstOrDefault()?.Value <JToken>("data")?.Value <JToken>("media")?.Value <JToken>("reddit_video")?.Value <string>("fallback_url") ?? string.Empty;
                    if (string.IsNullOrEmpty(videoUrl))
                    {
                        return;
                    }

                    // Parse URL.
                    if (Uri.TryCreate(videoUrl, UriKind.Absolute, out var videoUri) &&
                        (videoUri.Scheme == Uri.UriSchemeHttp || videoUri.Scheme == Uri.UriSchemeHttps)) // This is true if file is online.
                    {
                        var urlContentType = await GetUrlContentTypeAsync(videoUrl);

                        // Check if url points to video before downloading.
                        if (urlContentType.StartsWith("video/"))
                        {
                            // Get folder.
                            var folder = await StorageApplicationPermissions.FutureAccessList.GetFolderAsync("gallery");

                            // Append extension to uri.
                            var fileName = videoUri.Segments[videoUri.Segments.Length - 1] + $".{urlContentType.Split("/").LastOrDefault() ?? "mp4"}";
                            // Download without awaiting so the UI doesn't freeze.
                            OnlineUtil.DownloadFile(videoUri, fileName, folder);
                        }
                    }
                }
            }
        }
        private static async Task DownloadFromUriAsync(Uri uri)
        {
            // Check to make sure this URL points to a file and not a webpage.
            if (Path.HasExtension(uri.AbsoluteUri))
            {
                // Get MIME type of online file.
                var mimeType = MimeTypeMap.GetMimeType(Path.GetExtension(uri.AbsoluteUri));
                // Check if MIME type of online file is supported.
                if (FileTypes.IsSupportedMIME(mimeType))
                {
                    // Get folder.
                    var folder = await StorageApplicationPermissions.FutureAccessList.GetFolderAsync("gallery");

                    // Download without awaiting so the UI doesn't freeze.
                    OnlineUtil.DownloadFile(uri, folder);
                }
            }
        }
示例#4
0
        public async void SearchForHigherResolutionOnlineAsync()
        {
            // Don't scan for videos.
            if (MediaData is VideoData)
            {
                return;
            }

            var onlineMedias = new Dictionary <Uri, (Size, long)>();

            // Get file size and dimensions of every fully matched image.
            foreach (var webImage in FullyMatchedImages)
            {
                // Parse URL.
                if (Uri.TryCreate(webImage.Url, UriKind.Absolute, out var uri))
                {
                    // Check to make sure this URL points to a file and not a webpage.
                    if (Path.HasExtension(uri.AbsoluteUri))
                    {
                        // Get MIME type of online file.
                        var mimeType = MimeTypeMap.GetMimeType(Path.GetExtension(uri.AbsoluteUri));
                        // Check if MIME type of online file is supported.
                        if (FileTypes.IsSupportedMIME(mimeType))
                        {
                            try
                            {
                                // Get dimensions of online image.
                                var dimensions = await ImageUtilities.GetWebDimensionsAsync(uri);

                                // If dimensions is empty, continue.
                                if (dimensions.IsEmpty)
                                {
                                    continue;
                                }
                                // Get file size of online image.
                                var contentLength = await OnlineUtil.GetContentSizeAsync(uri);

                                // Add to dictionary.
                                onlineMedias.Add(uri, (dimensions, contentLength));
                            }
                            catch (Exception e)
                            {
                                // Log Exception.
                                LifecycleLog.Exception(e);
                            }
                        }
                    }
                }
            }

            // If these are null then no comparisons can be made.
            if (MediaData?.Meta == null)
            {
                return;
            }
            if (MediaData?.BasicProperties == null)
            {
                return;
            }

            // Check if any of the fully matched images are higher resolution or larger than local file.
            var largerMedia = new KeyValuePair <Uri, (Size, long)>();

            foreach (var urlAndData in onlineMedias)
            {
                var url = urlAndData.Key;
                var(dimensions, contentLength) = urlAndData.Value;

                // If online media is larger in file size than local media.
                if (contentLength > (long)MediaData.BasicProperties.Size)
                {
                    // If size of this online media is larger than the currently largest media.
                    if (Math.Max(largerMedia.Value.Item2, contentLength) == contentLength)
                    {
                        largerMedia = urlAndData; // Update largerMedia.
                    }
                }
                else if (dimensions.Height > MediaData.Meta.Height && // If online media is larger in both width and height
                         dimensions.Width > MediaData.Meta.Width)     // than the local media.
                {
                    // If dimensions of this online media is larger than the currently largest media.
                    if (Math.Max(largerMedia.Value.Item1.Height, dimensions.Height) == dimensions.Height &&
                        Math.Max(largerMedia.Value.Item1.Width, dimensions.Width) == dimensions.Width)
                    {
                        largerMedia = urlAndData; // Update largerMedia.
                    }
                }
            }

            // If no larger media has been found, return.
            if (largerMedia.Key == null)
            {
                return;
            }

            // Set property.
            LargerMedia = largerMedia.Key;
        }