snow/handlers/storage.go

149 lines
3.5 KiB
Go
Raw Normal View History

package handlers
import (
2024-01-04 19:02:14 +00:00
"crypto/sha1"
"crypto/sha256"
"encoding/hex"
2024-05-29 06:57:26 +00:00
"os"
"path/filepath"
2024-02-17 14:40:17 +00:00
"time"
2024-01-04 19:02:14 +00:00
"github.com/ectrc/snow/aid"
2024-02-04 19:49:31 +00:00
"github.com/ectrc/snow/person"
2024-01-04 19:02:14 +00:00
"github.com/ectrc/snow/storage"
"github.com/gofiber/fiber/v2"
)
2024-02-04 19:49:31 +00:00
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",
2024-02-17 14:40:17 +00:00
"uploaded": time.Now().Format(time.RFC3339),
2024-02-04 19:49:31 +00:00
"storageType": "S3",
"storageIds": aid.JSON{
"primary": "primary",
},
2024-02-17 14:40:17 +00:00
"doNotCache": true,
2024-02-04 19:49:31 +00:00
})
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 {
2024-02-04 19:49:31 +00:00
lookup := map[string][]byte {
2024-01-31 16:20:26 +00:00
"DefaultEngine.ini": storage.GetDefaultEngine(),
"DefaultGame.ini": storage.GetDefaultGame(),
"DefaultRuntimeOptions.ini": storage.GetDefaultRuntime(),
}
2024-02-04 19:49:31 +00:00
files := cloudstorage{}
for name, bytes := range lookup {
files.Add(name, bytes)
2024-01-31 16:20:26 +00:00
}
2024-02-04 19:49:31 +00:00
return c.Status(200).JSON(files.Get())
}
func GetCloudStorageConfig(c *fiber.Ctx) error {
2024-01-31 21:40:47 +00:00
return c.Status(200).JSON(aid.JSON{
"enumerateFilesPath": "/api/cloudstorage/system",
2023-12-27 04:35:12 +00:00
"enableMigration": true,
"enableWrites": true,
"epicAppName": "Live",
"isAuthenticated": true,
"disableV2": true,
2024-01-20 01:58:57 +00:00
"lastUpdated": aid.TimeStartOfDay(),
2023-12-27 04:35:12 +00:00
"transports": []string{},
})
}
2024-05-29 06:57:26 +00:00
func GetAssets(c *fiber.Ctx) error {
fileName := c.Params("fileName")
path := filepath.Join("./data/files", 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 {
2024-01-31 21:40:47 +00:00
c.Set("Content-Type", "application/octet-stream")
2024-01-04 19:02:14 +00:00
switch c.Params("fileName") {
case "DefaultEngine.ini":
2024-01-31 21:40:47 +00:00
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())
2024-01-04 19:02:14 +00:00
}
2024-01-31 21:40:47 +00:00
return c.Status(404).JSON(aid.ErrorBadRequest("File not found"))
}
func GetUserStorageFiles(c *fiber.Ctx) error {
2024-02-04 19:49:31 +00:00
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 {
2024-02-04 19:49:31 +00:00
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 {
2024-02-04 19:49:31 +00:00
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)
}