2020-06-12 18:35:12 +00:00
|
|
|
use ruffle_core::backend::storage::StorageBackend;
|
|
|
|
use std::fs;
|
|
|
|
use std::fs::File;
|
2020-06-15 16:53:19 +00:00
|
|
|
use std::io::{Read, Write};
|
2020-06-15 18:42:27 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
2020-06-12 18:35:12 +00:00
|
|
|
|
|
|
|
pub struct DiskStorageBackend {
|
2020-06-15 18:42:27 +00:00
|
|
|
base_path: PathBuf,
|
2020-06-12 18:35:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DiskStorageBackend {
|
2020-12-21 16:16:44 +00:00
|
|
|
pub fn new() -> Self {
|
|
|
|
let base_path = dirs::data_local_dir().unwrap().join(Path::new("ruffle"));
|
2020-06-12 18:35:12 +00:00
|
|
|
|
|
|
|
// Create a base dir if one doesn't exist yet
|
2020-06-21 20:10:20 +00:00
|
|
|
if !base_path.exists() {
|
2020-06-12 18:35:12 +00:00
|
|
|
log::info!("Creating storage dir");
|
2020-06-15 18:42:27 +00:00
|
|
|
if let Err(r) = fs::create_dir_all(&base_path) {
|
2020-06-12 18:35:12 +00:00
|
|
|
log::warn!("Unable to create storage dir {}", r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-15 18:42:27 +00:00
|
|
|
DiskStorageBackend { base_path }
|
2020-06-12 18:35:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl StorageBackend for DiskStorageBackend {
|
2021-05-04 01:22:01 +00:00
|
|
|
fn get(&self, name: &str) -> Option<Vec<u8>> {
|
2020-06-15 18:42:27 +00:00
|
|
|
let full_path = self.base_path.join(Path::new(name));
|
2020-06-12 18:35:12 +00:00
|
|
|
|
2020-06-13 23:22:53 +00:00
|
|
|
match File::open(full_path) {
|
2020-06-12 18:35:12 +00:00
|
|
|
Ok(mut file) => {
|
2021-05-04 01:22:01 +00:00
|
|
|
let mut buffer = Vec::new();
|
|
|
|
if let Err(r) = file.read_to_end(&mut buffer) {
|
2020-06-12 18:35:12 +00:00
|
|
|
log::warn!("Unable to read file content {:?}", r);
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(buffer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(r) => {
|
|
|
|
log::warn!("Unable to open file {:?}", r);
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-04 01:22:01 +00:00
|
|
|
fn put(&mut self, name: &str, value: &[u8]) -> bool {
|
2020-06-15 18:42:27 +00:00
|
|
|
let full_path = self.base_path.join(Path::new(name));
|
2020-12-21 16:16:44 +00:00
|
|
|
if let Some(parent_dir) = full_path.parent() {
|
|
|
|
if !parent_dir.exists() {
|
|
|
|
if let Err(r) = fs::create_dir_all(&parent_dir) {
|
|
|
|
log::warn!("Unable to create storage dir {}", r);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-12 18:35:12 +00:00
|
|
|
|
2020-06-15 18:42:27 +00:00
|
|
|
match File::create(full_path) {
|
2020-06-12 18:35:12 +00:00
|
|
|
Ok(mut file) => {
|
2021-05-04 01:22:01 +00:00
|
|
|
if let Err(r) = file.write_all(&value) {
|
2020-06-15 18:18:48 +00:00
|
|
|
log::warn!("Unable to write file content {:?}", r);
|
|
|
|
false
|
|
|
|
} else {
|
|
|
|
true
|
2020-06-12 18:35:12 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-15 18:18:48 +00:00
|
|
|
Err(r) => {
|
|
|
|
log::warn!("Unable to save file {:?}", r);
|
|
|
|
false
|
2020-06-17 17:58:57 +00:00
|
|
|
}
|
2020-06-12 18:35:12 +00:00
|
|
|
}
|
2020-06-15 18:18:48 +00:00
|
|
|
}
|
|
|
|
|
2020-06-15 18:42:27 +00:00
|
|
|
fn remove_key(&mut self, name: &str) {
|
|
|
|
let full_path = self.base_path.join(Path::new(name));
|
2020-06-17 17:58:57 +00:00
|
|
|
let _ = fs::remove_file(full_path);
|
2020-06-12 18:35:12 +00:00
|
|
|
}
|
|
|
|
}
|