Deleting and forgetting torrents

This commit is contained in:
Igor Katson 2023-11-24 21:45:38 +00:00
parent 4927850ff9
commit f789be22c9
No known key found for this signature in database
GPG key ID: B4EC22B66D61A3F5
3 changed files with 34 additions and 17 deletions

View file

@ -447,13 +447,13 @@ impl ApiInternal {
} }
fn api_torrent_action_forget(&self, idx: TorrentId) -> Result<EmptyJsonResponse> { fn api_torrent_action_forget(&self, idx: TorrentId) -> Result<EmptyJsonResponse> {
Err(ApiError::not_implemented("forgetting not implemented yet")) self.session.delete(idx, false)?;
Ok(Default::default())
} }
fn api_torrent_action_delete(&self, idx: TorrentId) -> Result<EmptyJsonResponse> { fn api_torrent_action_delete(&self, idx: TorrentId) -> Result<EmptyJsonResponse> {
Err(ApiError::not_implemented( self.session.delete(idx, true)?;
"deleting torrent not implemented yet", Ok(Default::default())
))
} }
pub async fn api_add_torrent( pub async fn api_add_torrent(

View file

@ -1,4 +1,6 @@
use std::{borrow::Cow, io::Read, net::SocketAddr, path::PathBuf, time::Duration}; use std::{
borrow::Cow, collections::HashMap, io::Read, net::SocketAddr, path::PathBuf, time::Duration,
};
use anyhow::{bail, Context}; use anyhow::{bail, Context};
use buffers::ByteString; use buffers::ByteString;
@ -26,13 +28,15 @@ pub type TorrentId = usize;
#[derive(Default)] #[derive(Default)]
pub struct SessionLocked { pub struct SessionLocked {
torrents: Vec<ManagedTorrentHandle>, next_id: usize,
torrents: HashMap<usize, ManagedTorrentHandle>,
} }
impl SessionLocked { impl SessionLocked {
fn add_torrent(&mut self, torrent: ManagedTorrentHandle) -> TorrentId { fn add_torrent(&mut self, torrent: ManagedTorrentHandle) -> TorrentId {
let idx = self.torrents.len(); let idx = self.next_id;
self.torrents.push(torrent); self.torrents.insert(idx, torrent);
self.next_id += 1;
idx idx
} }
} }
@ -205,7 +209,7 @@ impl Session {
&self, &self,
callback: impl Fn(&mut dyn Iterator<Item = (TorrentId, &ManagedTorrentHandle)>) -> R, callback: impl Fn(&mut dyn Iterator<Item = (TorrentId, &ManagedTorrentHandle)>) -> R,
) -> R { ) -> R {
callback(&mut self.locked.read().torrents.iter().enumerate()) callback(&mut self.locked.read().torrents.iter().map(|(id, t)| (*id, t)))
} }
pub async fn add_torrent( pub async fn add_torrent(
@ -404,13 +408,9 @@ impl Session {
let (managed_torrent, id) = { let (managed_torrent, id) = {
let mut g = self.locked.write(); let mut g = self.locked.write();
if let Some((id, handle)) = g if let Some((id, handle)) = g.torrents.iter().find(|(_, t)| t.info_hash() == info_hash)
.torrents
.iter()
.enumerate()
.find(|(_, t)| t.info_hash() == info_hash)
{ {
return Ok(AddTorrentResponse::AlreadyManaged(id, handle.clone())); return Ok(AddTorrentResponse::AlreadyManaged(*id, handle.clone()));
} }
let next_id = g.torrents.len(); let next_id = g.torrents.len();
let managed_torrent = builder.build(error_span!("torrent", id = next_id))?; let managed_torrent = builder.build(error_span!("torrent", id = next_id))?;
@ -430,7 +430,25 @@ impl Session {
} }
pub fn get(&self, id: TorrentId) -> Option<ManagedTorrentHandle> { pub fn get(&self, id: TorrentId) -> Option<ManagedTorrentHandle> {
self.locked.read().torrents.get(id).cloned() self.locked.read().torrents.get(&id).cloned()
}
pub fn delete(&self, id: TorrentId, delete_files: bool) -> anyhow::Result<()> {
let removed = self
.locked
.write()
.torrents
.remove(&id)
.with_context(|| format!("torrent with id {} did not exist", id))?;
if let Some(live) = removed.live() {
let _ = live.pause()?;
}
if delete_files {
bail!("torrent deleted, but deleting files not implemented")
}
Ok(())
} }
pub fn unpause(&self, handle: &ManagedTorrentHandle) -> anyhow::Result<()> { pub fn unpause(&self, handle: &ManagedTorrentHandle) -> anyhow::Result<()> {

View file

@ -1,5 +1,4 @@
import { defineConfig } from 'vite'; import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react'
// https://vitejs.dev/config/ // https://vitejs.dev/config/
export default defineConfig({ export default defineConfig({