Skip to content

Commit fe5b11d

Browse files
committed
fix edge-case for new json format
1 parent 902370f commit fe5b11d

3 files changed

Lines changed: 105 additions & 55 deletions

File tree

http.go

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ func (r *Redirector) redirectHandler(w http.ResponseWriter, req *http.Request) {
114114

115115
// If we have a dlMap, we map the url to a final path instead
116116
var isGithub bool
117+
var isLink bool
117118
if r.dlMap != nil {
118119
if newPath, exists := r.dlMap[strings.TrimLeft(req.URL.Path, "/")]; exists {
119120
downloadsMapped.Inc()
@@ -122,6 +123,9 @@ func (r *Redirector) redirectHandler(w http.ResponseWriter, req *http.Request) {
122123
if strings.Contains(newPath, "/armbian/") {
123124
redirectPath = newPath
124125
isGithub = true
126+
} else if strings.HasPrefix(newPath, "http://") || strings.HasPrefix(newPath, "https://") {
127+
isLink = true
128+
redirectPath = newPath
125129
} else {
126130
redirectPath = path.Join(server.Path, newPath)
127131
}
@@ -132,16 +136,19 @@ func (r *Redirector) redirectHandler(w http.ResponseWriter, req *http.Request) {
132136
redirectPath += "/"
133137
}
134138

135-
// We need to build the final url now
136-
u := &url.URL{
137-
Scheme: scheme,
138-
Host: server.Host,
139-
Path: redirectPath,
140-
}
139+
var u *url.URL
140+
if !isLink {
141+
// We need to build the final url now
142+
u = &url.URL{
143+
Scheme: scheme,
144+
Host: server.Host,
145+
Path: redirectPath,
146+
}
141147

142-
// Some images are hosted at Github, we have to redirect them to the correct URL
143-
if isGithub {
144-
u.Host = "github.com"
148+
// Some images are hosted at Github, we have to redirect them to the correct URL
149+
if isGithub {
150+
u.Host = "github.com"
151+
}
145152
}
146153

147154
server.Redirects.Inc()
@@ -152,7 +159,12 @@ func (r *Redirector) redirectHandler(w http.ResponseWriter, req *http.Request) {
152159
w.Header().Set("X-Geo-Distance", fmt.Sprintf("%f", distance))
153160
}
154161

155-
w.Header().Set("Location", u.String())
162+
path := redirectPath
163+
if !isLink && u != nil {
164+
path = u.String()
165+
}
166+
167+
w.Header().Set("Location", path)
156168
w.WriteHeader(http.StatusFound)
157169
}
158170

map.go

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,20 @@ type Map struct {
4747

4848
// ReleaseFile represents a file to be mapped
4949
type ReleaseFile struct {
50-
BoardSlug string `json:"board_slug"`
51-
FileURL string `json:"file_url"`
52-
FileUpdated string `json:"file_updated"`
53-
FileSize string `json:"file_size"`
54-
DistroRelease string `json:"distro_release"`
55-
KernelBranch string `json:"kernel_branch"`
56-
ImageVariant string `json:"image_variant"`
57-
Preinstalled string `json:"preinstalled_application"`
58-
Promoted string `json:"promoted"`
59-
Repository string `json:"download_repository"`
60-
Extension string `json:"file_extension"`
50+
BoardSlug string `json:"board_slug"`
51+
FileURL string `json:"file_url"`
52+
FileURLASC string `json:"file_url_asc"`
53+
FileURLSHA string `json:"file_url_sha"`
54+
FileURLTorrent string `json:"file_url_torrent"`
55+
FileUpdated string `json:"file_updated"`
56+
FileSize string `json:"file_size"`
57+
DistroRelease string `json:"distro_release"`
58+
KernelBranch string `json:"kernel_branch"`
59+
ImageVariant string `json:"image_variant"`
60+
Preinstalled string `json:"preinstalled_application"`
61+
Promoted string `json:"promoted"`
62+
Repository string `json:"download_repository"`
63+
Extension string `json:"file_extension"`
6164
}
6265

6366
var distroCaser = cases.Title(language.Und)
@@ -135,7 +138,17 @@ func loadMapJSON(f io.Reader, specialExtensions map[string]string) (map[string]s
135138
continue
136139
}
137140

138-
m[sb.String()+ext] = u.Path + ext
141+
var filePath string
142+
switch ext {
143+
case ".asc":
144+
filePath = file.FileURLASC
145+
case ".sha":
146+
filePath = file.FileURLSHA
147+
case ".torrent":
148+
filePath = file.FileURLTorrent
149+
}
150+
151+
m[sb.String()+ext] = filePath
139152
}
140153

141154
sb.WriteString(".")

map_test.go

Lines changed: 58 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -48,40 +48,60 @@ var _ = Describe("Map", func() {
4848
data := `{
4949
"assets": [
5050
{
51-
"board_slug": "khadas-vim1",
52-
"file_url": "https://dl.armbian.com/khadas-vim1/archive/Armbian_23.11.1_Khadas-vim1_bookworm_current_6.1.63_xfce_desktop.img.xz",
53-
"file_updated": "2023-11-30T01:06:34Z",
54-
"file_size": "1605260504",
55-
"distro_release": "bookworm",
56-
"kernel_branch": "current",
57-
"image_variant": "xfce",
58-
"preinstalled_application": "",
59-
"promoted": "false",
60-
"download_repository": "archive",
61-
"file_extension": "img.xz"
62-
},
63-
{
64-
"board_slug": "khadas-vim1",
65-
"file_url": "https://dl.armbian.com/khadas-vim1/archive/Armbian_23.11.1_Khadas-vim1_bookworm_current_6.1.63_xfce_desktop.img.xz",
66-
"file_updated": "2023-11-30T01:06:34Z",
67-
"file_size": "1605260504",
68-
"distro_release": "bookworm",
69-
"kernel_branch": "current",
70-
"image_variant": "xfce",
71-
"preinstalled_application": "test",
72-
"promoted": "false",
73-
"download_repository": "archive",
74-
"file_extension": "img.xz"
75-
}
51+
"board_slug": "khadas-vim1",
52+
"board_name": "Khadas VIM1",
53+
"board_vendor": "khadas",
54+
"armbian_version": "25.11.1",
55+
"file_url": "https://dl.armbian.com/khadas-vim1/archive/Armbian_25.11.1_Khadas-vim1_noble_current_6.12.58_xfce_desktop.img.xz",
56+
"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",
57+
"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",
58+
"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",
59+
"redi_url": "https://dl.armbian.com/khadas-vim1/Noble_current_xfce",
60+
"redi_url_asc": "https://dl.armbian.com/khadas-vim1/Noble_current_xfce.asc",
61+
"redi_url_sha": "https://dl.armbian.com/khadas-vim1/Noble_current_xfce.sha",
62+
"redi_url_torrent": "https://dl.armbian.com/khadas-vim1/Noble_current_xfce.torrent",
63+
"file_updated": "2025-11-22T15:39:59Z",
64+
"file_size": "1482867344",
65+
"distro_release": "noble",
66+
"kernel_branch": "current",
67+
"image_variant": "xfce",
68+
"preinstalled_application": "",
69+
"promoted": "false",
70+
"download_repository": "archive",
71+
"file_extension": "img.xz"
72+
},
73+
{
74+
"board_slug": "khadas-vim1",
75+
"board_name": "Khadas VIM1",
76+
"board_vendor": "khadas",
77+
"armbian_version": "25.11.1",
78+
"file_url": "https://dl.armbian.com/khadas-vim1/archive2/Armbian_25.11.1_Khadas-vim1_noble_current_6.12.58_xfce_desktop.img.xz",
79+
"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",
80+
"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",
81+
"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",
82+
"redi_url": "https://dl.armbian.com/khadas-vim1/Noble_current_xfce",
83+
"redi_url_asc": "https://dl.armbian.com/khadas-vim1/Noble_current_xfce.asc",
84+
"redi_url_sha": "https://dl.armbian.com/khadas-vim1/Noble_current_xfce.sha",
85+
"redi_url_torrent": "https://dl.armbian.com/khadas-vim1/Noble_current_xfce.torrent",
86+
"file_updated": "2025-11-22T15:39:59Z",
87+
"file_size": "1482867344",
88+
"distro_release": "noble",
89+
"kernel_branch": "current",
90+
"image_variant": "xfce",
91+
"preinstalled_application": "test",
92+
"promoted": "false",
93+
"download_repository": "archive",
94+
"file_extension": "img.xz"
95+
}
7696
]
7797
}`
7898

7999
m, err := loadMapJSON(strings.NewReader(data), testExtensions)
80100

81101
Expect(err).To(BeNil())
82-
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"))
83-
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"))
84-
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"))
102+
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"))
103+
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"))
104+
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"))
85105
})
86106
It("Should work with files that have weird extensions", func() {
87107
data := `{
@@ -90,6 +110,8 @@ var _ = Describe("Map", func() {
90110
"board_slug": "khadas-vim4",
91111
"armbian_version": "23.11.1",
92112
"file_url": "https://dl.armbian.com/khadas-vim4/archive/Armbian_23.11.1_Khadas-vim4_bookworm_legacy_5.4.180.oowow.img.xz",
113+
"file_url_sha": "sha_test_url_vim4",
114+
"file_url_asc": "asc_test_url_vim4",
93115
"file_updated": "2023-11-30T01:03:05Z",
94116
"file_size": "477868032",
95117
"distro_release": "bookworm",
@@ -104,6 +126,8 @@ var _ = Describe("Map", func() {
104126
"board_slug": "uefi-arm64",
105127
"armbian_version": "24.5.5",
106128
"file_url": "https://dl.armbian.com/uefi-arm64/archive/Armbian_24.5.5_Uefi-arm64_bookworm_current_6.6.42_minimal.img.qcow2",
129+
"file_url_sha": "sha_test_url_uefi",
130+
"file_url_asc": "asc_test_url_uefi",
107131
"redi_url": "https://dl.armbian.com/uefi-arm64/Bookworm_current_minimal",
108132
"file_updated": "2024-07-25T18:01:20Z",
109133
"file_size": "673315888",
@@ -119,6 +143,8 @@ var _ = Describe("Map", func() {
119143
"board_slug": "qemu-uboot-arm64",
120144
"armbian_version": "24.8.0-trunk.542",
121145
"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",
146+
"file_url_sha": "sha_test_url_qemu",
147+
"file_url_asc": "asc_test_url_qemu",
122148
"redi_url": "https://dl.armbian.com/qemu-uboot-arm64/Bookworm_current_minimal",
123149
"file_updated": "2024-08-09T10:07:43Z",
124150
"file_size": "314832",
@@ -137,13 +163,12 @@ var _ = Describe("Map", func() {
137163

138164
Expect(err).To(BeNil())
139165
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"))
140-
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"))
141-
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"))
166+
Expect(m["khadas-vim4/Bookworm_legacy_server.asc"]).To(Equal("asc_test_url_vim4"))
167+
Expect(m["khadas-vim4/Bookworm_legacy_server.sha"]).To(Equal("sha_test_url_vim4"))
142168

143169
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"))
144-
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"))
145-
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"))
146-
170+
Expect(m["uefi-arm64/Bookworm_current_minimal-qcow2.asc"]).To(Equal("asc_test_url_uefi"))
171+
Expect(m["uefi-arm64/Bookworm_current_minimal-qcow2.sha"]).To(Equal("sha_test_url_uefi"))
147172
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"))
148173
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"))
149174

0 commit comments

Comments
 (0)