[ENG-1220] Fix pdf thumb crashing app (#1460)

Fix pdf thumb crashing app
This commit is contained in:
Vítor Vasconcellos 2023-10-10 21:47:36 -03:00 committed by GitHub
parent 811faa22a0
commit 6fd8bd4ad8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 54 deletions

View file

@ -31,8 +31,4 @@ tracing = { workspace = true }
# this broke builds as we build our own liibheif, so i disabled their default features
libheif-rs = { version = "0.22.0", default-features = false, optional = true }
libheif-sys = { version = "2.0.0", default-features = false, optional = true }
pdfium-render = { version = "0.8.8", features = [
"sync",
"image",
"thread_safe",
] }
pdfium-render = { version = "0.8.8", features = ["image"] }

View file

@ -6,7 +6,6 @@ use std::{
use crate::{consts::PDF_RENDER_WIDTH, Error::PdfiumBinding, ImageHandler, Result};
use image::DynamicImage;
use once_cell::sync::Lazy;
use pdfium_render::prelude::{PdfPageRenderRotation, PdfRenderConfig, Pdfium};
use tracing::error;
@ -20,7 +19,9 @@ const BINDING_LOCATION: &str = if cfg!(target_os = "macos") {
"../lib/spacedrive"
};
static PDFIUM: Lazy<Option<Pdfium>> = Lazy::new(|| {
// FIX-ME: This is slow, but using Lazy with thread_safe was causing concurrency bugs that crashed the app
thread_local! {
static PDFIUM: Option<Pdfium> = {
let lib_name = Pdfium::pdfium_platform_library_name();
let lib_path = current_exe()
.ok()
@ -56,13 +57,15 @@ static PDFIUM: Lazy<Option<Pdfium>> = Lazy::new(|| {
.map(Pdfium::new)
.map_err(|err| error!("{err:#?}"))
.ok()
});
};
}
pub struct PdfHandler {}
impl ImageHandler for PdfHandler {
fn handle_image(&self, path: &Path) -> Result<DynamicImage> {
let pdfium = PDFIUM.as_ref().ok_or(PdfiumBinding)?;
PDFIUM.with(|maybe_pdfium| {
let pdfium = maybe_pdfium.as_ref().ok_or(PdfiumBinding)?;
let render_config = PdfRenderConfig::new()
.set_target_width(PDF_RENDER_WIDTH)
@ -74,5 +77,6 @@ impl ImageHandler for PdfHandler {
.first()?
.render_with_config(&render_config)?
.as_image())
})
}
}