update/teacher photo

This commit is contained in:
SowinskiBraeden committed 2022-02-14 14:03:49 -08:00
1 parent cca343a84d
commit 95b466f3ef
1 file changed
+113 -3
+113 -3
View File
@@ -1261,9 +1261,119 @@ func UpdateTeacherAddress(c *fiber.Ctx) error {
} }
func UpdateTeacherPhoto(c *fiber.Ctx) error { func UpdateTeacherPhoto(c *fiber.Ctx) error {
return c.Status(fiber.StatusNotImplemented).JSON(fiber.Map{ ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
"success": nil,
"message": "not implimented", //Ensure Authenticated admin sent request
if !AuthAdmin(c) {
cancel()
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
"success": false,
"message": "Unauthorized: only an admin can perform this action",
})
}
tid := c.FormValue("tid")
if tid == "" {
cancel()
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"success": false,
"message": "missing required fields",
})
}
// Get student
var teacher models.Teacher
findErr := teacherCollection.FindOne(context.TODO(), bson.M{"schooldata.tid": tid}).Decode(&teacher)
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": teacher.SchoolData.PhotoName}).Decode(&photo)
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,
})
}
// Read the entire file into a byte slice
bytes, 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 be read",
"error": err,
})
}
var base64Encoding string = toBase64(bytes)
// Update image name and base64 data
update_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
update := bson.M{
"$set": bson.M{
"base64": base64Encoding,
"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 image could not be updated",
"error": updateErr,
})
}
defer cancel()
// Remove local image
os.Remove(fmt.Sprintf("./database/images/%s", image))
return c.Status(fiber.StatusOK).JSON(fiber.Map{
"success": true,
"message": "successfully updated teacher photo",
"result": result,
}) })
} }