Add PhotoTranscoder cleanup to Windows script

This commit is contained in:
danrahn
2024-05-05 17:57:54 -07:00
parent d0084c96fb
commit 690e89cf57
2 changed files with 156 additions and 8 deletions

View File

@@ -0,0 +1,33 @@
param(
[Parameter(Mandatory)]
[String]$cacheDir,
[Int32]$cacheAge=30
);
$cutoff = (Get-Date).AddDays(-$cacheAge);
$allFiles = 0;
$oldFiles = 0;
$freedBytes = 0;
Get-ChildItem -Path $cacheDir -Recurse -File |
where { $_.extension -in '.jpg','.jpeg','.png','.ppm' } |
ForEach {
$allFiles++;
if ($_.LastWriteTime -lt $cutoff) {
$oldFiles++;
$freedBytes += $_.Length;
Remove-Item $_.FullName;
}
};
Write-Output $allFiles;
Write-Output $oldFiles;
if ($freedBytes -gt 1GB) {
Write-Output "$([math]::round($freedBytes / 1GB, 2)) GiB";
} elseif ($freedBytes -gt 1MB) {
Write-Output "$([math]::round($freedBytes / 1MB, 2)) MiB";
} elseif ($freedBytes -gt 1KB) {
Write-Output "$([math]::round($freedBytes / 1KB, 2)) KiB";
} else {
Write-Output "$($freedBytes) bytes";
}

View File

