fix: remove double image processing in upload handler
Frontend now compresses and strips metadata before upload. Skip backend re-encoding to preserve quality and reduce latency. Extract independent extensions for main file vs thumbnail. Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
@@ -556,7 +556,7 @@ func UploadImage(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
thumbFile, _, err := r.FormFile("thumbnail")
|
thumbFile, thumbHeader, err := r.FormFile("thumbnail")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Failed to get thumbnail: %v", err)
|
log.Printf("Failed to get thumbnail: %v", err)
|
||||||
http.Error(w, "No thumbnail provided", http.StatusBadRequest)
|
http.Error(w, "No thumbnail provided", http.StatusBadRequest)
|
||||||
@@ -575,13 +575,21 @@ func UploadImage(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
// Use nanosecond timestamp for unique keys
|
// Use nanosecond timestamp for unique keys
|
||||||
timestamp := time.Now().UnixNano()
|
timestamp := time.Now().UnixNano()
|
||||||
ext := ".jpg"
|
|
||||||
|
// Extract extension from main file
|
||||||
|
mainExt := ".jpg"
|
||||||
if idx := strings.LastIndex(header.Filename, "."); idx != -1 {
|
if idx := strings.LastIndex(header.Filename, "."); idx != -1 {
|
||||||
ext = strings.ToLower(header.Filename[idx:])
|
mainExt = strings.ToLower(header.Filename[idx:])
|
||||||
}
|
}
|
||||||
|
|
||||||
key := fmt.Sprintf("portfolio/%d%s", timestamp, ext)
|
// Extract extension from thumbnail file
|
||||||
thumbKey := fmt.Sprintf("portfolio/%d_thumb%s", timestamp, ext)
|
thumbExt := ".jpg"
|
||||||
|
if idx := strings.LastIndex(thumbHeader.Filename, "."); idx != -1 {
|
||||||
|
thumbExt = strings.ToLower(thumbHeader.Filename[idx:])
|
||||||
|
}
|
||||||
|
|
||||||
|
key := fmt.Sprintf("portfolio/%d%s", timestamp, mainExt)
|
||||||
|
thumbKey := fmt.Sprintf("portfolio/%d_thumb%s", timestamp, thumbExt)
|
||||||
|
|
||||||
fileBytes, err := io.ReadAll(file)
|
fileBytes, err := io.ReadAll(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -589,14 +597,8 @@ func UploadImage(w http.ResponseWriter, r *http.Request) {
|
|||||||
http.Error(w, "Failed to read file", http.StatusInternalServerError)
|
http.Error(w, "Failed to read file", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
// Images are already processed by frontend (compressed, metadata stripped)
|
||||||
// Strip metadata (EXIF, GPS, camera info) and auto-orient
|
// Skip re-encoding - just use the uploaded bytes directly
|
||||||
fileBytes, err = processImage(fileBytes, 85)
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("Failed to process image: %v", err)
|
|
||||||
http.Error(w, "Failed to process image", http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
thumbBytes, err := io.ReadAll(thumbFile)
|
thumbBytes, err := io.ReadAll(thumbFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -605,13 +607,7 @@ func UploadImage(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Strip metadata from thumbnail too
|
// Thumbnail is already processed by frontend - use as-is
|
||||||
thumbBytes, err = processImage(thumbBytes, 75)
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("Failed to process thumbnail: %v", err)
|
|
||||||
http.Error(w, "Failed to process thumbnail", http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
bucket := "crussell"
|
bucket := "crussell"
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user