Skip to content

Commit d54f101

Browse files
committed
Extract function to compute index file name
1 parent 433e7b3 commit d54f101

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

Diff for: arduino/resources/index.go

+15-7
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ type IndexResource struct {
3737
SignatureURL *url.URL
3838
}
3939

40+
// IndexFileName returns the index file name as it is saved in data dir (package_xxx_index.json).
41+
func (res *IndexResource) IndexFileName() string {
42+
filename := path.Base(res.URL.Path) // == package_index.json[.gz] || packacge_index.tar.bz2
43+
if i := strings.Index(filename, "."); i != -1 {
44+
filename = filename[:i]
45+
}
46+
return filename + ".json"
47+
}
48+
4049
// Download will download the index and possibly check the signature using the Arduino's public key.
4150
// If the file is in .gz format it will be unpacked first.
4251
func (res *IndexResource) Download(destDir *paths.Path, downloadCB rpc.DownloadProgressCB) error {
@@ -53,18 +62,18 @@ func (res *IndexResource) Download(destDir *paths.Path, downloadCB rpc.DownloadP
5362
defer tmp.RemoveAll()
5463

5564
// Download index file
56-
indexFileName := path.Base(res.URL.Path) // == package_index.json[.gz]
57-
tmpIndexPath := tmp.Join(indexFileName)
58-
if err := httpclient.DownloadFile(tmpIndexPath, res.URL.String(), "", tr("Downloading index: %s", indexFileName), downloadCB, nil, downloader.NoResume); err != nil {
65+
downloadFileName := path.Base(res.URL.Path) // == package_index.json[.gz] || package_index.tar.bz2
66+
indexFileName := res.IndexFileName() // == package_index.json
67+
tmpIndexPath := tmp.Join(downloadFileName)
68+
if err := httpclient.DownloadFile(tmpIndexPath, res.URL.String(), "", tr("Downloading index: %s", downloadFileName), downloadCB, nil, downloader.NoResume); err != nil {
5969
return &arduino.FailedDownloadError{Message: tr("Error downloading index '%s'", res.URL), Cause: err}
6070
}
6171

6272
var signaturePath, tmpSignaturePath *paths.Path
6373
hasSignature := false
6474

6575
// Expand the index if it is compressed
66-
if strings.HasSuffix(indexFileName, ".tar.bz2") {
67-
indexFileName = strings.TrimSuffix(indexFileName, ".tar.bz2") + ".json" // == package_index.json
76+
if strings.HasSuffix(downloadFileName, ".tar.bz2") {
6877
signatureFileName := indexFileName + ".sig"
6978
signaturePath = destDir.Join(signatureFileName)
7079

@@ -95,8 +104,7 @@ func (res *IndexResource) Download(destDir *paths.Path, downloadCB rpc.DownloadP
95104
} else {
96105
logrus.Infof("No signature %s found in package index archive %s", signatureFileName, tmpArchivePath.Base())
97106
}
98-
} else if strings.HasSuffix(indexFileName, ".gz") {
99-
indexFileName = strings.TrimSuffix(indexFileName, ".gz") // == package_index.json
107+
} else if strings.HasSuffix(downloadFileName, ".gz") {
100108
tmpUnzippedIndexPath := tmp.Join(indexFileName)
101109
if err := paths.GUnzip(tmpIndexPath, tmpUnzippedIndexPath); err != nil {
102110
return &arduino.PermissionDeniedError{Message: tr("Error extracting %s", indexFileName), Cause: err}

0 commit comments

Comments
 (0)