diff --git a/CleanPhotoCache-Windows.ps1 b/CleanPhotoCache-Windows.ps1 new file mode 100644 index 0000000..bbb556b --- /dev/null +++ b/CleanPhotoCache-Windows.ps1 @@ -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"; +} diff --git a/DBRepair-Windows.bat b/DBRepair-Windows.bat index 37eb65d..54aea20 100644 --- a/DBRepair-Windows.bat +++ b/DBRepair-Windows.bat @@ -1,7 +1,7 @@ @echo off REM PlexDBRepair.bat - Database maintenance / rebuild tool for Windows. 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 REM -- WARNNING -- WARNING -- WARNING @@ -10,6 +10,26 @@ REM This is stable working software but not "Released" software. Development wi 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 set Hour=%time:~0,2% set Min=%time:~3,2% @@ -21,14 +41,55 @@ set Hour=%Hour: =% REM ## Set TimeStamp ## set TimeStamp=%Hour%-%Min%-%Sec% -REM Find PMS database location -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" -if not exist "%PlexData%" ( - if exist "%LOCALAPPDATA%\Plex Media Server\Plug-in Support\Databases" ( - set "PlexData=%LOCALAPPDATA%\Plex Media Server\Plug-in Support\Databases" +REM Parse arguments +set PruneCache=0 +set CacheAge=30 +set SkipDBMaintenance=0 + +: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 ( - echo Could not determine Plex database path. - echo Normally %LOCALAPPDATA%\Plex Media Server\Plug-in Support\Databases + echo Could not determine Plex data directory. + 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. goto :EOF ) @@ -82,6 +143,11 @@ if %ERRORLEVEL%==0 ( exit /B 1 ) +set "CDSave=%cd%" + +if %SkipDBMaintenance% == 1 ( + goto :PruneStart +) 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. >> "%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. ====== >> "%PlexData%\PlexDBRepair.log"