generated from ekristen/go-project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
374 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package provider | ||
|
||
import ( | ||
"context" | ||
"crypto/sha256" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"os" | ||
"path/filepath" | ||
"strconv" | ||
|
||
"github.com/sirupsen/logrus" | ||
|
||
"github.com/ekristen/distillery/pkg/asset" | ||
"github.com/ekristen/distillery/pkg/common" | ||
) | ||
|
||
type GPGAsset struct { | ||
*asset.Asset | ||
|
||
KeyID uint64 | ||
|
||
Source ISource | ||
} | ||
|
||
func (a *GPGAsset) ID() string { | ||
return fmt.Sprintf("%s-%s", a.GetType(), a.KeyID) | ||
} | ||
|
||
func (a *GPGAsset) Path() string { | ||
return filepath.Join("gpg", strconv.FormatUint(a.KeyID, 10)) | ||
} | ||
|
||
func (a *GPGAsset) Download(_ context.Context) error { | ||
cacheDir, err := os.UserCacheDir() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
a.KeyID, err = a.MatchedAsset.GetGPGKeyID() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
downloadsDir := filepath.Join(cacheDir, common.NAME, "downloads") | ||
filename := strconv.FormatUint(a.KeyID, 10) | ||
|
||
assetFile := filepath.Join(downloadsDir, filename) | ||
a.DownloadPath = assetFile | ||
a.Extension = filepath.Ext(a.DownloadPath) | ||
|
||
assetFileHash := fmt.Sprintf("%s.sha256", assetFile) | ||
stats, err := os.Stat(assetFileHash) | ||
if err != nil && !os.IsNotExist(err) { | ||
return err | ||
} | ||
|
||
if stats != nil { | ||
logrus.Debugf("file already downloaded: %s", assetFile) | ||
return nil | ||
} | ||
|
||
logrus.Debugf("downloading asset: %s", a.KeyID) | ||
|
||
url := fmt.Sprintf("https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x%s", fmt.Sprintf("%X", a.KeyID)) | ||
|
||
// Make the HTTP request | ||
resp, err := http.Get(url) | ||
if err != nil { | ||
return fmt.Errorf("failed to download key: %w", err) | ||
} | ||
defer resp.Body.Close() | ||
|
||
// Check if the request was successful | ||
if resp.StatusCode != http.StatusOK { | ||
return fmt.Errorf("failed to download key: server returned status %s", resp.Status) | ||
} | ||
|
||
hasher := sha256.New() | ||
tmpFile, err := os.Create(assetFile) | ||
if err != nil { | ||
return err | ||
} | ||
defer tmpFile.Close() | ||
|
||
multiWriter := io.MultiWriter(tmpFile, hasher) | ||
|
||
f, err := os.Create(assetFile) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Write the asset's content to the temporary file | ||
_, err = io.Copy(multiWriter, resp.Body) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if _, err := io.Copy(f, resp.Body); err != nil { | ||
return err | ||
} | ||
|
||
logrus.Tracef("hash: %x", hasher.Sum(nil)) | ||
|
||
_ = os.WriteFile(assetFileHash, []byte(fmt.Sprintf("%x", hasher.Sum(nil))), 0600) | ||
a.Hash = string(hasher.Sum(nil)) | ||
|
||
logrus.Tracef("Downloaded asset to: %s", tmpFile.Name()) | ||
|
||
return nil | ||
} |
Oops, something went wrong.