Esempio n. 1
0
        public async Task <Dictionary <KeyValuePair <string, ulong>, DiscordSnapshotInfo> > GetLatestSnapshots(ulong ownerId, DateTime?filter = null)
        {
            Dictionary <KeyValuePair <string, ulong>, DiscordSnapshotInfo> snapshots = new Dictionary <KeyValuePair <string, ulong>, DiscordSnapshotInfo>();
            string ownerPath = Path.Combine(Core.Configuration.SnapshotsFolder, $"{ownerId}");

            if (!Directory.Exists(ownerPath))
            {
                return(snapshots);
            }

            foreach (var guildsDirectory in Directory.GetDirectories(ownerPath))
            {
                string snapshotHistoryPath = Path.Combine(guildsDirectory, "history.json");

                if (!File.Exists(snapshotHistoryPath))
                {
                    return(snapshots);
                }

                DiscordSnapshotHistory history =
                    JsonConvert.DeserializeObject <DiscordSnapshotHistory>(
                        await File.ReadAllTextAsync(snapshotHistoryPath));

                if (filter != null)
                {
                    history.History.RemoveAll(k => k.Value > filter);
                }

                if (history.History.Count == 0)
                {
                    continue;
                }

                history.History.Sort((c, k) =>
                {
                    if (c.Value < k.Value)
                    {
                        return(1);
                    }
                    if (c.Value > k.Value)
                    {
                        return(-1);
                    }
                    return(0);
                });

                string latestSnapshotPath = Path.Combine(guildsDirectory, $"{history.History.First().Key}.json");

                if (!File.Exists(latestSnapshotPath))
                {
                    continue;
                }

                DiscordSnapshotInfo info = JsonConvert.DeserializeObject <DiscordSnapshotInfo>(await File.ReadAllTextAsync(latestSnapshotPath));

                snapshots.Add(new KeyValuePair <string, ulong>(info.GuildName, info.GuildId), info);
            }

            return(snapshots);
        }
Esempio n. 2
0
        public async Task <DiscordSnapshotInfo> PushSnapshot(DiscordSnapshotInfo snapshotInfo)
        {
            snapshotInfo.SnapshotId = Guid.NewGuid();
            string snapshotDir = Path.Combine(Core.Configuration.SnapshotsFolder, $"{snapshotInfo.OwnerId}",
                                              $"{snapshotInfo.GuildId}");
            string snapshotPath        = Path.Combine(snapshotDir, $"{snapshotInfo.SnapshotId.ToString()}.json");
            string snapshotHistoryPath = Path.Combine(snapshotDir, "history.json");

            string snapshotData = JsonConvert.SerializeObject(snapshotInfo, Formatting.Indented);

            Directory.CreateDirectory(Path.GetDirectoryName(snapshotPath));

            await File.WriteAllTextAsync(snapshotPath, snapshotData);

            DiscordSnapshotHistory history;

            if (!File.Exists(snapshotHistoryPath))
            {
                history = new DiscordSnapshotHistory()
                {
                    History = new List <KeyValuePair <Guid, DateTime> >()
                };
            }
            else
            {
                string historyText = await File.ReadAllTextAsync(snapshotHistoryPath);

                history = JsonConvert.DeserializeObject <DiscordSnapshotHistory>(historyText);
            }

            history.History.Add(new KeyValuePair <Guid, DateTime>(snapshotInfo.SnapshotId, snapshotInfo.TakenAt));

            await File.WriteAllTextAsync(snapshotHistoryPath, JsonConvert.SerializeObject(history, Formatting.Indented));

            return(snapshotInfo);
        }