private void HandleAddTorrent(TorrentInfo info, string filePath)
        {
            var par = new AddTorrentParams { SavePath = "C:/Downloads", TorrentInfo = info };

            if (!this.ParentWindow.TorrentModel.Session.TorrentExist(par))
            {
                this.ParentWindow.TorrentModel.Session.AddTorrent(par);
                using (var db = new TorLiteDatabase())
                {
                    var fadded = db.FileStorage.Upload(Guid.NewGuid().ToString(), filePath);
                    var cools = db.GetCollection<Torrent>("Torrents");
                    cools.Insert(new Torrent { Name = par.TorrentInfo.Name, UTC_CreationDate = DateTime.UtcNow, File = fadded.Id, Hash = par.TorrentInfo.InfoHash, Path = par.SavePath });
                }
            }
        }
        public TorrentModel()
        {
            Session = new Session();
            using (var db = new TorLiteDatabase())
            {
                var colls = db.GetCollection<SessionState>("SessionState");
                var state = colls.FindAll().FirstOrDefault();
                if (state != null)
                {
                    var ms = new MemoryStream();
                    db.FileStorage.Download(state.State, ms);
                    ms.Position = 0;
                    Session.Start(ms.GetBuffer());
                }
                else Session.Start();
            }

            this.Session.OnAlert += Session_OnAlert;
            this.Session.OnTorrentAddedAlert += Session_OnTorrentAddedAlert;
            this.Session.OnTorrentCheckedAlert += Session_OnTorrentCheckedAlert;
            this.Session.OnTorrentResumedAlert += Session_OnTorrentResumedAlert;
            this.Session.OnStateChangedAlert += Session_OnStateChangedAlert;
            this.Session.OnStateUpdateAlert += Session_OnStateUpdateAlert;
            this.Session.OnSaveResumeDataAlert += Session_OnSaveResumeDataAlert; ;
            this.Session.OnStatsAlert += Session_OnStatsAlert;

            using (var db = new TorLiteDatabase())
            {

                var cools = db.GetCollection<Torrent>("Torrents");
                foreach (Torrent torrent in cools.FindAll())
                {
                    MemoryStream ms = new MemoryStream();
                    MemoryStream msInfo = new MemoryStream();

                    db.FileStorage.Download(torrent.File, msInfo);
                    msInfo.Position = 0;

                    db.FileStorage.Download(torrent.ResumeData, ms);
                    ms.Position = 0;

                    var par = new AddTorrentParams { ResumeData = ms.GetBuffer(), TorrentInfo = new TorrentInfo(msInfo.GetBuffer()) };
                    this.Session.AddTorrent(par);
                }
            }
        }
 private void Session_OnSaveResumeDataAlert(object sender, SaveResumeDataAlert e)
 {
     using (var db = new TorLiteDatabase())
     {
         var cools = db.GetCollection<Torrent>("Torrents");
         var tor = cools.FindOne(x => x.Hash.Equals(e.Handle.TorrentFile.InfoHash));
         if (tor != null)
         {
             MemoryStream ms = new MemoryStream(e.ResumeData);
             ms.Position = 0;
             var fadded = db.FileStorage.Upload(Guid.NewGuid().ToString(), ms);
             if (!string.IsNullOrWhiteSpace(tor.ResumeData)) db.FileStorage.Delete(tor.ResumeData);
             tor.ResumeData = fadded.Id;
             cools.Update(tor);
         }
     }
 }
        public void Dispose()
        {
            var state = Session.SaveState();
            if (state != null && state.Length > 0)
            {
                using (var db = new TorLiteDatabase())
                {
                    var cools = db.GetCollection<SessionState>("SessionState");
                    var Sstate = cools.FindAll().FirstOrDefault();
                    var ms = new MemoryStream(state);
                    ms.Position = 0;
                    var fadded = db.FileStorage.Upload(Guid.NewGuid().ToString(), ms);
                    if (Sstate != null)
                    {
                        if (!string.IsNullOrWhiteSpace(Sstate.State)) db.FileStorage.Delete(Sstate.State);
                        Sstate.State = fadded.Id;
                        cools.Update(Sstate);
                    }
                    else cools.Insert(new SessionState { UTC_CreationDate = DateTime.UtcNow, State = fadded.Id });
                }
            }

            Session.Dispose();
        }