torrent/usr/local/lib/torrent-mover/archive_handler.sh
2025-02-28 10:07:04 +01:00

45 lines
1.8 KiB
Bash

#!/bin/bash
# Archive extraction handler for torrent-mover
# Improved Archive Extraction Handler
# For each archive found in the source directory, create a subdirectory in the destination
# named after the archive (without its extension) and extract into that subdirectory.
# The archive is retained in the source, so it will remain until the ratio
# limits are reached and Transmission removes the torrent data.
handle_archives() {
local src="$1" dst="$2"
find "${src}" -type f \( -iname "*.rar" -o -iname "*.zip" -o -iname "*.7z" \) | while read -r arch; do
log_info "Extracting archive: ${arch}"
local base
base=$(basename "${arch}")
local subdir="${dst}/${base%.*}"
mkdir -p "${subdir}" || { log_error "Failed to create subdirectory ${subdir}"; continue; }
# Apply proper permissions to the extraction directory
chmod 775 "${subdir}"
chown ${TORRENT_USER:-debian-transmission}:${TORRENT_GROUP:-debian-transmission} "${subdir}"
local extract_success=0
case "${arch##*.}" in
rar)
retry_command "unrar x -o- \"${arch}\" \"${subdir}\"" 3 10
extract_success=$?
;;
zip)
retry_command "unzip -o \"${arch}\" -d \"${subdir}\"" 3 10
extract_success=$?
;;
7z)
retry_command "7z x \"${arch}\" -o\"${subdir}\"" 3 10
extract_success=$?
;;
esac
if [ $extract_success -eq 0 ]; then
log_info "Archive ${arch} extracted successfully to ${subdir}"
log_info "Archive ${arch} retained in source until ratio limits are reached."
else
log_error "Failed to extract archive ${arch}"
fi
done
}