You've already forked armbian-router
mirror of
https://github.com/armbian/armbian-router.git
synced 2026-01-06 10:37:03 -08:00
fix json handling
This commit is contained in:
48
map.go
48
map.go
@@ -4,13 +4,13 @@ import (
|
||||
"encoding/csv"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"golang.org/x/text/cases"
|
||||
"golang.org/x/text/language"
|
||||
"io"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/text/cases"
|
||||
"golang.org/x/text/language"
|
||||
)
|
||||
|
||||
var ErrUnsupportedFormat = errors.New("unsupported map format")
|
||||
@@ -96,22 +96,40 @@ func loadMapJSON(f io.Reader) (map[string]string, error) {
|
||||
}
|
||||
|
||||
for _, file := range data.Assets {
|
||||
builtUri := fmt.Sprintf("%s/%s_%s.%s",
|
||||
file.BoardSlug,
|
||||
distroCaser.String(file.DistroRelease),
|
||||
file.KernelBranch,
|
||||
file.Extension)
|
||||
var sb strings.Builder
|
||||
|
||||
m[builtUri] = file.FileURL
|
||||
sb.WriteString(file.BoardSlug)
|
||||
sb.WriteString("/")
|
||||
sb.WriteString(distroCaser.String(file.DistroRelease))
|
||||
sb.WriteString("_")
|
||||
sb.WriteString(file.KernelBranch)
|
||||
|
||||
if file.ImageVariant != "server" {
|
||||
sb.WriteString("_")
|
||||
sb.WriteString(file.ImageVariant)
|
||||
}
|
||||
|
||||
if file.Preinstalled != "" {
|
||||
sb.WriteString("-")
|
||||
sb.WriteString(file.Preinstalled)
|
||||
}
|
||||
|
||||
if file.Extension == "img.xz" {
|
||||
noExtUri := fmt.Sprintf("%s/%s_%s",
|
||||
file.BoardSlug,
|
||||
distroCaser.String(file.DistroRelease),
|
||||
file.KernelBranch)
|
||||
|
||||
m[noExtUri] = file.FileURL
|
||||
m[sb.String()] = file.FileURL
|
||||
}
|
||||
|
||||
sb.WriteString(".")
|
||||
|
||||
if file.Extension == "img.xz.sha" {
|
||||
sb.WriteString("sha")
|
||||
} else if file.Extension == "img.xz.asc" {
|
||||
sb.WriteString("asc")
|
||||
} else {
|
||||
sb.WriteString(file.Extension)
|
||||
}
|
||||
|
||||
builtUri := sb.String()
|
||||
m[builtUri] = file.FileURL
|
||||
}
|
||||
|
||||
return m, nil
|
||||
|
||||
63
map_test.go
63
map_test.go
@@ -1,10 +1,12 @@
|
||||
package redirector
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var _ = Describe("Map", func() {
|
||||
@@ -40,4 +42,63 @@ var _ = Describe("Map", func() {
|
||||
Expect(err).To(BeNil())
|
||||
Expect(m["aml-s9xx-box/Bookworm_current"]).To(Equal("https://dl.armbian.com/aml-s9xx-box/archive/Armbian_23.11.1_Aml-s9xx-box_bookworm_current_6.1.63.img.xz"))
|
||||
})
|
||||
|
||||
It("Should successfully load the map from a JSON file", func() {
|
||||
data := `{
|
||||
"assets": [
|
||||
{
|
||||
"board_slug": "khadas-vim1",
|
||||
"file_url": "https://dl.armbian.com/khadas-vim1/archive/Armbian_23.11.1_Khadas-vim1_bookworm_current_6.1.63_xfce_desktop.img.xz",
|
||||
"file_updated": "2023-11-30T01:06:34Z",
|
||||
"file_size": "1605260504",
|
||||
"distro_release": "bookworm",
|
||||
"kernel_branch": "current",
|
||||
"image_variant": "xfce",
|
||||
"preinstalled_application": "",
|
||||
"promoted": "false",
|
||||
"download_repository": "archive",
|
||||
"file_extension": "img.xz"
|
||||
},
|
||||
{
|
||||
"board_slug": "khadas-vim1",
|
||||
"file_url": "https://dl.armbian.com/khadas-vim1/archive/Armbian_23.11.1_Khadas-vim1_bookworm_current_6.1.63_xfce_desktop.img.xz.sha",
|
||||
"file_updated": "2023-11-30T01:06:34Z",
|
||||
"file_size": "1605260504",
|
||||
"distro_release": "bookworm",
|
||||
"kernel_branch": "current",
|
||||
"image_variant": "xfce",
|
||||
"preinstalled_application": "",
|
||||
"promoted": "false",
|
||||
"download_repository": "archive",
|
||||
"file_extension": "img.xz.sha"
|
||||
},
|
||||
{
|
||||
"board_slug": "khadas-vim1",
|
||||
"file_url": "https://dl.armbian.com/khadas-vim1/archive/Armbian_23.11.1_Khadas-vim1_bookworm_current_6.1.63_xfce_desktop.img.xz",
|
||||
"file_updated": "2023-11-30T01:06:34Z",
|
||||
"file_size": "1605260504",
|
||||
"distro_release": "bookworm",
|
||||
"kernel_branch": "current",
|
||||
"image_variant": "xfce",
|
||||
"preinstalled_application": "test",
|
||||
"promoted": "false",
|
||||
"download_repository": "archive",
|
||||
"file_extension": "img.xz"
|
||||
}
|
||||
]
|
||||
}`
|
||||
|
||||
m, err := loadMapJSON(strings.NewReader(data))
|
||||
|
||||
log.Println(m)
|
||||
|
||||
for k, v := range m {
|
||||
fmt.Printf("%s => %s\n", k, v)
|
||||
}
|
||||
|
||||
Expect(err).To(BeNil())
|
||||
Expect(m["khadas-vim1/Bookworm_current_xfce"]).To(Equal("https://dl.armbian.com/khadas-vim1/archive/Armbian_23.11.1_Khadas-vim1_bookworm_current_6.1.63_xfce_desktop.img.xz"))
|
||||
Expect(m["khadas-vim1/Bookworm_current_xfce.sha"]).To(Equal("https://dl.armbian.com/khadas-vim1/archive/Armbian_23.11.1_Khadas-vim1_bookworm_current_6.1.63_xfce_desktop.img.xz.sha"))
|
||||
Expect(m["khadas-vim1/Bookworm_current_xfce-test"]).To(Equal("https://dl.armbian.com/khadas-vim1/archive/Armbian_23.11.1_Khadas-vim1_bookworm_current_6.1.63_xfce_desktop.img.xz"))
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"github.com/armbian/redirector/db"
|
||||
"math/rand"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
"github.com/armbian/redirector/db"
|
||||
)
|
||||
|
||||
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
|
||||
|
||||
Reference in New Issue
Block a user