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 <admdly@users.noreply.github.com>
This commit is contained in:
CopilotandAdam Daley authored and GitHub committed 2026-05-30 16:25:10 +01:00
1 parent 5aa1834445
commit db0a3a14e6
2 files changed
+14 -8

No files matched your search

+5 -5
View File
@@ -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,
@@ -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);