fixing image display one step at a time. Removing .DS_Store
This commit is contained in:
parent
189d519364
commit
5c91623d25
|
@ -1,4 +1,4 @@
|
||||||
|
|
||||||
data/*
|
data/*
|
||||||
images/*
|
images/*
|
||||||
.DS_Store
|
.DS_Store
|
||||||
|
|
18
handlers.go
18
handlers.go
|
@ -231,20 +231,17 @@ func GetItemImageHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
// Retrieve the item from the database
|
// Retrieve the item from the database
|
||||||
var item Item
|
var item Item
|
||||||
if err := db.First(&item, itemID).Error; err != nil {
|
if err := db.First(&item, itemID).Error; err != nil {
|
||||||
http.Error(w, "Item not found", http.StatusNotFound)
|
item.ImagePath = "images/default.jpg"
|
||||||
return
|
} else if item.ImagePath == "" {
|
||||||
}
|
item.ImagePath = "images/default.jpg"
|
||||||
|
|
||||||
// Check if the item has an associated image
|
|
||||||
if item.ImagePath == "" {
|
|
||||||
http.Error(w, "Item has no image", http.StatusNotFound)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Open the image file
|
// Open the image file
|
||||||
imageFile, err := os.Open(item.ImagePath)
|
imageFile, err := os.Open(item.ImagePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "Unable to open image file", http.StatusInternalServerError)
|
// Log the error for debugging, but don't return an HTTP error
|
||||||
|
fmt.Println("Error opening image.", err)
|
||||||
|
item.ImagePath = "images/default.png"
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer imageFile.Close()
|
defer imageFile.Close()
|
||||||
|
@ -252,7 +249,8 @@ func GetItemImageHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
// Determine the content type of the image
|
// Determine the content type of the image
|
||||||
imageData, err := io.ReadAll(imageFile)
|
imageData, err := io.ReadAll(imageFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "Unable to read image file", http.StatusInternalServerError)
|
fmt.Println("Error reading image")
|
||||||
|
item.ImagePath = "images/default.png"
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
contentType := http.DetectContentType(imageData)
|
contentType := http.DetectContentType(imageData)
|
||||||
|
|
Loading…
Reference in New Issue