@@ -26,10 +26,12 @@ import (
26
26
"mime/multipart"
27
27
"net/http"
28
28
"os"
29
+ "strconv"
29
30
"strings"
30
31
31
32
"github.com/arduino/arduino-cloud-cli/config"
32
33
"github.com/arduino/arduino-cloud-cli/internal/iot"
34
+ "github.com/sirupsen/logrus"
33
35
"golang.org/x/oauth2"
34
36
)
35
37
@@ -190,13 +192,18 @@ func (c *StorageApiClient) ExportCustomTemplate(templateId string) (*string, err
190
192
}
191
193
defer res .Body .Close ()
192
194
193
- if res .StatusCode == 200 {
195
+ logrus .Debugf ("Export API call status: %d" , res .StatusCode )
196
+
197
+ if res .StatusCode == 200 || res .StatusCode == 201 {
194
198
outfile , fileExportPath , err := createNewLocalFile (templateId , res )
195
199
if err != nil {
196
200
return nil , err
197
201
}
198
202
defer outfile .Close ()
199
- io .Copy (outfile , res .Body )
203
+ _ , err = io .Copy (outfile , res .Body )
204
+ if err != nil {
205
+ return nil , err
206
+ }
200
207
return & fileExportPath , nil
201
208
} else if res .StatusCode == 400 {
202
209
bodyb , _ := io .ReadAll (res .Body )
@@ -233,19 +240,20 @@ func composeNewLocalFileName(templateId string, res *http.Response) (string, err
233
240
234
241
i := 1
235
242
for ; i < 51 ; i ++ {
236
- _ , err := os .Stat (fileExportPath )
243
+ fileE , err := os .Stat (fileExportPath )
237
244
if err != nil {
238
245
if os .IsNotExist (err ) {
239
246
break
240
- } else {
241
- newbase := strings .TrimSuffix (originalFileExportName , TemplateFileExtension )
242
- newbase = newbase + "_" + string (i ) + TemplateFileExtension
243
- fileExportPath = newbase
244
247
}
245
248
}
249
+ if fileE != nil {
250
+ newbase := strings .TrimSuffix (originalFileExportName , TemplateFileExtension )
251
+ newbase = newbase + "_" + strconv .Itoa (i ) + TemplateFileExtension
252
+ fileExportPath = newbase
253
+ }
246
254
}
247
255
if i >= 50 {
248
- return "" , errors .New ("cannot create a new file name. Max number of copy reached. " )
256
+ return "" , errors .New ("cannot create a new file name. Max number of copy reached" )
249
257
}
250
258
251
259
return fileExportPath , nil
0 commit comments