From db0a3a14e697b8510f9bf0dfe33e7ba644a70201 Mon Sep 17 00:00:00 2001 From: Copilot <198982749+Copilot@users.noreply.github.com> Date: Sat, 30 May 2026 16:25:10 +0100 Subject: [PATCH] Fix downloadable file replacement cleanup, stream downloads, and preserve config keys (#3678) * Initial plan * Fix servicedownloadable file update and streaming responses * Fix CI by updating streamed download test assertions --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Adam Daley --- src/modules/Servicedownloadable/Service.php | 10 +++++----- .../modules/Servicedownloadable/ServiceTest.php | 12 +++++++++--- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/modules/Servicedownloadable/Service.php b/src/modules/Servicedownloadable/Service.php index 41b926401..32434abc6 100644 --- a/src/modules/Servicedownloadable/Service.php +++ b/src/modules/Servicedownloadable/Service.php @@ -16,6 +16,7 @@ use FOSSBilling\InjectionAwareInterface; use FOSSBilling\Tools; use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Filesystem\Path; +use Symfony\Component\HttpFoundation\BinaryFileResponse; use Symfony\Component\HttpFoundation\HeaderUtils; use Symfony\Component\HttpFoundation\Response; @@ -399,6 +400,7 @@ class Service implements InjectionAwareInterface public function updateProductFile(\Model_ServiceDownloadable $serviceDownloadable, \Model_ClientOrder $order, ?string $filename = null, ?string $storedFilename = null): bool { $request = $this->di['request']; + $oldStoredFilename = $serviceDownloadable->stored_filename ?? null; // If filename is provided, use it directly (file was already uploaded in uploadProductFile) if ($filename !== null) { @@ -416,8 +418,6 @@ class Service implements InjectionAwareInterface } $this->validateFileUpload($file); - - $oldStoredFilename = $serviceDownloadable->stored_filename ?? null; $storedFilename = $this->storeUploadedFile($file); } else { $fileName = null; @@ -495,7 +495,7 @@ class Service implements InjectionAwareInterface $serviceDownloadable->updated_at = date('Y-m-d H:i:s'); $this->di['db']->store($serviceDownloadable); - $response = new Response($this->filesystem->readFile($filePath)); + $response = new BinaryFileResponse($filePath); $disposition = $response->headers->makeDisposition( HeaderUtils::DISPOSITION_ATTACHMENT, @@ -510,7 +510,7 @@ class Service implements InjectionAwareInterface return $response; } - public function saveProductConfig(\Model_Product $productModel, $data): bool + public function saveProductConfig(\Model_Product $productModel, array $data): bool { $config = json_decode($productModel->config ?? '', true) ?: []; $config['update_orders'] = Tools::normalizeBoolean($data['update_orders'] ?? false); @@ -542,7 +542,7 @@ class Service implements InjectionAwareInterface throw new \FOSSBilling\Exception('File cannot be downloaded at the moment. Please contact support.', null, 404); } - $response = new Response($this->filesystem->readFile($filePath)); + $response = new BinaryFileResponse($filePath); $disposition = $response->headers->makeDisposition( HeaderUtils::DISPOSITION_ATTACHMENT, diff --git a/tests-legacy/modules/Servicedownloadable/ServiceTest.php b/tests-legacy/modules/Servicedownloadable/ServiceTest.php index ecfb67729..506eb3dbd 100644 --- a/tests-legacy/modules/Servicedownloadable/ServiceTest.php +++ b/tests-legacy/modules/Servicedownloadable/ServiceTest.php @@ -130,7 +130,7 @@ final class ServiceTest extends \BBTestCase $productModel = new \Model_Product(); $productModel->loadBean(new \DummyBean()); - $productModel->config = '{"filename": "test.txt"}'; + $productModel->config = '{"filename": "test.txt", "stored_filename": "stored-test.txt"}'; $dbMock = $this->createMock(\Box_Database::class); $dbMock->expects($this->atLeastOnce()) @@ -151,6 +151,7 @@ final class ServiceTest extends \BBTestCase $updatedConfig = json_decode($productModel->config, true); $this->assertIsArray($updatedConfig); $this->assertEquals('test.txt', $updatedConfig['filename']); + $this->assertEquals('stored-test.txt', $updatedConfig['stored_filename']); $this->assertTrue($updatedConfig['update_orders']); $this->assertNotNull($productModel->updated_at); } @@ -273,8 +274,13 @@ final class ServiceTest extends \BBTestCase try { $this->assertSame('PRODUCT_A_CONTENT', file_get_contents($pathA)); $this->assertSame('PRODUCT_B_CONTENT', file_get_contents($pathB)); - $this->assertSame('PRODUCT_A_CONTENT', $this->service->sendProductFile($productA)->getContent()); - $this->assertSame('PRODUCT_B_CONTENT', $this->service->sendProductFile($productB)->getContent()); + $productAResponse = $this->service->sendProductFile($productA); + $productBResponse = $this->service->sendProductFile($productB); + + $this->assertInstanceOf(\Symfony\Component\HttpFoundation\BinaryFileResponse::class, $productAResponse); + $this->assertInstanceOf(\Symfony\Component\HttpFoundation\BinaryFileResponse::class, $productBResponse); + $this->assertSame('PRODUCT_A_CONTENT', file_get_contents($productAResponse->getFile()->getPathname())); + $this->assertSame('PRODUCT_B_CONTENT', file_get_contents($productBResponse->getFile()->getPathname())); } finally { @unlink($pathA); @unlink($pathB);