Skip to content
This repository has been archived by the owner on Sep 22, 2024. It is now read-only.

ODLC Endpoints #29

Merged
merged 3 commits into from
May 11, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
72 changes: 72 additions & 0 deletions controllers/image_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package controllers

import (
"fmt"
"gcom-backend/models"
"github.com/labstack/echo/v4"
"gorm.io/gorm"
"net/http"
"os"
"regexp"
"strconv"
)

var imgDirectory = "./imgs/"

func UploadImage(c echo.Context) error {
file, err := c.FormFile("file")
if err != nil {
return c.JSON(http.StatusBadRequest, err.Error())
}

src, err := file.Open()
if err != nil {
return c.JSON(http.StatusBadRequest, err.Error())
}
defer src.Close()

match, err := regexp.MatchString("(\\d{10})", file.Filename)
if err != nil || !match {
return c.JSON(http.StatusBadRequest, "Invalid image name, use UNIX timestamp")
}
dst, err := os.Create(imgDirectory + file.Filename)
if err != nil {
return c.JSON(http.StatusBadRequest, "Error saving image")
}
defer dst.Close()

timestamp, _ := strconv.Atoi(file.Filename[:10])
fmt.Println(file.Filename[:13])
fmt.Println(timestamp)
image := &models.Image{
Timestamp: int64(timestamp),
Filename: file.Filename,
}

db, _ := c.Get("db").(*gorm.DB)
if createErr := db.Create(&image).Error; createErr != nil {
return c.JSON(http.StatusInternalServerError, createErr.Error())
}

return c.JSON(http.StatusAccepted, "Upload sucessful")
}

func ListImages(c echo.Context) error {
var images []models.Image
db, _ := c.Get("db").(*gorm.DB)
if err := db.Find(&images).Error; err != nil {
return c.JSON(http.StatusInternalServerError, err.Error())
}

return c.JSON(http.StatusOK, images)
}

func GetImage(c echo.Context) error {
imgPath, err := os.Stat(imgDirectory + c.Param("filename"))
fmt.Println(imgPath.Name())
if err != nil {
return c.JSON(http.StatusBadRequest, err.Error())
} else {
return c.Attachment(imgDirectory+c.Param("filename"), c.Param("filename"))
}
}
28 changes: 14 additions & 14 deletions controllers/waypoint_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ func CreateWaypoint(c echo.Context) error {
}

return c.JSON(http.StatusOK, responses.SingleResponse[models.Waypoint]{
Message: "Waypoint created!",
Model: waypoint})
Message: "Waypoint created!",
Model: waypoint})
}

// CreateWaypointBatch creates multiple waypoints
Expand Down Expand Up @@ -121,8 +121,8 @@ func CreateWaypointBatch(c echo.Context) error {
}

return c.JSON(http.StatusOK, responses.MultipleResponse[models.Waypoint]{
Message: "Waypoints created!",
Models: waypoints})
Message: "Waypoints created!",
Models: waypoints})
}

// EditWaypoint edits a waypoint
Expand Down Expand Up @@ -199,8 +199,8 @@ func EditWaypoint(c echo.Context) error {
db.First(&updatedWaypoint, waypointId)

return c.JSON(http.StatusOK, responses.SingleResponse[models.Waypoint]{
Message: "Waypoint updated!",
Model: updatedWaypoint,
Message: "Waypoint updated!",
Model: updatedWaypoint,
})
}

Expand Down Expand Up @@ -230,8 +230,8 @@ func GetWaypoint(c echo.Context) error {
}

return c.JSON(http.StatusOK, responses.SingleResponse[models.Waypoint]{
Message: "Waypoint found!",
Model: waypoint,
Message: "Waypoint found!",
Model: waypoint,
})
}

Expand Down Expand Up @@ -267,8 +267,8 @@ func DeleteWaypoint(c echo.Context) error {
}

return c.JSON(http.StatusOK, responses.SingleResponse[models.Waypoint]{
Message: "Waypoint deleted!",
Model: models.Waypoint{},
Message: "Waypoint deleted!",
Model: models.Waypoint{},
})
}

Expand Down Expand Up @@ -329,8 +329,8 @@ func DeleteWaypointBatch(c echo.Context) error {
}

return c.JSON(http.StatusOK, responses.SingleResponse[models.Waypoint]{
Message: "Waypoints deleted!",
Model: models.Waypoint{},
Message: "Waypoints deleted!",
Model: models.Waypoint{},
})
}

Expand Down Expand Up @@ -360,7 +360,7 @@ func GetAllWaypoints(c echo.Context) error {
}

return c.JSON(http.StatusOK, responses.MultipleResponse[models.Waypoint]{
Message: "Waypoint found!",
Models: waypoints,
Message: "Images found!",
Models: waypoints,
})
}
13 changes: 13 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package main

import (
"fmt"
"gcom-backend/configs"
"gcom-backend/controllers"
_ "gcom-backend/docs"
"gcom-backend/util"
"log"
"os"

"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
Expand All @@ -28,6 +30,12 @@ import (

func main() {
db := configs.Connect(false)
err := os.MkdirAll("db", 0755) //Create db dir
err = os.MkdirAll("imgs", 0755) //Create images dir
if err != nil {
fmt.Println(err)
return
}

mp, err := configs.ConnectMissionPlanner("http://host.docker.internal:9000")
if err != nil {
Expand Down Expand Up @@ -80,6 +88,11 @@ func main() {
e.DELETE("/groundobjects", controllers.DeleteGroundObjectBatch)
e.GET("/groundobjects", controllers.GetAllGroundObjects)

//Image Handling
e.POST("/image", controllers.UploadImage)
e.GET("/image/list", controllers.ListImages)
e.GET("/image/:filename", controllers.GetImage)

//Websockets
e.Any("/socket.io/", controllers.WebsocketHandler())

Expand Down
6 changes: 6 additions & 0 deletions models/image_model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package models

type Image struct {
Timestamp int64 `json:"timestamp" gorm:"primaryKey" validate:"required" example:"1698544781" extensions:"x-order=1"`
Filename string `json:"filename" example:"1714898050.png" extensions:"x-order=2"`
}
2 changes: 1 addition & 1 deletion models/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import "gorm.io/gorm"
*/

func Migrate(db *gorm.DB) {
err := db.AutoMigrate(&Waypoint{}, &Drone{}, &GroundObject{})
err := db.AutoMigrate(&Waypoint{}, &Drone{}, &GroundObject{}, &Image{})
if err != nil {
panic(err)
}
Expand Down
4 changes: 2 additions & 2 deletions responses/multple_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ package responses
//
// @Description Describes a response with multiple waypoints
type MultipleResponse[T any] struct {
Message string `json:"message" example:"Sample success message"`
Models []T `json:"waypoints"`
Message string `json:"message" example:"Sample success message"`
Models []T `json:"models"`
}
Loading