Files
InvoiceShelf/app/Http/Controllers/V1/Admin/Update/DeleteFilesController.php
Darko Gjorgjijoski c1cadb7ee0 fix(updater): manifest-based stale-file cleanup + cache clearing for v2→v3 (#659)
The v2 self-updater only overlays new files (copyFiles) and never removes
files a release deleted; the one removal path (deleted_files) is not even
sent by the web UI. A major upgrade (v2 → v3) removes thousands of files,
so overlay-only leaves a broken hybrid, and stale bootstrap/cache config +
package-discovery survive and break the new boot.

Backport v3's manifest allow-list approach into this final v2 release:

- Updater::cleanStaleFiles(?string $basePath): delete every file under the
  install not listed in the release's manifest.json, except the configured
  update_protected_paths. No manifest present → safe no-op (v2→v2 updates).
- Updater::clearCompiledCaches(): wipe bootstrap/cache/*.php and compiled
  views so the freshly copied release re-reads config and re-runs package
  discovery. Called at the end of copyFiles() — the last point that runs as
  the currently-installed code before the new release boots, and necessary
  because bootstrap/cache is itself a protected path.
- DeleteFilesController + UpdateCommand: when manifest.json is present, run
  cleanStaleFiles(); otherwise fall back to the legacy deleted_files list.
  No route or frontend change — both already call the delete step between
  copy and migrate.
- config/invoiceshelf.php: add update_protected_paths (.env, storage,
  vendor, node_modules, Modules, public/storage, .git, bootstrap/cache,
  manifest.json).

The v3 release zip already ships manifest.json (built by its make dist), so
a v2 instance running this updater cleans itself up correctly on upgrade.

Tested: tests/Unit/UpdaterTest.php covers stale removal, protected-path and
manifest preservation, empty-dir pruning, no-manifest no-op, and invalid
manifest. Full suite green.
2026-06-12 09:10:49 +02:00

41 lines
1.2 KiB
PHP

<?php
namespace App\Http\Controllers\V1\Admin\Update;
use App\Http\Controllers\Controller;
use App\Space\Updater;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\File;
class DeleteFilesController extends Controller
{
/**
* Handle the incoming request.
*
* @return Response
*/
public function __invoke(Request $request)
{
if ((! $request->user()) || (! $request->user()->isOwner())) {
return response()->json([
'success' => false,
'message' => 'You are not allowed to update this app.',
], 401);
}
// Backward compatibility: use the explicit deleted_files list only when the
// release ships no manifest.json (same-line v2 updates). When a manifest is
// present (e.g. the v3 release), clean every stale file not in the manifest.
if (! File::exists(base_path('manifest.json'))
&& isset($request->deleted_files)
&& ! empty($request->deleted_files)) {
Updater::deleteFiles($request->deleted_files);
return response()->json(['success' => true, 'cleaned' => 0]);
}
return response()->json(Updater::cleanStaleFiles());
}
}