mirror of
https://github.com/InvoiceShelf/InvoiceShelf.git
synced 2026-07-17 14:25:21 +00:00
The app/Services/ directory had grown into 22 flat files at the root plus 7 uneven subdirectories — finding anything required scrolling through an alphabetical mix of small CRUD services, infrastructure drivers, and install-time utilities. This commit groups services by domain, folds Backup into a new Storage namespace, and moves framework-infrastructure and install-time helpers out of Services and into Support where they belong. New Services layout: Documents/ (Invoice, Estimate, RecurringInvoice, Payment, Expense, Transaction, DocumentItem, SerialNumber, Currency — matches the 'Documents' navigation group); Company/ (Company, Member, Invitation); Mail/ (MailConfiguration, CompanyMailConfig); Storage/ (FileDisk, plus Backup folded in). ExchangeRateProviderService moves next to its drivers in ExchangeRate/; FontService moves into Pdf/ where it belongs. CustomerService, ItemService, CustomFieldService stay at the Services root as standalone single-file domains. Moves to Support/: Hashids/ (library wrapper — not business logic); Setup/ (one-shot install-time utilities — stateless helpers); Pdf/ (ImageUtils, PdfTemplateUtils, plus the existing PdfHtmlSanitizer consolidated into the same subdir). These are all framework infrastructure and stateless utilities — the 'service' label never really fit them. Namespace declarations in 29 moved files updated to match new paths. 62 consumer files (controllers, other services, tests, database factories, seeders, routes, bootstrap/providers.php) have their use statements rewritten via a literal-string replacement script — no regex meant no risk of half-matching. Three Documents services needed an explicit 'use App\Services\Mail\CompanyMailConfigService' added because the same-namespace short reference they relied on no longer resolves after the split. Verified: composer dump-autoload, 350 tests pass (850 assertions), vendor/bin/pint clean, npm run build succeeds.
118 lines
3.5 KiB
PHP
118 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Jobs\CreateBackupJob;
|
|
use App\Rules\Backup\PathToZip;
|
|
use App\Services\Storage\BackupService;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Response;
|
|
use Spatie\Backup\BackupDestination\Backup;
|
|
use Spatie\Backup\Helpers\Format;
|
|
use Symfony\Component\HttpFoundation\StreamedResponse;
|
|
|
|
class BackupsController extends Controller
|
|
{
|
|
public function __construct(
|
|
private readonly BackupService $backupService,
|
|
) {}
|
|
|
|
public function index(Request $request): JsonResponse
|
|
{
|
|
$this->authorize('manage backups');
|
|
|
|
try {
|
|
$destination = $this->backupService->getDestination($request->file_disk_id);
|
|
|
|
$backups = $destination
|
|
->backups()
|
|
->map(function (Backup $backup) {
|
|
return [
|
|
'path' => $backup->path(),
|
|
'created_at' => $backup->date()->format('Y-m-d H:i:s'),
|
|
'size' => Format::humanReadableSize($backup->sizeInBytes()),
|
|
];
|
|
})
|
|
->toArray();
|
|
|
|
return response()->json([
|
|
'backups' => $backups,
|
|
]);
|
|
} catch (\Exception $e) {
|
|
return response()->json([
|
|
'backups' => [],
|
|
'error' => 'invalid_disk_credentials',
|
|
'error_message' => $e->getMessage(),
|
|
]);
|
|
}
|
|
}
|
|
|
|
public function store(Request $request): JsonResponse
|
|
{
|
|
$this->authorize('manage backups');
|
|
|
|
$data = $request->all();
|
|
|
|
dispatch(new CreateBackupJob($data))->onQueue(config('backup.queue.name'));
|
|
|
|
return response()->json(['success' => true]);
|
|
}
|
|
|
|
public function destroy($disk, Request $request): JsonResponse
|
|
{
|
|
$this->authorize('manage backups');
|
|
|
|
$validated = $request->validate([
|
|
'path' => ['required', new PathToZip],
|
|
]);
|
|
|
|
$destination = $this->backupService->getDestination($request->file_disk_id);
|
|
|
|
$destination
|
|
->backups()
|
|
->first(function (Backup $backup) use ($validated) {
|
|
return $backup->path() === $validated['path'];
|
|
})
|
|
->delete();
|
|
|
|
return response()->json(['success' => true]);
|
|
}
|
|
|
|
public function download(Request $request): Response|StreamedResponse
|
|
{
|
|
$this->authorize('manage backups');
|
|
|
|
$validated = $request->validate([
|
|
'path' => ['required', new PathToZip],
|
|
]);
|
|
|
|
$destination = $this->backupService->getDestination($request->file_disk_id);
|
|
|
|
$backup = $destination->backups()->first(function (Backup $backup) use ($validated) {
|
|
return $backup->path() === $validated['path'];
|
|
});
|
|
|
|
if (! $backup) {
|
|
return response('Backup not found', 422);
|
|
}
|
|
|
|
$fileName = pathinfo($backup->path(), PATHINFO_BASENAME);
|
|
|
|
return response()->stream(function () use ($backup) {
|
|
$stream = $backup->stream();
|
|
fpassthru($stream);
|
|
if (is_resource($stream)) {
|
|
fclose($stream);
|
|
}
|
|
}, 200, [
|
|
'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0',
|
|
'Content-Type' => 'application/zip',
|
|
'Content-Length' => $backup->sizeInBytes(),
|
|
'Content-Disposition' => 'attachment; filename="'.$fileName.'"',
|
|
'Pragma' => 'public',
|
|
]);
|
|
}
|
|
}
|