set default image and update image
This commit is contained in:
1 parent
eaae56b8f6
commit
0ef3142546
7 files changed
+164
-23
No files matched your search
@@ -2,13 +2,18 @@ package controllers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/smtp"
|
||||
"os"
|
||||
"school-management/database"
|
||||
"school-management/models"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/dgrijalva/jwt-go"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/google/uuid"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
@@ -791,7 +796,6 @@ func UpdateStudentPhoto(c *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
sid := c.FormValue("sid")
|
||||
|
||||
if sid == "" {
|
||||
cancel()
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
||||
@@ -800,17 +804,100 @@ func UpdateStudentPhoto(c *fiber.Ctx) error {
|
||||
})
|
||||
}
|
||||
|
||||
// get image
|
||||
// Get student
|
||||
var student models.Student
|
||||
findErr := studentCollection.FindOne(context.TODO(), bson.M{"sid": sid}).Decode(&student)
|
||||
if findErr != nil {
|
||||
cancel()
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||
"success": false,
|
||||
"message": "the student could not be found",
|
||||
"error": findErr,
|
||||
})
|
||||
}
|
||||
|
||||
// Collect image
|
||||
file, err := c.FormFile("image")
|
||||
if err != nil {
|
||||
cancel()
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||
"success": false,
|
||||
"message": "the image could not be retrieved",
|
||||
"error": err,
|
||||
})
|
||||
}
|
||||
|
||||
// Get student photo
|
||||
var photo models.Photo
|
||||
findErr = imageCollection.FindOne(context.TODO(), bson.M{"name": student.SchoolData.PhotoName}).Decode(&student)
|
||||
if findErr != nil {
|
||||
cancel()
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||
"success": false,
|
||||
"message": "the student image could not be found",
|
||||
"error": findErr,
|
||||
})
|
||||
}
|
||||
|
||||
// Save image to local
|
||||
uniqueId := uuid.New()
|
||||
filename := strings.Replace(uniqueId.String(), "-", "", -1)
|
||||
fileExt := strings.Split(file.Filename, ".")[1]
|
||||
image := fmt.Sprintf("%s.%s", filename, fileExt)
|
||||
err = c.SaveFile(file, fmt.Sprintf("./database/images/%s", image))
|
||||
if err != nil {
|
||||
cancel()
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||
"success": false,
|
||||
"message": "the image could not be saved",
|
||||
"error": err,
|
||||
})
|
||||
}
|
||||
|
||||
// Get local image as base64 data
|
||||
imageData, err := ioutil.ReadFile(fmt.Sprintf("./database/images/%s", image))
|
||||
if err != nil {
|
||||
cancel()
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||
"success": false,
|
||||
"message": "the image could not read",
|
||||
"error": err,
|
||||
})
|
||||
}
|
||||
// Remove local image
|
||||
os.Remove(fmt.Sprintf("./database/images/%s", image))
|
||||
|
||||
// Update image name and base64 data
|
||||
update_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
|
||||
update := bson.M{
|
||||
"$set": bson.M{
|
||||
"schooldata.Photo": nil,
|
||||
"updated_at": update_time,
|
||||
"name": filename,
|
||||
"base64": imageData,
|
||||
"updated_at": update_time,
|
||||
},
|
||||
}
|
||||
result, updateErr := imageCollection.UpdateOne(
|
||||
ctx,
|
||||
bson.M{"_id": photo.ID},
|
||||
update,
|
||||
)
|
||||
if updateErr != nil {
|
||||
cancel()
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||
"success": false,
|
||||
"message": "the student could not be updated",
|
||||
"error": updateErr,
|
||||
})
|
||||
}
|
||||
|
||||
result, updateErr := studentCollection.UpdateOne(
|
||||
// Update student photo name reference
|
||||
update = bson.M{
|
||||
"$set": bson.M{
|
||||
"SchoolData.PhotoName": filename,
|
||||
"updated_at": update_time,
|
||||
},
|
||||
}
|
||||
result, updateErr = studentCollection.UpdateOne(
|
||||
ctx,
|
||||
bson.M{"schooldata.sid": sid},
|
||||
update,
|
||||
@@ -827,7 +914,7 @@ func UpdateStudentPhoto(c *fiber.Ctx) error {
|
||||
|
||||
return c.Status(fiber.StatusOK).JSON(fiber.Map{
|
||||
"success": true,
|
||||
"message": "successfully uploaded image",
|
||||
"message": "successfully updated student photo",
|
||||
"result": result,
|
||||
})
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user