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(); }
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); } } }
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); } } }