Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions crates/backend/src/backend_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1627,6 +1627,9 @@ impl BackendState {
MessageToBackend::RequestSkinLibrary => {
SkinManager::load_skin_library(&self);
},
MessageToBackend::RemoveFromSkinLibrary { skin } => {
SkinManager::remove_skin(&self, skin);
},
MessageToBackend::AddToSkinLibrary { source } => {
let (bytes, filename) = match source {
bridge::message::UrlOrFile::Url { url } => {
Expand Down
38 changes: 37 additions & 1 deletion crates/backend/src/skin_manager.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{collections::HashMap, io::Cursor, path::Path, sync::Arc, time::SystemTime};
use std::{collections::HashMap, io::Cursor, path::{Path, PathBuf}, sync::Arc, time::SystemTime};

use bridge::message::{AccountSkinResult, BridgeDataLoadState, MessageToFrontend, SkinLibrary};
use image::DynamicImage;
Expand Down Expand Up @@ -54,6 +54,42 @@ impl SkinManager {
}
}

pub fn remove_skin(backend: &BackendState, image_bytes: Arc<[u8]>) {
let Some(skin_path) = Self::get_path_from_skin(backend, image_bytes) else {
log::warn!("Unable to find skin");
return;
};

let Ok(_) = std::fs::remove_file(skin_path) else {
log::warn!("Unable to delete skin");
return;
};

}

fn get_path_from_skin(backend: &BackendState, skin: Arc<[u8]>) -> Option<PathBuf> {
let Ok(read_dir) = std::fs::read_dir(&backend.directories.skin_library_dir) else {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Scanning the whole directory like this seems not ideal, perhaps the skin manager should maintain a map from skin -> path

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Totally agree, and had the same thought while programming this. I ended up opting for this solution as the it's a fairly low-access function and I didn't see an obvious way to implement something like a map without having to add an extra parameter to a bunch of closures. That said, I will look into something less costly.

If you have any ideas for the structure, let me know.

return None;
};

for entry in read_dir {
let Ok(entry) = entry else {
break;
};

let path = entry.path();
let Ok(bytes) = std::fs::read(&path) else {
continue;
};

if bytes == *skin {
return Some(path);
}
}

None
}

pub fn frontend_request(
backend: &BackendState,
skin_url: Arc<str>,
Expand Down
3 changes: 3 additions & 0 deletions crates/bridge/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,9 @@ pub enum MessageToBackend {
cape: Option<Uuid>,
},
RequestSkinLibrary,
RemoveFromSkinLibrary{
skin: Arc<[u8]>,
},
AddToSkinLibrary {
source: UrlOrFile,
},
Expand Down
19 changes: 19 additions & 0 deletions crates/frontend/src/pages/skins_page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,25 @@ impl Render for SkinsPage {
.when(active, |this| {
this.child(Icon::new(PandoraIcon::Flag).absolute().right(padding).bottom(padding))
})
.child(
Button::new("delete-skin").icon(PandoraIcon::Trash2)
.danger()
.compact()
.absolute()
.small()
.left(padding)
.bottom(padding)
.on_click({
let skin = skin.clone();
let skin: Arc<[u8]> = skin.into();
cx.listener(move |page, _, _, cx| {

page.data.backend_handle.send(MessageToBackend::RemoveFromSkinLibrary {
skin: { skin.clone() }
});
})
})
)
.child(skin_img)
.on_click({
let skin = skin.clone();
Expand Down