mirror of
https://github.com/zarzet/SpotiFLAC-Mobile.git
synced 2026-07-02 02:55:36 +02:00
feat(extension-repo): preserve package suffix from download URL
Detect .sflx and .spotiflac-ext from the registry download URL when saving repo downloads, and extract findExtension so the destination path can use the correct package extension.
This commit is contained in:
+24
-3
@@ -3916,13 +3916,29 @@ func GetStoreCategoriesJSON() (string, error) {
|
||||
return string(jsonBytes), nil
|
||||
}
|
||||
|
||||
func buildStoreExtensionDestPath(destDir, extensionID string) (string, error) {
|
||||
func storeExtensionPackageSuffix(downloadURL string) string {
|
||||
rawPath := downloadURL
|
||||
if parsed, err := url.Parse(downloadURL); err == nil {
|
||||
rawPath = parsed.Path
|
||||
}
|
||||
|
||||
lowerPath := strings.ToLower(rawPath)
|
||||
if strings.HasSuffix(lowerPath, ".sflx") {
|
||||
return ".sflx"
|
||||
}
|
||||
if strings.HasSuffix(lowerPath, ".spotiflac-ext") {
|
||||
return ".spotiflac-ext"
|
||||
}
|
||||
return ".spotiflac-ext"
|
||||
}
|
||||
|
||||
func buildStoreExtensionDestPath(destDir, extensionID, downloadURL string) (string, error) {
|
||||
if strings.TrimSpace(extensionID) == "" {
|
||||
return "", fmt.Errorf("invalid extension id")
|
||||
}
|
||||
|
||||
safeExtensionID := sanitizeFilename(extensionID)
|
||||
return filepath.Join(destDir, safeExtensionID+".spotiflac-ext"), nil
|
||||
return filepath.Join(destDir, safeExtensionID+storeExtensionPackageSuffix(downloadURL)), nil
|
||||
}
|
||||
|
||||
func DownloadStoreExtensionJSON(extensionID, destDir string) (string, error) {
|
||||
@@ -3931,7 +3947,12 @@ func DownloadStoreExtensionJSON(extensionID, destDir string) (string, error) {
|
||||
return "", fmt.Errorf("extension store not initialized")
|
||||
}
|
||||
|
||||
destPath, err := buildStoreExtensionDestPath(destDir, extensionID)
|
||||
ext, err := store.findExtension(extensionID)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
destPath, err := buildStoreExtensionDestPath(destDir, extensionID, ext.getDownloadURL())
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
@@ -428,10 +428,25 @@ func TestExportsJSONWrappersAndExtensionManagerSurface(t *testing.T) {
|
||||
if catsJSON, err := GetStoreCategoriesJSON(); err != nil || !strings.Contains(catsJSON, "metadata") {
|
||||
t.Fatalf("GetStoreCategoriesJSON = %q/%v", catsJSON, err)
|
||||
}
|
||||
if dest, err := buildStoreExtensionDestPath(dir, "coverage/ext"); err != nil || !strings.HasSuffix(dest, ".spotiflac-ext") {
|
||||
if dest, err := buildStoreExtensionDestPath(
|
||||
dir,
|
||||
"coverage/ext",
|
||||
"https://registry.example.com/coverage.spotiflac-ext",
|
||||
); err != nil || !strings.HasSuffix(dest, ".spotiflac-ext") {
|
||||
t.Fatalf("buildStoreExtensionDestPath = %q/%v", dest, err)
|
||||
}
|
||||
if _, err := buildStoreExtensionDestPath(dir, " "); err == nil {
|
||||
if dest, err := buildStoreExtensionDestPath(
|
||||
dir,
|
||||
"coverage/ext",
|
||||
"https://registry.example.com/coverage.sflx",
|
||||
); err != nil || !strings.HasSuffix(dest, ".sflx") {
|
||||
t.Fatalf("buildStoreExtensionDestPath sflx = %q/%v", dest, err)
|
||||
}
|
||||
if _, err := buildStoreExtensionDestPath(
|
||||
dir,
|
||||
" ",
|
||||
"https://registry.example.com/coverage.sflx",
|
||||
); err == nil {
|
||||
t.Fatal("expected invalid extension id")
|
||||
}
|
||||
if err := ClearStoreCacheJSON(); err != nil {
|
||||
|
||||
@@ -330,22 +330,26 @@ func (s *extensionStore) getExtensionsWithStatus(forceRefresh bool) ([]storeExte
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *extensionStore) downloadExtension(extensionID string, destPath string) error {
|
||||
func (s *extensionStore) findExtension(extensionID string) (*storeExtension, error) {
|
||||
registry, err := s.fetchRegistry(false)
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var ext *storeExtension
|
||||
for _, e := range registry.Extensions {
|
||||
if e.ID == extensionID {
|
||||
ext = &e
|
||||
break
|
||||
ext := e
|
||||
return &ext, nil
|
||||
}
|
||||
}
|
||||
|
||||
if ext == nil {
|
||||
return fmt.Errorf("extension %s not found in store", extensionID)
|
||||
return nil, fmt.Errorf("extension %s not found in store", extensionID)
|
||||
}
|
||||
|
||||
func (s *extensionStore) downloadExtension(extensionID string, destPath string) error {
|
||||
ext, err := s.findExtension(extensionID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := requireHTTPSURL(ext.getDownloadURL(), "extension download"); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user