Ejemplo n.º 1
0
        private static RavenJToken GetValueWithDates(string val)
        {
            try
            {
                if (val.StartsWith("{"))
                {
                    return(RavenJObject.Parse(val));
                }
                if (val.StartsWith("["))
                {
                    return(RavenJArray.Parse(val));
                }

                DateTime dateTime;
                if (DateTime.TryParseExact(val, Default.OnlyDateTimeFormat, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, out dateTime))
                {
                    if (val.EndsWith("Z"))
                    {
                        return(DateTime.SpecifyKind(dateTime, DateTimeKind.Utc));
                    }
                    return(new RavenJValue(dateTime));
                }

                DateTimeOffset dateTimeOffset;
                if (DateTimeOffset.TryParseExact(val, Default.DateTimeFormatsToRead, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, out dateTimeOffset))
                {
                    return(new RavenJValue(dateTimeOffset));
                }

                return(new RavenJValue(Uri.UnescapeDataString(val)));
            }
            catch (Exception exc)
            {
                throw new JsonReaderException(string.Concat("Unable to get value: ", val), exc);
            }
        }
Ejemplo n.º 2
0
 private static RavenJToken GetValue(string val)
 {
     try
     {
         if (val.StartsWith("{"))
         {
             return(RavenJObject.Parse(val));
         }
         if (val.StartsWith("["))
         {
             return(RavenJArray.Parse(val));
         }
         DateTime result;
         if (DateTime.TryParseExact(val, new[] { "r", "o" }, CultureInfo.InvariantCulture, DateTimeStyles.None, out result))
         {
             return(new RavenJValue(result));
         }
         return(new RavenJValue(val));
     }
     catch (Exception exc)
     {
         throw new JsonReaderException(string.Concat("Unable to get value: ", val), exc);
     }
 }
Ejemplo n.º 3
0
 public static IEnumerable <dynamic> GetDocumentsFromString(string json)
 {
     return(RavenJArray.Parse(json).Cast <RavenJObject>().Select(JsonToExpando.Convert));
 }
Ejemplo n.º 4
0
        /* Checks for updates and returns release informations when there is newer one.
         * Returns null if there is no newer release.
         * If Prerelease argument is true, it will return also Pre-release, otherwise Pre-releases are ignored.
         * An existing last checked release will be discarded.
         * Throws UpdaterException if error occurs.
         */
        public static Release CheckForUpdates(bool Prerelease = true)
        {
            if (Latest != null)
            {
                if (Latest.IsDownloading)
                {
                    throw new UpdaterException("Download already in progress");
                }
                Latest.Dispose();
            }
            Latest    = null;
            IsChecked = IsInstalled = false;

            var webClient = new UpdaterWebClient();

            webClient.Encoding = Encoding.UTF8;

            try
            {
                string json = webClient.DownloadString(GitAPILatestReleaseURL);

                RavenJArray releases = RavenJArray.Parse(json);
                if (releases.Length < 1)
                {
                    throw new UpdaterException("No release found");
                }

                string current = GetCurrentVersion(); // Current version (tag).

                // Iterate thru avialable releases.
                foreach (RavenJObject release in (RavenJArray)releases)
                {
                    // Drafts are not returned by API, but just in case...
                    bool draft = release["draft"].Value <bool>();
                    if (draft)
                    {
                        continue;        // Ignore drafts.
                    }
                    // Check if there are assets attached.
                    RavenJArray assets = (RavenJArray)release["assets"];
                    if (assets.Length < 1)
                    {
                        continue;                    // No assets, ignore it.
                    }
                    // Compare release tag with our version (tag).
                    // Assumption is that no one will make realease with older version tag.
                    // So, any different version should be newer.
                    string tag = release["tag_name"].Value <string>();
                    if (tag == current) // If we didn't find different tag till now, then there is no newer version.
                    {
                        IsChecked = true;

                        return(null);
                    }

                    // Check if it is pre-release and we want to update to it.
                    bool prerelease = release["prerelease"].Value <bool>();
                    if (prerelease && !Prerelease)
                    {
                        continue;                            // Found unwanted pre-release, ignore it.
                    }
                    // Find PoESkillTree ZIP package.
                    RavenJObject zipAsset = null;
                    foreach (RavenJObject asset in assets)
                    {
                        string content_type = asset["content_type"].Value <string>();
                        if (content_type != "application/zip")
                        {
                            continue;                                    // Not a ZIP, ignore it.
                        }
                        string name = asset["name"].Value <string>();
                        Match  m    = ReZipPackage.Match(name);
                        if (m.Success)
                        {
                            // Found ZIP package.
                            zipAsset = asset;
                            break;
                        }
                    }
                    if (zipAsset == null)
                    {
                        continue;                   // No ZIP package found.
                    }
                    // This is newer release (probably).
                    IsChecked = true;
                    Latest    = new Release
                    {
                        Name        = release["name"].Value <string>(),
                        Description = release["body"].Value <string>(),
                        Prerelease  = prerelease,
                        Version     = tag,
                        URI         = new Uri(zipAsset["browser_download_url"].Value <string>())
                    };

                    // We are done, exit loop.
                    break;
                }
            }
            catch (WebException e)
            {
                if (e.Status == WebExceptionStatus.ProtocolError)
                {
                    throw new UpdaterException("HTTP " + ((int)((HttpWebResponse)e.Response).StatusCode) + " " + ((HttpWebResponse)e.Response).StatusDescription);
                }
                else
                {
                    throw new UpdaterException(e.Message, e);
                }
            }
            catch (Exception e)
            {
                throw new UpdaterException(e.Message, e);
            }

            return(Latest);
        }
