53 lines
1,002 B
Go
53 lines
1,002 B
Go
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)
|
|
}
|