feat(user/bk-copy-rand-music):Restrict filenames of copied files
authorSteven Baltakatei Sandoval <baltakatei@gmail.com>
Sat, 20 May 2023 22:23:19 +0000 (22:23 +0000)
committerSteven Baltakatei Sandoval <baltakatei@gmail.com>
Sat, 20 May 2023 22:23:19 +0000 (22:23 +0000)
- Note: Using `tr -dc '[:graph:][:space:]'` as character filter.

user/bk-copy-rand-music

index bc21e5b93215defa013c1d8efe707c3c90cfc1a1..b807286e8b75c30042e0283f59ed7eba51e324ba 100644 (file)
@@ -1,7 +1,7 @@
 #!/usr/bin/env bash
 # Desc: Copies random audio files
 # Usage: bk-copy-rand-music [dir SOURCE] [dir DEST] [int DURATION] ([int BYTES])
-# Version: 0.1.1
+# Version: 0.2.0
 # Depends: BK-2020-03: bkshuf v0.1.0
 
 declare -Ag appRollCall # Associative array for storing app status
@@ -516,13 +516,15 @@ main() {
         fpath="$(printf "%s" "$line" | cut -d',' -f3-)";
         ## Get basename of path
         file_basename="$(basename "$fpath")";
+        ### Get basename without unprintable non-ASCII characters
+        file_basename_compat="$(printf "%s" "$file_basename" | tr -dc '[:graph:][:space:]' )";
 
         ## Get 16-character b2sum fingerprint (for different files that share basename)
        fingerprint="$(b2sum -l32 "$fpath" | awk '{print $1}' )";
 
         ## Form output filename
         num="$(printf "$num_fmt" "$n")";
-       file_name="$num"_"$fingerprint".."$file_basename";
+       file_name="$num"_"$fingerprint".."$file_basename_compat";
        file_name="${file_name:0:$max_filename_length}"; # Limit filename length (e.g. Windows has max of 255 characters)
 
        ## Form output path
@@ -538,7 +540,7 @@ main() {
         printf "$log_fmt" "$num" "$fingerprint" "$fdur" "$fsize" "$fpath_can" >> "$path_log_output";
 
        ((n++));
-       unset file_basename path_output
+       unset file_basename file_basename_compat path_output;
     done < <(printf "%s\n" "${list_copy[@]}");
 
     # Report total duration and size