initial commit

This commit is contained in:
stratic-dev
2024-03-12 23:00:01 +00:00
parent 19aadc6e10
commit def2bc3d2c
9 changed files with 157 additions and 23 deletions

View File

@@ -6,7 +6,12 @@ import (
"errors"
_config "erupe-ce/config"
"erupe-ce/server/channelserver"
"fmt"
"image"
"image/jpeg"
"net/http"
"os"
"path/filepath"
"strings"
"time"
@@ -286,3 +291,66 @@ func (s *Server) ExportSave(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
json.NewEncoder(w).Encode(save)
}
func (s *Server) ScreenShot(w http.ResponseWriter, r *http.Request) {
if !s.erupeConfig.SaveDumps.Enabled {
http.Error(w, "Screenshots not enabled in Config", http.StatusBadRequest)
return
} else {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
// Get File from Request
file, _, err := r.FormFile("img")
if err != nil {
http.Error(w, "No valid file uploaded", http.StatusBadRequest)
return
}
token := r.FormValue("token")
if token == "" {
http.Error(w, "Token not specified cannot continue", http.StatusBadRequest)
return
}
// Validate file
img, _, err := image.Decode(file)
if err != nil {
http.Error(w, "Invalid image file", http.StatusBadRequest)
return
}
dir := filepath.Join(s.erupeConfig.Screenshots.OutputDir)
path := filepath.Join(s.erupeConfig.Screenshots.OutputDir, fmt.Sprintf("%s.jpg", token))
_, err = os.Stat(dir)
if err != nil {
if os.IsNotExist(err) {
err = os.MkdirAll(dir, os.ModePerm)
if err != nil {
s.logger.Error("Error writing screenshot, could not create folder")
return
}
} else {
s.logger.Error("Error writing screenshot")
return
}
}
// Create or open the output file
outputFile, err := os.Create(path)
if err != nil {
panic(err)
}
defer outputFile.Close()
// Encode the image and write it to the file
err = jpeg.Encode(outputFile, img, &jpeg.Options{})
if err != nil {
panic(err)
}
if err != nil {
s.logger.Error("Error writing screenshot, could not write file", zap.Error(err))
}
}
}

View File

@@ -2,7 +2,7 @@ package signv2server
import (
"context"
"erupe-ce/config"
_config "erupe-ce/config"
"fmt"
"net/http"
"os"
@@ -52,6 +52,7 @@ func (s *Server) Start() error {
r.HandleFunc("/character/create", s.CreateCharacter)
r.HandleFunc("/character/delete", s.DeleteCharacter)
r.HandleFunc("/character/export", s.ExportSave)
r.HandleFunc("/api/ss/bbs/upload.php", s.ScreenShot)
handler := handlers.CORS(handlers.AllowedHeaders([]string{"Content-Type"}))(r)
s.httpServer.Handler = handlers.LoggingHandler(os.Stdout, handler)
s.httpServer.Addr = fmt.Sprintf(":%d", s.erupeConfig.SignV2.Port)