Skip to content
Snippets Groups Projects
Commit 08b2158b authored by nicolas.albanesi's avatar nicolas.albanesi
Browse files

Changed directory

parent 899866d8
No related branches found
No related tags found
No related merge requests found
Makefile
all: display_backend
display_backend: main.go queue.c queue.h
CC=arm-linux-gcc GOARCH=arm GOOS=linux CGO_ENABLED=1 go build -o $@
install: display_backend
sudo cp display_backend ~/Documents/hepia/3eme_sem/embedded_linux/nfsroot/root/.
\ No newline at end of file
......@@ -27,6 +27,7 @@ import (
"github.com/labstack/echo/v4/middleware"
)
const SERVER_PATH = "/var/backendgo/"
func main() {
......@@ -37,7 +38,8 @@ func main() {
e.HideBanner = true
e.Use(middleware.BodyLimit("1M"))
os.Mkdir("images", os.ModePerm)
os.Mkdir(SERVER_PATH, os.ModePerm)
os.Mkdir(SERVER_PATH + "images", os.ModePerm)
e.GET("/list", listFiles)
e.GET("/download/:image", downloadFile)
......@@ -57,7 +59,7 @@ func listFiles(c echo.Context) error {
var buffer bytes.Buffer
// Read the files in a directory
files, err := ioutil.ReadDir("images")
files, err := ioutil.ReadDir(SERVER_PATH + "images")
if err != nil {
log.Fatal(err)
return c.String(http.StatusInternalServerError, "Failed to get files list")
......@@ -78,13 +80,13 @@ func deleteFile(c echo.Context) error {
filename := c.Param("image")
// Check if the file exists
_, err := os.Stat("images/" + filename)
_, err := os.Stat(SERVER_PATH + "images/" + filename)
if errors.Is(err, os.ErrNotExist) {
return c.String(http.StatusNotFound, "File " + filename + " not found.\n")
}
// Remove the file
os.Remove("images/" + filename)
os.Remove(SERVER_PATH + "images/" + filename)
return c.String(http.StatusOK, filename + " Deleted\n")
}
......@@ -106,7 +108,7 @@ func uploadFile(c echo.Context) error {
defer src.Close()
// Open destination file
dst, err := os.Create("images/" + file.Filename)
dst, err := os.Create(SERVER_PATH + "images/" + file.Filename)
if err != nil {
return err
}
......@@ -128,19 +130,19 @@ func downloadFile(c echo.Context) error {
filename := c.Param("image")
// Check if the file exists
_, err := os.Stat("images/" + filename)
_, err := os.Stat(SERVER_PATH + "images/" + filename)
if errors.Is(err, os.ErrNotExist) {
fmt.Println(err.Error())
return c.String(http.StatusNotFound, "File " + filename + " not found.\n")
}
return c.File("images/" + filename)
return c.File(SERVER_PATH + "images/" + filename)
}
// Convert a file from any format to .ppm and scale it to 240x320
func convertFile(filename string) {
basename := strings.Split(filename, ".")[0]
cmd := exec.Command("convert", "images/"+filename, "-scale", "240x320!", "images/"+basename+".ppm")
cmd := exec.Command("convert", SERVER_PATH+"images/"+filename, "-scale", "240x320!", SERVER_PATH+"images/"+basename+".ppm")
err := cmd.Run()
if err != nil {
......@@ -151,7 +153,7 @@ func convertFile(filename string) {
func displayImage(c echo.Context) error {
filename := c.Param("image")
// Check if the file exists
_, err := os.Stat("images/" + filename)
_, err := os.Stat(SERVER_PATH + "images/" + filename)
if errors.Is(err, os.ErrNotExist) && filename != "break" {
fmt.Println(err.Error())
return c.String(http.StatusNotFound, "File " + filename + " not found.\n")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment