snow/handlers/storage.go
2024-05-29 00:08:16 -07:00

149 lines
3.5 KiB
Go

package handlers
import (
"crypto/sha1"
"crypto/sha256"
"encoding/hex"
"os"
"path/filepath"
"time"
"github.com/ectrc/snow/aid"
"github.com/ectrc/snow/person"
"github.com/ectrc/snow/storage"
"github.com/gofiber/fiber/v2"
)
type cloudstorage struct {
f []aid.JSON
}
func (c *cloudstorage) Add(name string, bytes []byte) error {
sumation1 := sha1.Sum(bytes)
sumation256 := sha256.Sum256(bytes)
c.f = append(c.f, aid.JSON{
"uniqueFilename": name,
"filename": name,
"hash": hex.EncodeToString(sumation1[:]),
"hash256": hex.EncodeToString(sumation256[:]),
"length": len(bytes),
"contentType": "application/octet-stream",
"uploaded": time.Now().Format(time.RFC3339),
"storageType": "S3",
"storageIds": aid.JSON{
"primary": "primary",
},
"doNotCache": true,
})
return nil
}
func (c *cloudstorage) Get() []aid.JSON {
if c.f == nil {
c.f = []aid.JSON{}
}
return c.f
}
func GetCloudStorageFiles(c *fiber.Ctx) error {
lookup := map[string][]byte {
"DefaultEngine.ini": storage.GetDefaultEngine(),
"DefaultGame.ini": storage.GetDefaultGame(),
"DefaultRuntimeOptions.ini": storage.GetDefaultRuntime(),
}
files := cloudstorage{}
for name, bytes := range lookup {
files.Add(name, bytes)
}
return c.Status(200).JSON(files.Get())
}
func GetCloudStorageConfig(c *fiber.Ctx) error {
return c.Status(200).JSON(aid.JSON{
"enumerateFilesPath": "/api/cloudstorage/system",
"enableMigration": true,
"enableWrites": true,
"epicAppName": "Live",
"isAuthenticated": true,
"disableV2": true,
"lastUpdated": aid.TimeStartOfDay(),
"transports": []string{},
})
}
func GetAssets(c *fiber.Ctx) error {
fileName := c.Params("fileName")
path := filepath.Join("./data/", fileName)
_, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
return c.Status(404).JSON(aid.ErrorBadRequest("File not found"))
}
return c.Status(fiber.StatusInternalServerError).JSON(aid.ErrorInternalServer)
}
return c.SendFile(path)
}
func GetCloudStorageFile(c *fiber.Ctx) error {
c.Set("Content-Type", "application/octet-stream")
switch c.Params("fileName") {
case "DefaultEngine.ini":
return c.Status(200).Send(storage.GetDefaultEngine())
case "DefaultGame.ini":
return c.Status(200).Send(storage.GetDefaultGame())
case "DefaultRuntimeOptions.ini":
return c.Status(200).Send(storage.GetDefaultRuntime())
}
return c.Status(404).JSON(aid.ErrorBadRequest("File not found"))
}
func GetUserStorageFiles(c *fiber.Ctx) error {
if !aid.Config.Amazon.Enabled {
return c.Status(200).JSON([]aid.JSON{})
}
person := c.Locals("person").(*person.Person)
files := cloudstorage{}
file, err := storage.Repo.Amazon.GetUserFile(person.ID)
if err == nil {
files.Add("ClientSettings.sav", file)
}
return c.Status(200).JSON(files.Get())
}
func GetUserStorageFile(c *fiber.Ctx) error {
if !aid.Config.Amazon.Enabled {
return c.SendStatus(204)
}
person := c.Locals("person").(*person.Person)
file, err := storage.Repo.Amazon.GetUserFile(person.ID)
if err != nil {
return c.Status(500).JSON(aid.ErrorBadRequest("Failed to retrieve user file"))
}
c.Set("Content-Type", "application/octet-stream")
return c.Status(200).Send(file)
}
func PutUserStorageFile(c *fiber.Ctx) error {
if !aid.Config.Amazon.Enabled {
return c.SendStatus(204)
}
person := c.Locals("person").(*person.Person)
body := c.Body()
err := storage.Repo.Amazon.CreateUserFile(person.ID, body)
if err != nil {
return c.Status(500).JSON(aid.ErrorBadRequest("Failed to create user file"))
}
return c.SendStatus(204)
}