Clean up + add webserver

This commit is contained in:
William Bouzourène 2026-02-15 20:50:58 +01:00
parent 2eed16376f
commit 67e2e4d306
Signed by: bouzoure
GPG key ID: 423440D735B56BE2
7 changed files with 216 additions and 68 deletions

53
routes/fetch.go Normal file
View file

@ -0,0 +1,53 @@
package routes
import (
"fmt"
"git.readonly.ch/bouzoure/gpx-downloader/providers"
"github.com/gofiber/fiber/v2"
"github.com/tkrajina/gpxgo/gpx"
)
func Fetch(c *fiber.Ctx) error {
sourceUrl := c.FormValue("url")
if len(sourceUrl) <= 0 {
c.SendStatus(422)
return c.SendString("Error: URL is empty")
}
var gpxFile gpx.GPX
var err error
var filename string
// Check if link is Suisse Mobile
suisseMobileId := providers.SuisseMobileCheckURL(sourceUrl)
if suisseMobileId > 0 {
filename = fmt.Sprintf(
"suisse_mobile_%d",
suisseMobileId,
)
gpxFile, err = providers.SuisseMobileFetch(suisseMobileId)
if err != nil {
return err
}
}
if len(filename) <= 0 {
c.SendStatus(422)
return c.SendString("Error: URL is not supported")
}
xml, err := gpxFile.ToXml(gpx.ToXmlParams{})
if err != nil {
return err
}
c.Set("Content-Type", "octet-stream")
c.Set("Content-Disposition", fmt.Sprintf(
"attechment,filename=%s.gpx",
filename,
))
return c.Send(xml)
}