@@ -40,6 +40,28 @@ public function handle()
4040 return static ::FAILURE ;
4141 }
4242
43+ if (! config ('nativephp-internal.zephpyr.token ' )) {
44+ $ this ->line ('' );
45+ $ this ->warn ('No ZEPHPYR_TOKEN found. Cannot bundle! ' );
46+ $ this ->line ('' );
47+ $ this ->line ('Add your api ZEPHPYR_TOKEN to its .env file: ' );
48+ $ this ->line (base_path ('.env ' ));
49+ $ this ->line ('' );
50+ $ this ->info ('Not set up with Zephpyr yet? Secure your NativePHP app builds and more! ' );
51+ $ this ->info ('Check out https://zephpyr.com ' );
52+ $ this ->line ('' );
53+
54+ return static ::FAILURE ;
55+ }
56+
57+ $ result = $ this ->checkAuthenticated ();
58+
59+ if ($ result ->failed ()) {
60+ $ this ->error ('Invalid API token: check your ZEPHPYR_TOKEN on https://zephpyr.com/user/api-tokens ' );
61+
62+ return ;
63+ }
64+
4365 if ($ this ->option ('fetch ' )) {
4466 if (! $ this ->fetchLatestBundle ()) {
4567 $ this ->warn ('Latest bundle not yet available. Try again soon. ' );
@@ -58,11 +80,19 @@ public function handle()
5880
5981 return static ::FAILURE ;
6082 }
83+ // $this->zipName = 'app_CcINfsoQ.zip';
84+ // $this->zipPath = base_path('temp/'.$this->zipName);
6185
6286 // Send the zip file
63- dd ( $ result = $ this ->sendToZephpyr () );
87+ $ result = $ this ->sendToZephpyr ();
6488
65- if ($ result ->failed ()) {
89+ //dd($result->status(), json_decode($result->body()));
90+
91+ if ($ result ->code () === 413 ) {
92+ $ this ->error ('The zip file is too large to upload to Zephpyr. Please contact support. ' );
93+
94+ return static ::FAILURE ;
95+ } elseif ($ result ->failed ()) {
6696 $ this ->error ("Failed to upload zip [ {$ this ->zipPath }] to Zephpyr. " );
6797
6898 return static ::FAILURE ;
@@ -79,7 +109,12 @@ public function handle()
79109 private function zipApplication (): bool
80110 {
81111 $ this ->zipName = 'app_ ' .str ()->random (8 ).'.zip ' ;
82- $ this ->zipPath = storage_path ($ this ->zipName );
112+ $ this ->zipPath = base_path ('temp/ ' .$ this ->zipName );
113+
114+ // Create zip path
115+ if (! @mkdir (dirname ($ this ->zipPath ), recursive: true ) && ! is_dir (dirname ($ this ->zipPath ))) {
116+ return false ;
117+ }
83118
84119 $ zip = new ZipArchive ;
85120
@@ -100,8 +135,10 @@ private function zipApplication(): bool
100135
101136 private function addFilesToZip (ZipArchive $ zip ): void
102137 {
103- // TODO: Check the composer.json to make sure there are no symlinked or private packages as these will be a
104- // pain later
138+ // TODO: Check the composer.json to make sure there are no symlinked
139+ // or private packages as these will be a pain later
140+
141+ $ this ->line ('Adding files to zip… ' );
105142
106143 $ app = (new Finder )->files ()
107144 ->followLinks ()
@@ -144,17 +181,36 @@ private function finderToZip(Finder $finder, ZipArchive $zip, ?string $path = nu
144181 }
145182 }
146183
184+ private function baseUrl (): string
185+ {
186+ return str (config ('nativephp-internal.zephpyr.host ' ))->finish ('/ ' );
187+ }
188+
147189 private function sendToZephpyr ()
148190 {
149- return Http::withToken (config ('nativephp-internal.zephpyr.token ' ))
191+ $ this ->line ('Uploading to Zephpyr… ' );
192+
193+ return Http::acceptJson ()
194+ ->withoutRedirecting () // Upload won't work if we follow the redirect
195+ ->withToken (config ('nativephp-internal.zephpyr.token ' ))
150196 ->attach ('archive ' , fopen ($ this ->zipPath , 'r ' ), $ this ->zipName )
151- ->post (str (config ('nativephp-internal.zephpyr.host ' ))->finish ('/ ' ).'api/build/ ' .$ this ->key );
197+ ->post ($ this ->baseUrl ().'api/build/ ' .$ this ->key );
198+ }
199+
200+ private function checkAuthenticated ()
201+ {
202+ $ this ->line ('Checking authentication… ' );
203+
204+ return Http::acceptJson ()
205+ ->withToken (config ('nativephp-internal.zephpyr.token ' ))
206+ ->get ($ this ->baseUrl ().'api/user ' );
152207 }
153208
154209 private function fetchLatestBundle (): bool
155210 {
156- $ response = Http::withToken (config ('nativephp-internal.zephpyr.token ' ))
157- ->get (str (config ('nativephp-internal.zephpyr.host ' ))->finish ('/ ' ).'api/download/ ' .$ this ->key );
211+ $ response = Http::acceptJson ()
212+ ->withToken (config ('nativephp-internal.zephpyr.token ' ))
213+ ->get ($ this ->baseUrl ().'api/download/ ' .$ this ->key );
158214
159215 if ($ response ->failed ()) {
160216 return false ;
0 commit comments