Exemple #1
0
        /// <summary>
        ///     Checks if we have an updated version and returns update info
        /// </summary>
        /// <returns>Update info</returns>
        public static async Task <UpdateInfo> IsUpdateAvailable()
        {
            try
            {
                //clean update folder on backgraound, dont bother with result - we are ready for collisions
                var t1 = CleanTemporaryUpdateFolderAsync();

                var httpFilter = new Windows.Web.Http.Filters.HttpBaseProtocolFilter();
                httpFilter.CacheControl.ReadBehavior =
                    Windows.Web.Http.Filters.HttpCacheReadBehavior.MostRecent;

                //dwonload version info
                using (var client = new HttpClient(httpFilter))
                {
                    using (var response = await client.GetAsync(new Uri(VersionFileUrl), HttpCompletionOption.ResponseContentRead))
                    {
                        string json = await response.Content.ReadAsStringAsync();

                        if (!VersionInfo.SetInstance(json))
                        {
                            return(new UpdateInfo(UpdateStatus.NoInternet));
                        }
                    }
                }

                // Check if version is newer
                var currentVersion = Package.Current.Id.Version;
                var repoVersion    = GetVersionFromPattern(@"(\d*)\.(\d*)\.(\d*)", VersionInfo.Instance.latest_release.version);
                var minVersion     = GetVersionFromPattern(@"(\d*)\.(\d*)\.(\d*)", VersionInfo.Instance.minimum_version);

                //compare major & minor & build (ignore revision)
                if (IsVersionGreater(currentVersion, repoVersion))
                {
                    UpdateStatus updateStatus = UpdateStatus.UpdateAvailable;
                    //patch architecture
                    VersionInfo.Instance.latest_release.setup_file = VersionInfo.Instance.latest_release.setup_file.Replace("{arch}", Package.Current.Id.Architecture.ToString());

                    if (IsVersionGreater(currentVersion, minVersion))
                    {
                        updateStatus = UpdateStatus.UpdateForced;
                    }


                    return(new UpdateInfo(updateStatus, repoVersion.Major + "." + repoVersion.Minor + "." + repoVersion.Build,
                                          VersionInfo.Instance.latest_release.setup_file, VersionInfo.Instance.latest_release.changes));
                }
                else if (IsVersionGreater(repoVersion, minVersion))
                {
                    return(new UpdateInfo(UpdateStatus.NextVersionNotReady));
                }

                return(new UpdateInfo(UpdateStatus.NoUpdate));
            }
            catch (Exception)
            {
                return(new UpdateInfo(UpdateStatus.NoInternet));
            }
        }
        public static async Task <String> Download(String src)
        {
            filter       = new Windows.Web.Http.Filters.HttpBaseProtocolFilter();
            httpClient   = new HttpClient();
            cts          = new CancellationTokenSource();
            isFilterUsed = false;
            Uri resourceUri;


            if (!Uri.TryCreate(src.Trim(), UriKind.Absolute, out resourceUri))
            {
                return(null);
            }

            filter.CacheControl.ReadBehavior  = HttpCacheReadBehavior.Default;
            filter.CacheControl.WriteBehavior = HttpCacheWriteBehavior.Default;

            // ---------------------------------------------------------------------------
            // WARNING: Only test applications should ignore SSL errors.
            // In real applications, ignoring server certificate errors can lead to MITM
            // attacks (while the connection is secure, the server is not authenticated).
            //
            // The SetupServer script included with this sample creates a server certificate that is self-signed
            // and issued to fabrikam.com, and hence we need to ignore these errors here.
            // ---------------------------------------------------------------------------
            //filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.Untrusted);
            //filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.InvalidName);

            HttpResponseMessage response = await httpClient.GetAsync(resourceUri);

            String output = await response.Content.ReadAsStringAsync();

            System.Diagnostics.Debug.WriteLine("!" + output + "!");
            isFilterUsed = true;
            return(output);
        }