Ejemplo n.º 5
0
        /* Checks for updates and returns release informations when there is newer one.
         * Returns null if there is no newer release.
         * If Prerelease argument is true, it will return also Pre-release, otherwise Pre-releases are ignored.
         * An existing last checked release will be discarded.
         * Throws UpdaterException if error occurs.
         */
        public static Release CheckForUpdates(bool Prerelease = true)
        {
            if (Latest != null)
            {
                if (Latest.IsDownloading)
                {
                    throw new UpdaterException(L10n.Message("Download already in progress"));
                }

                // If release was installed, report no update.
                if (Latest.IsInstalled)
                {
                    return(null);
                }

                Dispose();
            }

            if (IsNewerProductInstalled())
            {
                // Newer product is installed, there is no update.
                IsChecked = true;

                return(null);
            }

            var webClient = new UpdaterWebClient();

            webClient.Encoding = Encoding.UTF8;

            try
            {
                string json = webClient.DownloadString(GitAPILatestReleaseURL);

                RavenJArray releases = RavenJArray.Parse(json);
                if (releases.Length < 1)
                {
                    throw new UpdaterException(L10n.Message("No release found"));
                }

                Version current = GetCurrentVersion(); // Current version (tag).

                // Iterate thru avialable releases.
                foreach (RavenJObject release in (RavenJArray)releases)
                {
                    // Drafts are not returned by API, but just in case...
                    bool draft = release["draft"].Value <bool>();
                    if (draft)
                    {
                        continue;        // Ignore drafts.
                    }
                    // Check if there are assets attached.
                    RavenJArray assets = (RavenJArray)release["assets"];
                    if (assets.Length < 1)
                    {
                        continue;                    // No assets, ignore it.
                    }
                    // Compare release tag with our version (tag).
                    string  tag     = release["tag_name"].Value <string>();
                    Version version = new Version(tag);
                    if (version.CompareTo(current) <= 0)
                    {
                        // Same or older version.
                        IsChecked = true;

                        return(null);
                    }

                    // Check if it is pre-release and we want to update to it.
                    bool prerelease = release["prerelease"].Value <bool>();
                    if (prerelease && !Prerelease)
                    {
                        continue;                            // Found unwanted pre-release, ignore it.
                    }
                    // Find release package.
                    string       fileName = null;
                    RavenJObject pkgAsset = null;
                    foreach (RavenJObject asset in assets)
                    {
                        // Check if asset upload completed.
                        if (asset["state"].Value <string>() != "uploaded")
                        {
                            continue;
                        }

                        string content_type = asset["content_type"].Value <string>();
                        if (content_type != PackageContentType)
                        {
                            continue;                                     // Not a package, ignore it.
                        }
                        fileName = asset["name"].Value <string>();
                        Match m = RePackage.Match(fileName);
                        if (m.Success)
                        {
                            // Found release package.
                            pkgAsset = asset;
                            break;
                        }
                    }
                    if (pkgAsset == null)
                    {
                        continue;                   // No package found.
                    }
                    // This is newer release.
                    IsChecked = true;
                    Latest    = new Release
                    {
                        Name         = release["name"].Value <string>(),
                        Description  = release["body"].Value <string>(),
                        IsPrerelease = prerelease,
                        // A release is an update, if file name starts with our PackageName.
                        IsUpdate        = fileName.StartsWith(GetPackageName(Properties.Version.ProductName) + "-"),
                        PackageFileName = fileName,
                        Version         = tag,
                        URI             = new Uri(pkgAsset["browser_download_url"].Value <string>())
                    };

                    // We are done, exit loop.
                    break;
                }
            }
            catch (WebException e)
            {
                if (e.Status == WebExceptionStatus.ProtocolError)
                {
                    throw new UpdaterException("HTTP " + ((int)((HttpWebResponse)e.Response).StatusCode) + " " + ((HttpWebResponse)e.Response).StatusDescription);
                }
                else
                {
                    throw new UpdaterException(e.Message, e);
                }
            }
            catch (Exception e)
            {
                throw new UpdaterException(e.Message, e);
            }

            return(Latest);
        }
