2024-02-04 10:40:20 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2024-03-20 12:57:21 +00:00
|
|
|
"git.solsynth.dev/hydrogen/interactive/pkg/models"
|
|
|
|
"git.solsynth.dev/hydrogen/interactive/pkg/services"
|
2024-02-04 10:40:20 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
"path/filepath"
|
|
|
|
)
|
|
|
|
|
|
|
|
func openAttachment(c *fiber.Ctx) error {
|
|
|
|
id := c.Params("fileId")
|
|
|
|
basepath := viper.GetString("content")
|
|
|
|
|
|
|
|
return c.SendFile(filepath.Join(basepath, id))
|
|
|
|
}
|
|
|
|
|
|
|
|
func uploadAttachment(c *fiber.Ctx) error {
|
|
|
|
user := c.Locals("principal").(models.Account)
|
|
|
|
file, err := c.FormFile("attachment")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
attachment, err := services.NewAttachment(user, file)
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.SaveFile(file, attachment.GetStoragePath()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(fiber.Map{
|
|
|
|
"info": attachment,
|
|
|
|
"url": attachment.GetAccessPath(),
|
|
|
|
})
|
|
|
|
}
|