You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
1.5 KiB
66 lines
1.5 KiB
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"io/ioutil"
|
|
"os"
|
|
"log"
|
|
"github.com/gofiber/fiber/v2"
|
|
)
|
|
|
|
var DestinationPath string
|
|
|
|
func main() {
|
|
log.Println("Starting Catchy Cat")
|
|
DestinationPath = os.Getenv("DESTINATION_PATH")
|
|
if DestinationPath == "" {
|
|
DestinationPath = "./"
|
|
}
|
|
log.Println("DestinationPath:", DestinationPath)
|
|
|
|
log.Println("Starting Fiber...")
|
|
app := fiber.New()
|
|
|
|
app.Get("/", func(c *fiber.Ctx) error {
|
|
return c.SendString("Catchy-Cat, Listening!")
|
|
})
|
|
|
|
app.Post("/", HandlePostRequest)
|
|
port := os.Getenv("PORT")
|
|
|
|
app.Listen(fmt.Sprintf(":%s", port))
|
|
}
|
|
|
|
func HandlePostRequest(c *fiber.Ctx) error {
|
|
file, err := c.FormFile("file")
|
|
if err != nil {
|
|
return c.Status(fiber.StatusBadRequest).SendString("File Not found in file arg")
|
|
}
|
|
buffer, err := file.Open()
|
|
form, err:=c.MultipartForm()
|
|
log.Printf("%d files present in the multipart form", len(form.File))
|
|
if err != nil {
|
|
return c.Status(fiber.StatusBadRequest).SendString("Could not open file")
|
|
}
|
|
|
|
defer buffer.Close()
|
|
|
|
f, err := os.Create(fmt.Sprintf("%s/%s", DestinationPath, file.Filename))
|
|
if err != nil {
|
|
return c.Status(fiber.StatusBadRequest).SendString("Could not Create file")
|
|
}
|
|
|
|
data, err := ioutil.ReadAll(buffer)
|
|
|
|
if err != nil {
|
|
return c.Status(fiber.StatusBadRequest).SendString("Failed reading buffer")
|
|
}
|
|
|
|
_, err = f.Write(data)
|
|
|
|
if err != nil {
|
|
return c.Status(fiber.StatusBadRequest).SendString("Failed writing file")
|
|
}
|
|
|
|
return c.SendString("Catchy-Cat, File Caught!")
|
|
}
|
|
|