Beispiel #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);
                }
            }
        }