forked from albertba/Advertisement_Panel
Changed file structure to align with the recommendations given by https://github.com/golang-standards/project-layout
This commit is contained in:
1
api/Admin.go
Normal file
1
api/Admin.go
Normal file
@@ -0,0 +1 @@
|
||||
package api
|
||||
58
api/FileHandlers.go
Normal file
58
api/FileHandlers.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"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{},
|
||||
}
|
||||
|
||||
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.SpicyImageNames = append(data.SpicyImageNames, 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},
|
||||
)
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(data)
|
||||
}
|
||||
36
api/UploadHandlers.go
Normal file
36
api/UploadHandlers.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
func UploadHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != "POST" {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
file, header, err := r.FormFile("image") // "image" is the name of the file input
|
||||
if err != nil {
|
||||
http.Error(w, "Error retrieving file", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
dst, err := os.Create("./uploads/" + header.Filename)
|
||||
if err != nil {
|
||||
http.Error(w, "Error creating file on server", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer dst.Close()
|
||||
|
||||
if _, err := io.Copy(dst, file); err != nil {
|
||||
http.Error(w, "Error saving file", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
fmt.Fprint(w, "<script>location.href = '/admin/'</script>")
|
||||
fmt.Fprintf(w, "Image uploaded successfully: %s", header.Filename)
|
||||
}
|
||||
Reference in New Issue
Block a user