@@ -40,6 +40,28 @@ public function handle()
40
40
return static ::FAILURE ;
41
41
}
42
42
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
+
43
65
if ($ this ->option ('fetch ' )) {
44
66
if (! $ this ->fetchLatestBundle ()) {
45
67
$ this ->warn ('Latest bundle not yet available. Try again soon. ' );
@@ -58,11 +80,19 @@ public function handle()
58
80
59
81
return static ::FAILURE ;
60
82
}
83
+ // $this->zipName = 'app_CcINfsoQ.zip';
84
+ // $this->zipPath = base_path('temp/'.$this->zipName);
61
85
62
86
// Send the zip file
63
- dd ( $ result = $ this ->sendToZephpyr () );
87
+ $ result = $ this ->sendToZephpyr ();
64
88
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 ()) {
66
96
$ this ->error ("Failed to upload zip [ {$ this ->zipPath }] to Zephpyr. " );
67
97
68
98
return static ::FAILURE ;
@@ -79,7 +109,12 @@ public function handle()
79
109
private function zipApplication (): bool
80
110
{
81
111
$ 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
+ }
83
118
84
119
$ zip = new ZipArchive ;
85
120
@@ -100,8 +135,10 @@ private function zipApplication(): bool
100
135
101
136
private function addFilesToZip (ZipArchive $ zip ): void
102
137
{
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… ' );
105
142
106
143
$ app = (new Finder )->files ()
107
144
->followLinks ()
@@ -144,17 +181,36 @@ private function finderToZip(Finder $finder, ZipArchive $zip, ?string $path = nu
144
181
}
145
182
}
146
183
184
+ private function baseUrl (): string
185
+ {
186
+ return str (config ('nativephp-internal.zephpyr.host ' ))->finish ('/ ' );
187
+ }
188
+
147
189
private function sendToZephpyr ()
148
190
{
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 ' ))
150
196
->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 ' );
152
207
}
153
208
154
209
private function fetchLatestBundle (): bool
155
210
{
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 );
158
214
159
215
if ($ response ->failed ()) {
160
216
return false ;
0 commit comments