Ejemplo n.º 6
0
        public async Task DataDumperExportHandlesMaxEtagCorrectly()
        {
            using (var store = NewDocumentStore())
            {
                using (var session = store.OpenSession())
                {
                    for (var i = 0; i < 10; i++)
                    {
                        session.Store(new User {
                            Name = "oren #" + (i + 1)
                        });
                    }
                    session.SaveChanges();
                }

                using (var textStream = new StringWriter())
                    using (var writer = new JsonTextWriter(textStream))
                    {
                        var dumper = new CustomDataDumper(store.SystemDatabase);

                        var startEtag = store.SystemDatabase.Statistics.LastDocEtag.IncrementBy(-5);
                        var endEtag   = startEtag.IncrementBy(2);

                        writer.WriteStartArray();
                        var lastEtag = await dumper.ExportDocuments(writer, startEtag, endEtag);

                        writer.WriteEndArray();
                        writer.Flush();

                        // read exported content
                        var exportedDocs = RavenJArray.Parse(textStream.GetStringBuilder().ToString());
                        Assert.Equal(2, exportedDocs.Count());

                        Assert.Equal("01000000-0000-0001-0000-000000000007", exportedDocs.First().Value <RavenJObject>("@metadata").Value <string>("@etag"));
                        Assert.Equal("01000000-0000-0001-0000-000000000008", exportedDocs.Last().Value <RavenJObject>("@metadata").Value <string>("@etag"));
                        Assert.Equal("01000000-0000-0001-0000-000000000008", lastEtag.ToString());
                    }

                using (var textStream = new StringWriter())
                    using (var writer = new JsonTextWriter(textStream))
                    {
                        var dumper = new CustomDataDumper(store.SystemDatabase);

                        var startEtag = store.SystemDatabase.Statistics.LastDocEtag.IncrementBy(-5);

                        writer.WriteStartArray();
                        var lastEtag = await dumper.ExportDocuments(writer, startEtag, null);

                        writer.WriteEndArray();
                        writer.Flush();

                        // read exported content
                        var exportedDocs = RavenJArray.Parse(textStream.GetStringBuilder().ToString());
                        Assert.Equal(5, exportedDocs.Count());

                        Assert.Equal("01000000-0000-0001-0000-000000000007", exportedDocs.First().Value <RavenJObject>("@metadata").Value <string>("@etag"));
                        Assert.Equal("01000000-0000-0001-0000-00000000000B", exportedDocs.Last().Value <RavenJObject>("@metadata").Value <string>("@etag"));
                        Assert.Equal("01000000-0000-0001-0000-00000000000B", lastEtag.ToString());
                    }

                for (var i = 0; i < 10; i++)
                {
                    store.DatabaseCommands.PutAttachment("attach/" + (i + 1), null, new MemoryStream(new [] { (byte)i }), new RavenJObject());
                }

                using (var textStream = new StringWriter())
                    using (var writer = new JsonTextWriter(textStream))
                    {
                        var dumper = new CustomDataDumper(store.SystemDatabase);

                        var startEtag = store.SystemDatabase.Statistics.LastAttachmentEtag.IncrementBy(-5);
                        var endEtag   = startEtag.IncrementBy(2);

                        writer.WriteStartArray();
                        var lastEtag = await dumper.ExportAttachments(writer, startEtag, endEtag);

                        writer.WriteEndArray();
                        writer.Flush();

                        // read exported content
                        var exportedAttachments = RavenJArray.Parse(textStream.GetStringBuilder().ToString());
                        Assert.Equal(2, exportedAttachments.Count());

                        Assert.Equal("02000000-0000-0001-0000-000000000006", exportedAttachments.First().Value <string>("Etag"));
                        Assert.Equal("02000000-0000-0001-0000-000000000007", exportedAttachments.Last().Value <string>("Etag"));
                        Assert.Equal("02000000-0000-0001-0000-000000000007", lastEtag.ToString());
                    }

                using (var textStream = new StringWriter())
                    using (var writer = new JsonTextWriter(textStream))
                    {
                        var dumper = new CustomDataDumper(store.SystemDatabase);

                        var startEtag = store.SystemDatabase.Statistics.LastAttachmentEtag.IncrementBy(-5);

                        writer.WriteStartArray();
                        var lastEtag = await dumper.ExportAttachments(writer, startEtag, null);

                        writer.WriteEndArray();
                        writer.Flush();

                        // read exported content
                        var exportedAttachments = RavenJArray.Parse(textStream.GetStringBuilder().ToString());
                        Assert.Equal(5, exportedAttachments.Count());

                        Assert.Equal("02000000-0000-0001-0000-000000000006", exportedAttachments.First().Value <string>("Etag"));
                        Assert.Equal("02000000-0000-0001-0000-00000000000A", exportedAttachments.Last().Value <string>("Etag"));
                        Assert.Equal("02000000-0000-0001-0000-00000000000A", lastEtag.ToString());
                    }

                WaitForIndexing(store);

                store.DatabaseCommands.DeleteByIndex("Raven/DocumentsByEntityName", new IndexQuery()
                {
                    Query = "Tag:Users"
                }).WaitForCompletion();

                for (var i = 0; i < 10; i++)
                {
                    store.DatabaseCommands.DeleteAttachment("attach/" + (i + 1), null);
                }

                Etag user6DeletionEtag = null, user9DeletionEtag = null, attach5DeletionEtag = null, attach7DeletionEtag = null;

                WaitForUserToContinueTheTest(store);

                store.SystemDatabase.TransactionalStorage.Batch(accessor =>
                {
                    user6DeletionEtag =
                        accessor.Lists.Read(Constants.RavenPeriodicExportsDocsTombstones, "users/6").Etag;
                    user9DeletionEtag =
                        accessor.Lists.Read(Constants.RavenPeriodicExportsDocsTombstones, "users/9").Etag;
                    attach5DeletionEtag =
                        accessor.Lists.Read(Constants.RavenPeriodicExportsAttachmentsTombstones, "attach/5").Etag;
                    attach7DeletionEtag =
                        accessor.Lists.Read(Constants.RavenPeriodicExportsAttachmentsTombstones, "attach/7").Etag;
                });

                using (var textStream = new StringWriter())
                    using (var writer = new JsonTextWriter(textStream))
                    {
                        var dumper = new CustomDataDumper(store.SystemDatabase);

                        writer.WriteStartObject();
                        var lastEtags    = new LastEtagsInfo();
                        var exportResult = new ExportDataResult
                        {
                            LastDocDeleteEtag         = user6DeletionEtag,
                            LastAttachmentsDeleteEtag = attach5DeletionEtag
                        };

                        lastEtags.LastDocDeleteEtag         = user9DeletionEtag;
                        lastEtags.LastAttachmentsDeleteEtag = attach7DeletionEtag;
                        dumper.ExportDeletions(writer, exportResult, lastEtags).Wait();
                        writer.WriteEndObject();
                        writer.Flush();

                        // read exported content
                        var exportJson = RavenJObject.Parse(textStream.GetStringBuilder().ToString());
                        var docsKeys   =
                            exportJson.Value <RavenJArray>("DocsDeletions").Select(x => x.Value <string>("Key")).ToArray();
                        var attachmentsKeys =
                            exportJson.Value <RavenJArray>("AttachmentsDeletions")
                            .Select(x => x.Value <string>("Key"))
                            .ToArray();
                        Assert.Equal(new [] { "users/7", "users/8", "users/9" }, docsKeys);
                        Assert.Equal(new [] { "attach/6", "attach/7" }, attachmentsKeys);
                    }
            }
        }
