44
55use App \Models \WorkflowNamespace ;
66use App \Support \ControlPlaneProtocol ;
7+ use App \Support \NamespaceExternalPayloadStorage ;
78use Illuminate \Http \JsonResponse ;
89use Illuminate \Http \Request ;
9- use Illuminate \Support \Facades \File ;
10- use Illuminate \Support \Str ;
1110use Illuminate \Validation \Rule ;
11+ use Workflow \V2 \Contracts \ExternalPayloadStorageDriver ;
1212
1313class StorageController
1414{
15+ public function __construct (
16+ private readonly NamespaceExternalPayloadStorage $ externalPayloadStorage ,
17+ ) {}
18+
1519 public function test (Request $ request ): JsonResponse
1620 {
1721 if ($ response = ControlPlaneProtocol::rejectUnsupported ($ request )) {
@@ -37,24 +41,34 @@ public function test(Request $request): JsonResponse
3741 return $ this ->diagnosticError ('external_storage_disabled ' , 'External payload storage is disabled for this namespace. ' , $ namespace , $ driver );
3842 }
3943
40- if ($ driver !== ' local ' ) {
44+ if ($ driver !== ( $ policy [ ' driver ' ] ?? null ) ) {
4145 return $ this ->diagnosticError (
4246 'storage_driver_unavailable ' ,
43- 'The server can persist this storage policy, but only the local diagnostic driver can run a round-trip test in this release . ' ,
47+ 'The requested external payload storage driver is not configured for this namespace . ' ,
4448 $ namespace ,
4549 $ driver ,
4650 ['supported_diagnostic_drivers ' => ['local ' ]],
4751 );
4852 }
4953
50- $ directory = $ this ->localDirectory ($ policy , $ namespace );
54+ $ storage = $ this ->externalPayloadStorage ->driverFor ($ namespace );
55+
56+ if ($ storage === null ) {
57+ return $ this ->diagnosticError (
58+ 'storage_driver_unavailable ' ,
59+ 'The server can persist this storage policy, but the configured storage driver is not available in this runtime. ' ,
60+ $ namespace ,
61+ $ driver ,
62+ ['supported_diagnostic_drivers ' => ['local ' , 's3 ' , 'gcs ' , 'azure ' ]],
63+ );
64+ }
5165
5266 return ControlPlaneProtocol::json ([
5367 'status ' => 'passed ' ,
5468 'namespace ' => $ namespace ,
5569 'driver ' => $ driver ,
56- 'small_payload ' => $ this ->roundTrip ($ directory , 'small ' , (int ) $ validated ['small_payload_bytes ' ]),
57- 'large_payload ' => $ this ->roundTrip ($ directory , 'large ' , (int ) $ validated ['large_payload_bytes ' ]),
70+ 'small_payload ' => $ this ->roundTrip ($ storage , 'small ' , (int ) $ validated ['small_payload_bytes ' ]),
71+ 'large_payload ' => $ this ->roundTrip ($ storage , 'large ' , (int ) $ validated ['large_payload_bytes ' ]),
5872 ]);
5973 }
6074
@@ -74,27 +88,13 @@ private function diagnosticError(
7488 ] + $ extra , 422 );
7589 }
7690
77- private function localDirectory ( array $ policy , string $ namespace ): string
91+ private function roundTrip ( ExternalPayloadStorageDriver $ storage , string $ kind , int $ bytes ): array
7892 {
79- $ uri = $ policy ['config ' ]['uri ' ] ?? null ;
80- if (is_string ($ uri ) && str_starts_with ($ uri , 'file:// ' )) {
81- return rtrim (substr ($ uri , 7 ), '/ ' );
82- }
83-
84- return storage_path ('app/external-payloads/ ' .$ namespace );
85- }
86-
87- private function roundTrip (string $ directory , string $ kind , int $ bytes ): array
88- {
89- File::ensureDirectoryExists ($ directory );
90-
91- $ path = $ directory .'/storage-test- ' .$ kind .'- ' .(string ) Str::uuid ().'.bin ' ;
9293 $ payload = str_repeat ($ kind === 'small ' ? 's ' : 'l ' , $ bytes );
9394 $ expectedHash = hash ('sha256 ' , $ payload );
94-
95- file_put_contents ($ path , $ payload );
96- $ read = file_get_contents ($ path );
97- @unlink ($ path );
95+ $ uri = $ storage ->put ($ payload , $ expectedHash , 'storage-test- ' .$ kind );
96+ $ read = $ storage ->get ($ uri );
97+ $ storage ->delete ($ uri );
9898
9999 if ($ read !== $ payload ) {
100100 throw new \RuntimeException ('External storage round trip failed integrity verification. ' );
@@ -104,7 +104,7 @@ private function roundTrip(string $directory, string $kind, int $bytes): array
104104 'status ' => 'passed ' ,
105105 'bytes ' => $ bytes ,
106106 'sha256 ' => $ expectedHash ,
107- 'reference_uri ' => ' file:// ' . $ path ,
107+ 'reference_uri ' => $ uri ,
108108 ];
109109 }
110110}
0 commit comments