tikz-gallery-generator

Custum build of stapix for tikz.pablopie.xyz

Commit
e3ce3993efa127e60197031b60a3455202502336
Parent
9983aa825f36680ea6f3a3dde1dbe25e604a7b15
Author
Pablo <pablo-pie@riseup.net>
Date

Replaced symlinks by copying

Made it so that the app copies the SVG to the desired folders instead of symlinking them

This will make it easier to deploy the websites using rsync

Diffstats

1 files changed, 13 insertions, 33 deletions

Status Name Changes Insertions Deletions
Modified src/main.rs 2 files changed 13 33
diff --git a/src/main.rs b/src/main.rs
@@ -9,7 +9,6 @@ use std::{
   path::{Path, PathBuf},
   process::{ExitCode, Command},
   sync::mpsc,
-  os::unix,
 };
 use gallery_entry::{GalleryEntry, FileFormat, LicenseType};
 use threadpool::ThreadPool;
@@ -107,13 +106,7 @@ fn render_gallery(
     target_path.push(IMAGES_PATH);
     target_path.push(&pic.file_name);
 
-    if let Err(e) = fs::copy(&pic.path, &target_path) {
-      errorln!(
-        "Couldn't copy file {src:?} to {target_path:?}: {e}",
-        src = pic.path,
-      );
-      return Err(());
-    }
+    copy(&pic.path, &target_path)?;
   }
 
   info_done!();
@@ -129,7 +122,7 @@ fn render_gallery(
   }
 
   // ========================================================================
-  let num_threads = min(num_cpus::get() + 1, pics.len());
+  let num_threads = min(num_cpus::get() - 1, pics.len());
   let rendering_pool = ThreadPool::with_name(
     String::from("thumbnails renderer"),
     num_threads
@@ -138,11 +131,9 @@ fn render_gallery(
 
   infoln!( "Started generating thumbnails (using {num_threads} threads)");
 
+  let mut render_pool_count = 0;
   for pic in &pics {
     let thumb_path = thumb_path(pic);
-
-    // Here we do not want to call fs::symlink_metada: we want to know when was
-    // the symlink last updated
     let thumb_meta = fs::metadata(&thumb_path);
 
     if !full_build {
@@ -157,6 +148,8 @@ fn render_gallery(
       }
     }
 
+    render_pool_count  += 1;
+
     let sender = sender.clone();
     let pic = pic.clone();
 
@@ -166,7 +159,7 @@ fn render_gallery(
       });
   }
 
-  for _ in 0..pics.len() {
+  for _ in 0..render_pool_count {
     let msg = reciever.recv();
     if msg.is_err() {
       // propagate the panic to the main thread: reciever.recv should
@@ -467,26 +460,7 @@ fn render_thumbnail(pic: GalleryEntry, thumb_path: &Path) -> Result<(), ()> {
       src_path.push(IMAGES_PATH);
       src_path.push(&pic.file_name);
 
-      // here we need the absolute path of the image to prevent issues
-      // with symlinks
-      let src_path = fs::canonicalize(&src_path)
-        .map_err(|e| {
-          errorln!(
-            "Failed to create symlink for {thumb_path:?}: Could not get absolute path of {src_path:?}: {e}"
-          );
-        })?;
-
-      // TODO: copy the file instead of using a symlink!
-      // delete the thumbnail file if it exists already: fs::symlink does
-      // not override files
-      if thumb_path.exists() { let _ = fs::remove_file(thumb_path); }
-
-      if let Err(e) = unix::fs::symlink(&src_path, thumb_path) {
-        errorln!(
-          "Failed to create symlink {thumb_path:?} -> {src_path:?}: {e}"
-        );
-        return Err(());
-      }
+      copy(&src_path, thumb_path)?;
     },
     FileFormat::Jpeg | FileFormat::Png => {
       let mut thumb_file = create_file(thumb_path).map_err(|_| ())?;
@@ -555,6 +529,12 @@ fn create_file(path: &Path) -> io::Result {
     .map_err(|e| { errorln!("Could not open file {path:?}: {e}"); e })
 }
 
+fn copy(from: &Path, to: &Path) -> Result<(), ()> {
+  fs::copy(from, to)
+    .map(|_| ())
+    .map_err(|e| errorln!("Failed to copy {from:?} to {to:?}: {e}"))
+}
+
 impl Display for ThumbPath<'_> {
   fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
     write!(f, "/{THUMBS_PATH}/{name}", name = Escaped(&self.0.file_name))?;