✨ Attachments
This commit is contained in:
38
pkg/server/attachments_api.go
Normal file
38
pkg/server/attachments_api.go
Normal file
@ -0,0 +1,38 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"code.smartsheep.studio/hydrogen/interactive/pkg/models"
|
||||
"code.smartsheep.studio/hydrogen/interactive/pkg/services"
|
||||
"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(),
|
||||
})
|
||||
}
|
@ -36,7 +36,7 @@ func doLogin(c *fiber.Ctx) error {
|
||||
})
|
||||
}
|
||||
|
||||
func doPostLogin(c *fiber.Ctx) error {
|
||||
func postLogin(c *fiber.Ctx) error {
|
||||
buildOauth2Config()
|
||||
code := c.Query("code")
|
||||
|
||||
|
@ -48,14 +48,15 @@ func createPost(c *fiber.Ctx) error {
|
||||
user := c.Locals("principal").(models.Account)
|
||||
|
||||
var data struct {
|
||||
Alias string `json:"alias"`
|
||||
Title string `json:"title"`
|
||||
Content string `json:"content" validate:"required"`
|
||||
Tags []models.Tag `json:"tags"`
|
||||
Categories []models.Category `json:"categories"`
|
||||
PublishedAt *time.Time `json:"published_at"`
|
||||
RepostTo uint `json:"repost_to"`
|
||||
ReplyTo uint `json:"reply_to"`
|
||||
Alias string `json:"alias"`
|
||||
Title string `json:"title"`
|
||||
Content string `json:"content" validate:"required"`
|
||||
Tags []models.Tag `json:"tags"`
|
||||
Categories []models.Category `json:"categories"`
|
||||
Attachments []models.Attachment `json:"attachments"`
|
||||
PublishedAt *time.Time `json:"published_at"`
|
||||
RepostTo uint `json:"repost_to"`
|
||||
ReplyTo uint `json:"reply_to"`
|
||||
}
|
||||
|
||||
if err := BindAndValidate(c, &data); err != nil {
|
||||
@ -94,6 +95,7 @@ func createPost(c *fiber.Ctx) error {
|
||||
data.Alias,
|
||||
data.Title,
|
||||
data.Content,
|
||||
data.Attachments,
|
||||
data.Categories,
|
||||
data.Tags,
|
||||
data.PublishedAt,
|
||||
|
@ -56,14 +56,17 @@ func NewServer() {
|
||||
|
||||
api := A.Group("/api").Name("API")
|
||||
{
|
||||
api.Get("/auth", doLogin)
|
||||
api.Get("/auth/callback", postLogin)
|
||||
api.Post("/auth/refresh", doRefreshToken)
|
||||
|
||||
api.Get("/users/me", auth, getUserinfo)
|
||||
api.Get("/users/:accountId", getOthersInfo)
|
||||
api.Get("/users/:accountId/follow", auth, getAccountFollowed)
|
||||
api.Post("/users/:accountId/follow", auth, doFollowAccount)
|
||||
|
||||
api.Get("/auth", doLogin)
|
||||
api.Get("/auth/callback", doPostLogin)
|
||||
api.Post("/auth/refresh", doRefreshToken)
|
||||
api.Get("/attachments/o/:fileId", openAttachment)
|
||||
api.Post("/attachments", auth, uploadAttachment)
|
||||
|
||||
api.Get("/posts", listPost)
|
||||
api.Post("/posts", auth, createPost)
|
||||
|
Reference in New Issue
Block a user