Ejemplo n.º 7
0
        /* Checks for updates and returns release informations when there is newer one.
         * Returns null if there is no newer release.
         * An existing last checked release will be discarded.
         * Throws UpdaterException if error occurs.
         */
        public static Release CheckForUpdates()
        {
            if (Latest != null)
            {
                if (Latest.IsDownloading)
                {
                    throw new UpdaterException("Download already in progress");
                }
                Latest.Dispose();
            }
            Latest    = null;
            IsChecked = IsInstalled = false;

            var webClient = new UpdaterWebClient();

            webClient.Encoding = Encoding.UTF8;

            try
            {
                string json = webClient.DownloadString(GitAPILatestReleaseURL);

                RavenJArray releases = RavenJArray.Parse(json);
                if (releases.Length < 1)
                {
                    throw new UpdaterException("No release found");
                }

                RavenJObject latest = (RavenJObject)releases[0];
                RavenJArray  assets = (RavenJArray)latest["assets"];
                if (assets.Length < 1)
                {
                    throw new UpdaterException("Package for release is missing");
                }

                string current = GetCurrentVersion();
                string tag     = latest["tag_name"].Value <string>();
                if (tag == current)
                {
                    IsChecked = true;

                    return(null);
                }

                string url = ((RavenJObject)assets[0])["browser_download_url"].Value <string>();

                IsChecked = true;
                Latest    = new Release
                {
                    Name        = latest["name"].Value <string>(),
                    Description = latest["body"].Value <string>(),
                    Version     = tag,
                    URI         = new Uri(url)
                };
            }
            catch (WebException e)
            {
                if (e.Status == WebExceptionStatus.ProtocolError)
                {
                    throw new UpdaterException("HTTP " + ((int)((HttpWebResponse)e.Response).StatusCode) + " " + ((HttpWebResponse)e.Response).StatusDescription);
                }
                else
                {
                    throw new UpdaterException(e.Message, e);
                }
            }
            catch (Exception e)
            {
                throw new UpdaterException(e.Message, e);
            }

            return(Latest);
        }