Added Image metadata and made spice lvl editable
Also added db support
This commit is contained in:
@@ -1,57 +1,16 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"Advertisement_Panel/db"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
type FileData struct {
|
||||
ImageNames []string
|
||||
SpicyImageNames []string
|
||||
AsciiFiles []AsciiEntry
|
||||
}
|
||||
|
||||
type AsciiEntry struct {
|
||||
Name string
|
||||
FontSize int
|
||||
}
|
||||
|
||||
const staticDir = "static"
|
||||
|
||||
// move FileData and AsciiEntry here if you want, or leave in main.go
|
||||
func FileHandler(w http.ResponseWriter, r *http.Request) {
|
||||
data := FileData{
|
||||
ImageNames: []string{},
|
||||
SpicyImageNames: []string{},
|
||||
AsciiFiles: []AsciiEntry{},
|
||||
}
|
||||
var data []db.ImagesEntry
|
||||
|
||||
files, _ := os.ReadDir(filepath.Join(staticDir, "images"))
|
||||
for _, file := range files {
|
||||
fileName := file.Name()
|
||||
data.ImageNames = append(data.ImageNames, filepath.Join("/", staticDir, "images", fileName))
|
||||
}
|
||||
files, _ = os.ReadDir(filepath.Join("uploads"))
|
||||
for _, file := range files {
|
||||
fileName := file.Name()
|
||||
data.ImageNames = append(data.ImageNames, filepath.Join("/uploads", fileName))
|
||||
}
|
||||
|
||||
files, _ = os.ReadDir(filepath.Join(staticDir, "spicy"))
|
||||
for _, file := range files {
|
||||
fileName := file.Name()
|
||||
data.ImageNames = append(data.ImageNames, filepath.Join("/", staticDir, "spicy", fileName))
|
||||
}
|
||||
|
||||
files, _ = os.ReadDir(filepath.Join(staticDir, "ascii_art"))
|
||||
for _, file := range files {
|
||||
fileName := file.Name()
|
||||
data.AsciiFiles = append(data.AsciiFiles,
|
||||
AsciiEntry{Name: filepath.Join("/", staticDir, "ascii_art", fileName), FontSize: 12},
|
||||
)
|
||||
}
|
||||
data = db.GetImageMetadata()
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(data)
|
||||
|
||||
52
api/MetadataSaving.go
Normal file
52
api/MetadataSaving.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"Advertisement_Panel/db"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func SaveMetadataHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != "POST" {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to read request body", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
defer r.Body.Close()
|
||||
|
||||
var newMetadata []db.ImagesEntry
|
||||
log.Println(string(body))
|
||||
err = json.Unmarshal(body, &newMetadata)
|
||||
|
||||
if err != nil {
|
||||
log.Println("Error unmarshalling JSON:", err)
|
||||
http.Error(w, "Failed to parse JSON", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
var currentMetadata []db.ImagesEntry
|
||||
currentMetadata = db.GetImageMetadata()
|
||||
|
||||
for _, newEntry := range newMetadata {
|
||||
for _, currentEntry := range currentMetadata {
|
||||
if newEntry.ID == currentEntry.ID {
|
||||
if newEntry.SpiceLevel != currentEntry.SpiceLevel {
|
||||
_, err := db.DB.Exec("UPDATE images SET spice_level = $1 WHERE id = $2", newEntry.SpiceLevel, newEntry.ID)
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to update database", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"Advertisement_Panel/db"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
@@ -33,4 +34,5 @@ func UploadHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
fmt.Fprint(w, "<script>location.href = '/admin/'</script>")
|
||||
fmt.Fprintf(w, "Image uploaded successfully: %s", header.Filename)
|
||||
db.DB.Exec("INSERT INTO images (path, spice_level) VALUES ($1, $2)", "/uploads/"+header.Filename, 0)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user