Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some changes to fix the path problem under windows system and android TAK client #20

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions cmd/goatak_server/admin_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func NewAdminAPI(app *App, addr string, webtakRoot string) *AdminAPI {

engine.Delims("[[", "]]")

api.f = fiber.New(fiber.Config{EnablePrintRoutes: false, DisableStartupMessage: true, Views: engine})
api.f = fiber.New(fiber.Config{EnablePrintRoutes: false, DisableStartupMessage: true, Views: engine, BodyLimit: 64 * 1024 * 1024})

api.f.Use(log.NewFiberLogger(&log.LoggerConfig{Name: "admin_api", Level: slog.LevelDebug}))

Expand Down Expand Up @@ -71,9 +71,12 @@ func NewAdminAPI(app *App, addr string, webtakRoot string) *AdminAPI {
api.f.Get("/mission", getAllMissionHandler(app))
}

api.f.All("/webtak", webTakPathHandler())
if webtakRoot != "" {
api.f.Static("/webtak", webtakRoot)

addMartiRoutes(app, api.f)
} else {
staticfiles.EmbedWebTak(api.f)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't want to use /webtak handle at all if it's not a webtacRoot

addMartiRoutes(app, api.f)
}

Expand All @@ -83,6 +86,15 @@ func NewAdminAPI(app *App, addr string, webtakRoot string) *AdminAPI {
return api
}

func webTakPathHandler() fiber.Handler {
return func(c *fiber.Ctx) error {
if c.Path() == "/webtak" {
return c.Redirect("/webtak/", http.StatusMovedPermanently)
}
return c.Next()
}
}

func (api *AdminAPI) Address() string {
return api.addr
}
Expand Down
25 changes: 20 additions & 5 deletions cmd/goatak_server/marti_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type MartiAPI struct {

func NewMartiApi(app *App, addr string) *MartiAPI {
api := &MartiAPI{
f: fiber.New(fiber.Config{EnablePrintRoutes: false, DisableStartupMessage: true}),
f: fiber.New(fiber.Config{EnablePrintRoutes: false, DisableStartupMessage: true, BodyLimit: 64 * 1024 * 1024}),
addr: addr,
}

Expand Down Expand Up @@ -201,7 +201,7 @@ func getMissionQueryHandler(app *App) fiber.Handler {
if pi := app.packageManager.GetFirst(func(pi *pm.PackageInfo) bool {
return pi.Hash == hash && user.CanSeeScope(pi.Scope)
}); pi != nil {
return ctx.SendString(packageUrl(pi))
return ctx.SendString(ctx.BaseURL() + packageUrl(pi))
}

return ctx.Status(fiber.StatusNotFound).SendString("not found")
Expand Down Expand Up @@ -229,7 +229,7 @@ func getMissionUploadHandler(app *App) fiber.Handler {
return pi.Hash == hash && user.CanSeeScope(pi.Scope)
}); pi != nil {
app.logger.Info("hash already exists: " + hash)
return ctx.SendString(packageUrl(pi))
return ctx.SendString(ctx.BaseURL() + packageUrl(pi))
}

pi, err := app.uploadMultipart(ctx, "", hash, fname, true)
Expand All @@ -240,7 +240,7 @@ func getMissionUploadHandler(app *App) fiber.Handler {

app.logger.Info(fmt.Sprintf("save packege %s %s %s", pi.Name, pi.UID, pi.Hash))

return ctx.SendString(packageUrl(pi))
return ctx.SendString(ctx.BaseURL() + packageUrl(pi))
}
}

Expand Down Expand Up @@ -425,7 +425,22 @@ func getMetadataGetHandler(app *App) fiber.Handler {
if pi := app.packageManager.GetFirst(func(pi *pm.PackageInfo) bool {
return pi.Hash == hash && user.CanSeeScope(pi.Scope)
}); pi != nil {
return ctx.SendString(pi.Tool)
f, err := app.packageManager.GetFile(pi.Hash)

if err != nil {
app.logger.Error("get file error", slog.Any("error", err))
return err
}

defer f.Close()

ctx.Set(fiber.HeaderContentType, pi.MIMEType)
ctx.Set(fiber.HeaderLastModified, pi.SubmissionDateTime.UTC().Format(http.TimeFormat))
ctx.Set(fiber.HeaderContentLength, strconv.Itoa(pi.Size))
ctx.Set(fiber.HeaderETag, pi.Hash)

_, err = io.Copy(ctx.Response().BodyWriter(), f)
return err
}

return ctx.SendStatus(fiber.StatusNotFound)
Expand Down
5 changes: 2 additions & 3 deletions internal/pm/blob_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (m *BlobManager) PutFile(hash string, r io.Reader) (string, int64, error) {
return hash, 0, nil
}

f, err := os.CreateTemp("", "")
f, err := os.OpenFile(m.name(hash), os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0600)

if err != nil {
return "", 0, err
Expand All @@ -84,10 +84,9 @@ func (m *BlobManager) PutFile(hash string, r io.Reader) (string, int64, error) {
hash1 := hex.EncodeToString(h.Sum(nil))

if hash != "" && hash != hash1 {
os.Remove(f.Name())
return "", 0, fmt.Errorf("invalid hash")
}

err = os.Rename(f.Name(), m.name(hash1))

return hash1, n, err
}
25 changes: 25 additions & 0 deletions staticfiles/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package staticfiles
import (
"embed"
"net/http"
"net/url"

"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/filesystem"
Expand All @@ -11,9 +12,33 @@ import (
//go:embed static
var staticFiles embed.FS

//go:embed webtak
var webtakFiles embed.FS
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do not embed webtak in the binary


type PathUnescapeFs struct {
fs http.FileSystem
}

func (c *PathUnescapeFs) Open(name string) (http.File, error) {
// 解码路径中的空格
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use english comments only

decodedName, err := url.PathUnescape(name)
if err != nil {
return nil, err
}
// 使用解码后的路径打开文件
return c.fs.Open(decodedName)
}

func Embed(f *fiber.App) {
f.Use("/static", filesystem.New(filesystem.Config{
Root: http.FS(staticFiles),
PathPrefix: "static",
}))
}

func EmbedWebTak(f *fiber.App) {
f.Use("/webtak", filesystem.New(filesystem.Config{
Root: &PathUnescapeFs{fs: http.FS(webtakFiles)},
PathPrefix: "webtak",
}))
}