debug/not-update-image

This commit is contained in:
SowinskiBraeden committed 2022-02-13 19:13:04 -08:00
1 parent b89d8b4744
commit 0b538158c7
4 files changed
+24 -36

No files matched your search

+3 -1
View File
@@ -138,6 +138,7 @@ func Enroll(c *fiber.Ctx) error {
offset++ offset++
} }
student.AccountData.SchoolEmail = schoolEmail student.AccountData.SchoolEmail = schoolEmail
student.AccountData.HashHistory = []string{}
// Disable login block // Disable login block
student.AccountData.AccountDisabled = false student.AccountData.AccountDisabled = false
@@ -732,10 +733,11 @@ func Student(c *fiber.Ctx) error {
} }
var photo models.Photo var photo models.Photo
findErr = imageCollection.FindOne(context.TODO(), bson.M{"_id": student.SchoolData.PhotoName}).Decode(&photo) findErr = imageCollection.FindOne(context.TODO(), bson.M{"name": student.SchoolData.PhotoName}).Decode(&photo)
if findErr != nil { if findErr != nil {
responceData["error"] = "Error! There was an error finding the student photo" responceData["error"] = "Error! There was an error finding the student photo"
} }
responceData["photo"] = photo
return c.Status(fiber.StatusAccepted).JSON(fiber.Map{ return c.Status(fiber.StatusAccepted).JSON(fiber.Map{
"success": true, "success": true,
+19 -33
View File
@@ -2,6 +2,7 @@ package controllers
import ( import (
"context" "context"
"encoding/base64"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net/smtp" "net/smtp"
@@ -22,6 +23,10 @@ import (
var lockerCollection *mongo.Collection = database.OpenCollection(database.Client, "lockers") var lockerCollection *mongo.Collection = database.OpenCollection(database.Client, "lockers")
var imageCollection *mongo.Collection = database.OpenCollection(database.Client, "images") var imageCollection *mongo.Collection = database.OpenCollection(database.Client, "images")
func toBase64(b []byte) string {
return base64.StdEncoding.EncodeToString(b)
}
func UpdateStudentName(c *fiber.Ctx) error { func UpdateStudentName(c *fiber.Ctx) error {
var data map[string]string var data map[string]string
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
@@ -274,7 +279,7 @@ func UpdateStudentPassword(c *fiber.Ctx) error {
} }
// Check required fields are included // Check required fields are included
if data["currentpassword"] == "" || data["newpassword1"] == "" || data["newpassword2"] == "" { if data["password"] == "" || data["newpassword1"] == "" || data["newpassword2"] == "" {
cancel() cancel()
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"success": false, "success": false,
@@ -282,7 +287,7 @@ func UpdateStudentPassword(c *fiber.Ctx) error {
}) })
} }
if !student.ComparePasswords(data["currentpassword"]) { if !student.ComparePasswords(data["password"]) {
cancel() cancel()
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
"success": false, "success": false,
@@ -806,7 +811,7 @@ func UpdateStudentPhoto(c *fiber.Ctx) error {
// Get student // Get student
var student models.Student var student models.Student
findErr := studentCollection.FindOne(context.TODO(), bson.M{"sid": sid}).Decode(&student) findErr := studentCollection.FindOne(context.TODO(), bson.M{"schooldata.sid": sid}).Decode(&student)
if findErr != nil { if findErr != nil {
cancel() cancel()
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
@@ -829,7 +834,7 @@ func UpdateStudentPhoto(c *fiber.Ctx) error {
// Get student photo // Get student photo
var photo models.Photo var photo models.Photo
findErr = imageCollection.FindOne(context.TODO(), bson.M{"name": student.SchoolData.PhotoName}).Decode(&student) findErr = imageCollection.FindOne(context.TODO(), bson.M{"name": student.SchoolData.PhotoName}).Decode(&photo)
if findErr != nil { if findErr != nil {
cancel() cancel()
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
@@ -854,25 +859,24 @@ func UpdateStudentPhoto(c *fiber.Ctx) error {
}) })
} }
// Get local image as base64 data // Read the entire file into a byte slice
imageData, err := ioutil.ReadFile(fmt.Sprintf("./database/images/%s", image)) bytes, err := ioutil.ReadFile(fmt.Sprintf("./database/images/%s", image))
if err != nil { if err != nil {
cancel() cancel()
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"success": false, "success": false,
"message": "the image could not read", "message": "the image could not be read",
"error": err, "error": err,
}) })
} }
// Remove local image
os.Remove(fmt.Sprintf("./database/images/%s", image)) var base64Encoding string = toBase64(bytes)
// Update image name and base64 data // Update image name and base64 data
update_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339)) update_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
update := bson.M{ update := bson.M{
"$set": bson.M{ "$set": bson.M{
"name": filename, "base64": base64Encoding,
"base64": imageData,
"updated_at": update_time, "updated_at": update_time,
}, },
} }
@@ -885,33 +889,15 @@ func UpdateStudentPhoto(c *fiber.Ctx) error {
cancel() cancel()
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"success": false, "success": false,
"message": "the student could not be updated", "message": "the image could not be updated",
"error": updateErr,
})
}
// 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,
)
if updateErr != nil {
cancel()
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"success": false,
"message": "the student could not be updated",
"error": updateErr, "error": updateErr,
}) })
} }
defer cancel() defer cancel()
// Remove local image
os.Remove(fmt.Sprintf("./database/images/%s", image))
return c.Status(fiber.StatusOK).JSON(fiber.Map{ return c.Status(fiber.StatusOK).JSON(fiber.Map{
"success": true, "success": true,
"message": "successfully updated student photo", "message": "successfully updated student photo",
File diff suppressed because one or more lines are too long.
+1 -1
View File
@@ -42,7 +42,7 @@ type Student struct {
Homeroom string `json:"homeroom"` Homeroom string `json:"homeroom"`
Locker string `json:"-"` // Locker ID Locker string `json:"-"` // Locker ID
YOG int `json:"yog"` // Year of Graduation YOG int `json:"yog"` // Year of Graduation
PhotoName string `json:"photo"` // name of photo in db PhotoName string `json:"photoname"` // name of photo in db
} `json:"schooldata"` } `json:"schooldata"`
AccountData struct { AccountData struct {
SchoolEmail string `json:"schoolemail"` SchoolEmail string `json:"schoolemail"`