@@ -1,7 +1,7 @@
@echo off @echo off
REM PlexDBRepair.bat - Database maintenance / rebuild tool for Windows. REM PlexDBRepair.bat - Database maintenance / rebuild tool for Windows.
REM REM
REM This tool currently works as a "full shot" service. REM This tool currently works as a "full shot" service, outside of PhotoTranscoder pruning.
REM - everything is done without need to interact. REM - everything is done without need to interact.
REM REM
REM -- WARNNING -- WARNING -- WARNING REM -- WARNNING -- WARNING -- WARNING
@@ -10,6 +10,26 @@ REM This is stable working software but not "Released" software. Development wi
setlocal enabledelayedexpansion setlocal enabledelayedexpansion
goto :Begin
:Help
echo PlexDBRepair.bat - Database maintenance / rebuild tool for Windows.
echo.
echo Usage: PlexDBRepair.bat [OPTION]...
echo.
echo -prune Prune (remove) old image files (jpeg,jpg,png) from PhotoTranscoder
echo in addition to standard database maintenance tasks.
echo -cacheAge [age] Set the date cutoff for pruned images. Defaults to pruning images
echo over 30 days old.
echo -skipDB Don't perform database maintenance.
echo.
echo -- WARNNING -- WARNING -- WARNING
echo.
echo This is stable working software but not "Released" software. Development will continue.
goto :EOF
:Begin
REM ### Create Timestamp REM ### Create Timestamp
set Hour=%time:~0,2% set Hour=%time:~0,2%
set Min=%time:~3,2% set Min=%time:~3,2%
@@ -21,14 +41,55 @@ set Hour=%Hour: =%
REM ## Set TimeStamp ## REM ## Set TimeStamp ##
set TimeStamp=%Hour%-%Min%-%Sec% set TimeStamp=%Hour%-%Min%-%Sec%
REM Find PMS database location REM Parse arguments
for /F "tokens=2* skip=2" %%a in ('REG.EXE QUERY "HKCU\Software\Plex, Inc.\Plex Media Server" /v "LocalAppDataPath" 2^> nul') do set "PlexData=%%b\Plex Media Server\Plug-in Support\Databases" set PruneCache=0
if not exist "%PlexData%" ( set CacheAge=30
if exist "%LOCALAPPDATA%\Plex Media Server\Plug-in Support\Databases" ( set SkipDBMaintenance=0
set "PlexData=%LOCALAPPDATA%\Plex Media Server\Plug-in Support\Databases"
:ParseArgs
if "%1" == "" goto :ParseArgsDone
for %%a in (prune -prune) do if /i "%1" == "%%a" ( set PruneCache=1 ) & shift & goto :ParseArgs
for %%a in (cacheage -cacheage) do if /i "%1" == "%%a" ( set "CacheAge=%2" & shift ) & shift & goto :ParseArgs
for %%a in (skipdb -skipdb) do if /i "%1" == "%%a" ( set SkipDBMaintenance=1 ) & shift & goto :ParseArgs
for %%a in (help -help /? -?) do if /i "%1" == "%%a" goto :Help
:ParseArgsDone
if not "%1" == "" (
echo Unknown option "%1"
echo.
goto :Help
)
REM Find PMS data directory
for /F "tokens=2* skip=2" %%a in ('REG.EXE QUERY "HKCU\Software\Plex, Inc.\Plex Media Server" /v "LocalAppDataPath" 2^> nul') do set "AppSupDir=%%b\Plex Media Server"
if not exist "%AppSupDir%" (
if exist "%LOCALAPPDATA%\Plex Media Server" (
set "%AppSupDir%=%LOCALAPPDATA%\Plex Media Server"
) else ( ) else (
echo Could not determine Plex database path. echo Could not determine Plex data directory.
echo Normally %LOCALAPPDATA%\Plex Media Server\Plug-in Support\Databases echo Normally "%LOCALAPPDATA%\Plex Media Server"
echo.
goto :EOF
)
)
REM Find PMS database location
set "PlexData=%AppSupDir%\Plug-in Support\Databases"
if not exist "%PlexData%" (
echo Could not determine Plex database path.
echo Normally %AppSupDir%\Plug-in Support\Databases
echo.
goto :EOF
)
REM Find PMS cache dir
set "PlexCache=%AppSupDir%\Cache\PhotoTranscoder"
if %PruneCache% == 1 (
if not exist "%PlexCache%" (
echo Could not determine Plex photo cache path, cannot prune images.
echo Normally "%PlexCache%"
echo. echo.
goto :EOF goto :EOF
) )
@@ -82,6 +143,11 @@ if %ERRORLEVEL%==0 (
exit /B 1 exit /B 1
) )
set "CDSave=%cd%"
if %SkipDBMaintenance% == 1 (
goto :PruneStart
)
cd "%PlexData%" cd "%PlexData%"
@@ -183,6 +249,55 @@ move "%PlexData%\com.plexapp.plugins.library.blobs.db_%TimeStamp%" "%PlexData%\c
echo %time% -- Database repair/rebuild/reindex completed. echo %time% -- Database repair/rebuild/reindex completed.
echo %time% -- Database repair/rebuild/reindex completed. >> "%PlexData%\PlexDBRepair.log" echo %time% -- Database repair/rebuild/reindex completed. >> "%PlexData%\PlexDBRepair.log"
:PruneStart
if %PruneCache% neq 1 (
goto :Done
)
REM Restore working directory
cd %CDSave%
echo %time% -- Prune - START
echo %time% -- Prune - START >> "%PlexData%\PlexDBRepair.log"
REM Validate cache age parameter
echo %CacheAge%|findstr /r /c:"^[1-9][0-9]*$" >nul
if errorlevel 1 (
goto :PruneAgeError
) else (
set /a "checkZero=%CacheAge%"
if "%checkZero%" == 0 (
goto :PruneAgeError
)
)
goto :Prune
:PruneAgeError
echo %time% -- Prune - ERROR: Cache age %CacheAge% is not a valid value
echo %time% -- Prune - ERROR: Cache age %CacheAge% is not a valid value >> "%PlexData%\PlexDBRepair.log"
goto :Done
:Prune
set i=0
for /f "delims=" %%a in ('powershell .\CleanPhotoCache-Windows.ps1 -cacheDir '%PlexCache%' -cacheAge %CacheAge%') do (
set PSReturn[!i!]=%%a
set /a i += 1
)
set TotalFiles=%PSReturn[0]%
set DeletedFiles=%PSReturn[1]%
set FreedBytes=%PSReturn[2]%
echo %time% -- Prune - Removing %DeletedFiles% files over %CacheAge% days old (out of %TotalFiles% files), freeing %FreedBytes%
echo %time% -- Prune - Removing %DeletedFiles% files over %CacheAge% days old (out of %TotalFiles% files), freeing %FreedBytes% >> "%PlexData%\PlexDBRepair.log"
echo %time% -- Prune - PASS
echo %time% -- Prune - PASS >> "%PlexData%\PlexDBRepair.log"
:Done
echo %time% -- ====== Session completed. ====== echo %time% -- ====== Session completed. ======
echo %time% -- ====== Session completed. ====== >> "%PlexData%\PlexDBRepair.log" echo %time% -- ====== Session completed. ====== >> "%PlexData%\PlexDBRepair.log"