|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "os" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/gofiber/fiber/v2" |
| 10 | + "github.com/gofiber/fiber/v2/middleware/cors" |
| 11 | + "github.com/google/uuid" |
| 12 | +) |
| 13 | + |
| 14 | +func main() { |
| 15 | + // create new fiber instance and use across whole app |
| 16 | + app := fiber.New() |
| 17 | + |
| 18 | + // middleware to allow all clients to communicate using http and allow cors |
| 19 | + app.Use(cors.New()) |
| 20 | + |
| 21 | + // serve images from images directory prefixed with /images |
| 22 | + // i.e http://localhost:4000/images/someimage.webp |
| 23 | + |
| 24 | + app.Static("/images", "./images") |
| 25 | + |
| 26 | + // handle image uploading using post request |
| 27 | + |
| 28 | + app.Post("/", handleFileupload) |
| 29 | + |
| 30 | + // delete uploaded image by providing unique image name |
| 31 | + |
| 32 | + app.Delete("/:imageName", handleDeleteImage) |
| 33 | + |
| 34 | + // start dev server on port 4000 |
| 35 | + |
| 36 | + log.Fatal(app.Listen(":4000")) |
| 37 | +} |
| 38 | + |
| 39 | +func handleFileupload(c *fiber.Ctx) error { |
| 40 | + |
| 41 | + // parse incomming image file |
| 42 | + |
| 43 | + file, err := c.FormFile("image") |
| 44 | + |
| 45 | + if err != nil { |
| 46 | + log.Println("image upload error --> ", err) |
| 47 | + return c.JSON(fiber.Map{"status": 500, "message": "Server error", "data": nil}) |
| 48 | + |
| 49 | + } |
| 50 | + |
| 51 | + // generate new uuid for image name |
| 52 | + uniqueId := uuid.New() |
| 53 | + |
| 54 | + // remove "- from imageName" |
| 55 | + |
| 56 | + filename := strings.Replace(uniqueId.String(), "-", "", -1) |
| 57 | + |
| 58 | + // extract image extension from original file filename |
| 59 | + |
| 60 | + fileExt := strings.Split(file.Filename, ".")[1] |
| 61 | + |
| 62 | + // generate image from filename and extension |
| 63 | + image := fmt.Sprintf("%s.%s", filename, fileExt) |
| 64 | + |
| 65 | + // save image to ./images dir |
| 66 | + err = c.SaveFile(file, fmt.Sprintf("./images/%s", image)) |
| 67 | + |
| 68 | + if err != nil { |
| 69 | + log.Println("image save error --> ", err) |
| 70 | + return c.JSON(fiber.Map{"status": 500, "message": "Server error", "data": nil}) |
| 71 | + } |
| 72 | + |
| 73 | + // generate image url to serve to client using CDN |
| 74 | + |
| 75 | + imageUrl := fmt.Sprintf("http://localhost:4000/images/%s", image) |
| 76 | + |
| 77 | + // create meta data and send to client |
| 78 | + |
| 79 | + data := map[string]interface{}{ |
| 80 | + |
| 81 | + "imageName": image, |
| 82 | + "imageUrl": imageUrl, |
| 83 | + "header": file.Header, |
| 84 | + "size": file.Size, |
| 85 | + } |
| 86 | + |
| 87 | + return c.JSON(fiber.Map{"status": 201, "message": "Image uploaded successfully", "data": data}) |
| 88 | +} |
| 89 | + |
| 90 | +func handleDeleteImage(c *fiber.Ctx) error { |
| 91 | + // extract image name from params |
| 92 | + imageName := c.Params("imageName") |
| 93 | + |
| 94 | + // delete image from ./images |
| 95 | + err := os.Remove(fmt.Sprintf("./images/%s", imageName)) |
| 96 | + if err != nil { |
| 97 | + log.Println(err) |
| 98 | + return c.JSON(fiber.Map{"status": 500, "message": "Server Error", "data": nil}) |
| 99 | + } |
| 100 | + |
| 101 | + return c.JSON(fiber.Map{"status": 201, "message": "Image deleted successfully", "data": nil}) |
| 102 | +} |
0 commit comments