fix edge-case for new json format

This commit is contained in:
Muhammed Efe Cetin
2025-12-20 19:52:46 +01:00
committed by M. Efe Çetin
parent 902370f725
commit fe5b11d0e4
3 changed files with 105 additions and 55 deletions

32
http.go
View File

@@ -114,6 +114,7 @@ func (r *Redirector) redirectHandler(w http.ResponseWriter, req *http.Request) {
// If we have a dlMap, we map the url to a final path instead
var isGithub bool
var isLink bool
if r.dlMap != nil {
if newPath, exists := r.dlMap[strings.TrimLeft(req.URL.Path, "/")]; exists {
downloadsMapped.Inc()
@@ -122,6 +123,9 @@ func (r *Redirector) redirectHandler(w http.ResponseWriter, req *http.Request) {
if strings.Contains(newPath, "/armbian/") {
redirectPath = newPath
isGithub = true
} else if strings.HasPrefix(newPath, "http://") || strings.HasPrefix(newPath, "https://") {
isLink = true
redirectPath = newPath
} else {
redirectPath = path.Join(server.Path, newPath)
}
@@ -132,16 +136,19 @@ func (r *Redirector) redirectHandler(w http.ResponseWriter, req *http.Request) {
redirectPath += "/"
}
// We need to build the final url now
u := &url.URL{
Scheme: scheme,
Host: server.Host,
Path: redirectPath,
}
var u *url.URL
if !isLink {
// We need to build the final url now
u = &url.URL{
Scheme: scheme,
Host: server.Host,
Path: redirectPath,
}
// Some images are hosted at Github, we have to redirect them to the correct URL
if isGithub {
u.Host = "github.com"
// Some images are hosted at Github, we have to redirect them to the correct URL
if isGithub {
u.Host = "github.com"
}
}
server.Redirects.Inc()
@@ -152,7 +159,12 @@ func (r *Redirector) redirectHandler(w http.ResponseWriter, req *http.Request) {
w.Header().Set("X-Geo-Distance", fmt.Sprintf("%f", distance))
}
w.Header().Set("Location", u.String())
path := redirectPath
if !isLink && u != nil {
path = u.String()
}
w.Header().Set("Location", path)
w.WriteHeader(http.StatusFound)
}

37
map.go
View File

@@ -47,17 +47,20 @@ type Map struct {
// ReleaseFile represents a file to be mapped
type ReleaseFile struct {
BoardSlug string `json:"board_slug"`
FileURL string `json:"file_url"`
FileUpdated string `json:"file_updated"`
FileSize string `json:"file_size"`
DistroRelease string `json:"distro_release"`
KernelBranch string `json:"kernel_branch"`
ImageVariant string `json:"image_variant"`
Preinstalled string `json:"preinstalled_application"`
Promoted string `json:"promoted"`
Repository string `json:"download_repository"`
Extension string `json:"file_extension"`
BoardSlug string `json:"board_slug"`
FileURL string `json:"file_url"`
FileURLASC string `json:"file_url_asc"`
FileURLSHA string `json:"file_url_sha"`
FileURLTorrent string `json:"file_url_torrent"`
FileUpdated string `json:"file_updated"`
FileSize string `json:"file_size"`
DistroRelease string `json:"distro_release"`
KernelBranch string `json:"kernel_branch"`
ImageVariant string `json:"image_variant"`
Preinstalled string `json:"preinstalled_application"`
Promoted string `json:"promoted"`
Repository string `json:"download_repository"`
Extension string `json:"file_extension"`
}
var distroCaser = cases.Title(language.Und)
@@ -135,7 +138,17 @@ func loadMapJSON(f io.Reader, specialExtensions map[string]string) (map[string]s
continue
}
m[sb.String()+ext] = u.Path + ext
var filePath string
switch ext {
case ".asc":
filePath = file.FileURLASC
case ".sha":
filePath = file.FileURLSHA
case ".torrent":
filePath = file.FileURLTorrent
}
m[sb.String()+ext] = filePath
}
sb.WriteString(".")

View File

@@ -48,40 +48,60 @@ var _ = Describe("Map", 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",
"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"
}
"board_slug": "khadas-vim1",
"board_name": "Khadas VIM1",
"board_vendor": "khadas",
"armbian_version": "25.11.1",
"file_url": "https://dl.armbian.com/khadas-vim1/archive/Armbian_25.11.1_Khadas-vim1_noble_current_6.12.58_xfce_desktop.img.xz",
"file_url_asc": "https://dl.armbian.com/khadas-vim1/archive/Armbian_25.11.1_Khadas-vim1_noble_current_6.12.58_xfce_desktop.img.xz.asc",
"file_url_sha": "https://dl.armbian.com/khadas-vim1/archive/Armbian_25.11.1_Khadas-vim1_noble_current_6.12.58_xfce_desktop.img.xz.sha",
"file_url_torrent": "https://dl.armbian.com/khadas-vim1/archive/Armbian_25.11.1_Khadas-vim1_noble_current_6.12.58_xfce_desktop.img.xz.torrent",
"redi_url": "https://dl.armbian.com/khadas-vim1/Noble_current_xfce",
"redi_url_asc": "https://dl.armbian.com/khadas-vim1/Noble_current_xfce.asc",
"redi_url_sha": "https://dl.armbian.com/khadas-vim1/Noble_current_xfce.sha",
"redi_url_torrent": "https://dl.armbian.com/khadas-vim1/Noble_current_xfce.torrent",
"file_updated": "2025-11-22T15:39:59Z",
"file_size": "1482867344",
"distro_release": "noble",
"kernel_branch": "current",
"image_variant": "xfce",
"preinstalled_application": "",
"promoted": "false",
"download_repository": "archive",
"file_extension": "img.xz"
},
{
"board_slug": "khadas-vim1",
"board_name": "Khadas VIM1",
"board_vendor": "khadas",
"armbian_version": "25.11.1",
"file_url": "https://dl.armbian.com/khadas-vim1/archive2/Armbian_25.11.1_Khadas-vim1_noble_current_6.12.58_xfce_desktop.img.xz",
"file_url_asc": "https://dl.armbian.com/khadas-vim1/archive/Armbian_25.11.1_Khadas-vim1_noble_current_6.12.58_xfce_desktop.img.xz.asc",
"file_url_sha": "https://dl.armbian.com/khadas-vim1/archive/Armbian_25.11.1_Khadas-vim1_noble_current_6.12.58_xfce_desktop.img.xz.sha",
"file_url_torrent": "https://dl.armbian.com/khadas-vim1/archive/Armbian_25.11.1_Khadas-vim1_noble_current_6.12.58_xfce_desktop.img.xz.torrent",
"redi_url": "https://dl.armbian.com/khadas-vim1/Noble_current_xfce",
"redi_url_asc": "https://dl.armbian.com/khadas-vim1/Noble_current_xfce.asc",
"redi_url_sha": "https://dl.armbian.com/khadas-vim1/Noble_current_xfce.sha",
"redi_url_torrent": "https://dl.armbian.com/khadas-vim1/Noble_current_xfce.torrent",
"file_updated": "2025-11-22T15:39:59Z",
"file_size": "1482867344",
"distro_release": "noble",
"kernel_branch": "current",
"image_variant": "xfce",
"preinstalled_application": "test",
"promoted": "false",
"download_repository": "archive",
"file_extension": "img.xz"
}
]
}`
m, err := loadMapJSON(strings.NewReader(data), testExtensions)
Expect(err).To(BeNil())
Expect(m["khadas-vim1/Bookworm_current_xfce"]).To(Equal("/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("/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("/khadas-vim1/archive/Armbian_23.11.1_Khadas-vim1_bookworm_current_6.1.63_xfce_desktop.img.xz"))
Expect(m["khadas-vim1/Noble_current_xfce"]).To(Equal("/khadas-vim1/archive/Armbian_25.11.1_Khadas-vim1_noble_current_6.12.58_xfce_desktop.img.xz"))
Expect(m["khadas-vim1/Noble_current_xfce.sha"]).To(Equal("https://dl.armbian.com/khadas-vim1/archive/Armbian_25.11.1_Khadas-vim1_noble_current_6.12.58_xfce_desktop.img.xz.sha"))
Expect(m["khadas-vim1/Noble_current_xfce-test"]).To(Equal("/khadas-vim1/archive2/Armbian_25.11.1_Khadas-vim1_noble_current_6.12.58_xfce_desktop.img.xz"))
})
It("Should work with files that have weird extensions", func() {
data := `{
@@ -90,6 +110,8 @@ var _ = Describe("Map", func() {
"board_slug": "khadas-vim4",
"armbian_version": "23.11.1",
"file_url": "https://dl.armbian.com/khadas-vim4/archive/Armbian_23.11.1_Khadas-vim4_bookworm_legacy_5.4.180.oowow.img.xz",
"file_url_sha": "sha_test_url_vim4",
"file_url_asc": "asc_test_url_vim4",
"file_updated": "2023-11-30T01:03:05Z",
"file_size": "477868032",
"distro_release": "bookworm",
@@ -104,6 +126,8 @@ var _ = Describe("Map", func() {
"board_slug": "uefi-arm64",
"armbian_version": "24.5.5",
"file_url": "https://dl.armbian.com/uefi-arm64/archive/Armbian_24.5.5_Uefi-arm64_bookworm_current_6.6.42_minimal.img.qcow2",
"file_url_sha": "sha_test_url_uefi",
"file_url_asc": "asc_test_url_uefi",
"redi_url": "https://dl.armbian.com/uefi-arm64/Bookworm_current_minimal",
"file_updated": "2024-07-25T18:01:20Z",
"file_size": "673315888",
@@ -119,6 +143,8 @@ var _ = Describe("Map", func() {
"board_slug": "qemu-uboot-arm64",
"armbian_version": "24.8.0-trunk.542",
"file_url": "https://github.com/armbian/os/releases/download/24.8.0-trunk.542/Armbian_24.8.0-trunk.542_Qemu-uboot-arm64_bookworm_current_6.6.44_minimal.u-boot.bin.xz",
"file_url_sha": "sha_test_url_qemu",
"file_url_asc": "asc_test_url_qemu",
"redi_url": "https://dl.armbian.com/qemu-uboot-arm64/Bookworm_current_minimal",
"file_updated": "2024-08-09T10:07:43Z",
"file_size": "314832",
@@ -137,13 +163,12 @@ var _ = Describe("Map", func() {
Expect(err).To(BeNil())
Expect(m["khadas-vim4/Bookworm_legacy_server"]).To(Equal("/khadas-vim4/archive/Armbian_23.11.1_Khadas-vim4_bookworm_legacy_5.4.180.oowow.img.xz"))
Expect(m["khadas-vim4/Bookworm_legacy_server.asc"]).To(Equal("/khadas-vim4/archive/Armbian_23.11.1_Khadas-vim4_bookworm_legacy_5.4.180.oowow.img.xz.asc"))
Expect(m["khadas-vim4/Bookworm_legacy_server.sha"]).To(Equal("/khadas-vim4/archive/Armbian_23.11.1_Khadas-vim4_bookworm_legacy_5.4.180.oowow.img.xz.sha"))
Expect(m["khadas-vim4/Bookworm_legacy_server.asc"]).To(Equal("asc_test_url_vim4"))
Expect(m["khadas-vim4/Bookworm_legacy_server.sha"]).To(Equal("sha_test_url_vim4"))
Expect(m["uefi-arm64/Bookworm_current_minimal-qcow2"]).To(Equal("/uefi-arm64/archive/Armbian_24.5.5_Uefi-arm64_bookworm_current_6.6.42_minimal.img.qcow2"))
Expect(m["uefi-arm64/Bookworm_current_minimal-qcow2.asc"]).To(Equal("/uefi-arm64/archive/Armbian_24.5.5_Uefi-arm64_bookworm_current_6.6.42_minimal.img.qcow2.asc"))
Expect(m["uefi-arm64/Bookworm_current_minimal-qcow2.sha"]).To(Equal("/uefi-arm64/archive/Armbian_24.5.5_Uefi-arm64_bookworm_current_6.6.42_minimal.img.qcow2.sha"))
Expect(m["uefi-arm64/Bookworm_current_minimal-qcow2.asc"]).To(Equal("asc_test_url_uefi"))
Expect(m["uefi-arm64/Bookworm_current_minimal-qcow2.sha"]).To(Equal("sha_test_url_uefi"))
Expect(m["nightly/qemu-uboot-arm64/Bookworm_current_minimal-uboot-bin"]).To(Equal("/armbian/os/releases/download/24.8.0-trunk.542/Armbian_24.8.0-trunk.542_Qemu-uboot-arm64_bookworm_current_6.6.44_minimal.u-boot.bin.xz"))
Expect(m["nightly/qemu-uboot-arm64/Bookworm_current_minimal-uboot-bin.boot.bin.xz"]).To(Equal("/armbian/os/releases/download/24.8.0-trunk.542/Armbian_24.8.0-trunk.542_Qemu-uboot-arm64_bookworm_current_6.6.44_minimal.u-boot.bin.xz"))