image-cache: migrate storage
This commit is contained in:
@@ -11,6 +11,7 @@ use hex::ToHex;
|
|||||||
use sha2::Digest;
|
use sha2::Digest;
|
||||||
use std::path;
|
use std::path;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
use tracing::warn;
|
||||||
|
|
||||||
pub type ImageCacheValue = Promise<Result<TextureHandle>>;
|
pub type ImageCacheValue = Promise<Result<TextureHandle>>;
|
||||||
pub type ImageCacheMap = HashMap<String, ImageCacheValue>;
|
pub type ImageCacheMap = HashMap<String, ImageCacheValue>;
|
||||||
@@ -78,6 +79,41 @@ impl ImageCache {
|
|||||||
.to_string()
|
.to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Migrate from base32 encoded url to sha256 url + sub-dir structure
|
||||||
|
pub fn migrate_v0(&self) -> Result<()> {
|
||||||
|
for file in std::fs::read_dir(&self.cache_dir)? {
|
||||||
|
if let Ok(file) = file {
|
||||||
|
if !file.path().is_file() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let old_filename = file.file_name().to_string_lossy().to_string();
|
||||||
|
let old_url = if let Some(u) =
|
||||||
|
base32::decode(base32::Alphabet::Crockford, &old_filename)
|
||||||
|
.and_then(|s| String::from_utf8(s).ok())
|
||||||
|
{
|
||||||
|
u
|
||||||
|
} else {
|
||||||
|
warn!("Invalid base32 filename: {}", &old_filename);
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
let new_path = self.cache_dir.join(Self::key(&old_url));
|
||||||
|
if let Some(p) = new_path.parent() {
|
||||||
|
create_dir_all(p)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Err(e) = std::fs::rename(file.path(), &new_path) {
|
||||||
|
warn!(
|
||||||
|
"Failed to migrate file from {} to {}: {:?}",
|
||||||
|
file.path().display(),
|
||||||
|
new_path.display(),
|
||||||
|
e
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
pub fn map(&self) -> &ImageCacheMap {
|
pub fn map(&self) -> &ImageCacheMap {
|
||||||
&self.url_imgs
|
&self.url_imgs
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -207,6 +207,9 @@ impl Notedeck {
|
|||||||
let tabs = Tabs::new(None);
|
let tabs = Tabs::new(None);
|
||||||
let app_rect_handler = AppSizeHandler::new(&path);
|
let app_rect_handler = AppSizeHandler::new(&path);
|
||||||
|
|
||||||
|
// migrate
|
||||||
|
img_cache.migrate_v0().expect("img-cache migration");
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
ndb,
|
ndb,
|
||||||
img_cache,
|
img_cache,
|
||||||
|
|||||||
Reference in New Issue
Block a user