Skip to content

πŸ“š The open-source, offline-first Notion, Obsidian & Confluence alternative. Advanced Markdown, multi-tenant teams, OIDC/SSO, and local S3 backups. Deploy in one command.

Cleanup old routes & functions

Smaug6739cf00c1cec970
diff --git a/backend/controllers/ressources.controller.go b/backend/controllers/ressources.controller.go
index 3737c1cf..6e16cf38 100644
--- a/backend/controllers/ressources.controller.go
+++ b/backend/controllers/ressources.controller.go
@@ -1,127 +1,109 @@
package controllers
import (
-"alexandrie/app"
-"alexandrie/permissions"
-"alexandrie/utils"
-"errors"
-"io"
-"net/http"
-
-"github.com/gin-gonic/gin"
+ "alexandrie/app"
+ "alexandrie/permissions"
+ "alexandrie/utils"
+ "errors"
+ "io"
+ "net/http"
+
+ "github.com/gin-gonic/gin"
)
type RessourceController interface {
-GetBackup(c *gin.Context) (int, any)
-UploadFile(c *gin.Context) (int, any)
-UploadAvatar(c *gin.Context) (int, any)
-DeleteUpload(c *gin.Context) (int, any)
+ GetBackup(c *gin.Context) (int, any)
+ UploadFile(c *gin.Context) (int, any)
+ UploadAvatar(c *gin.Context) (int, any)
}
func NewRessourceController(app *app.App) RessourceController {
-return &Controller{
-app: app,
-authorizer: permissions.NewAuthorizer(app.Repos.Permission),
-}
+ return &Controller{
+ app: app,
+ authorizer: permissions.NewAuthorizer(app.Repos.Permission),
+ }
}
func (ctr *Controller) GetBackup(c *gin.Context) (int, any) {
-userId, err := utils.GetUserIdCtx(c)
-if err != nil {
-return http.StatusBadRequest, err
-}
+ userId, err := utils.GetUserIdCtx(c)
+ if err != nil {
+ return http.StatusBadRequest, err
+ }
-link, err := ctr.app.Services.Ressource.CreateBackup(userId, ctr.app.MinioClient)
-if err != nil {
-return http.StatusInternalServerError, err
-}
-return http.StatusCreated, gin.H{"link": link}
+ link, err := ctr.app.Services.Ressource.CreateBackup(userId, ctr.app.MinioClient)
+ if err != nil {
+ return http.StatusInternalServerError, err
+ }
+ return http.StatusCreated, gin.H{"link": link}
}
func (ctr *Controller) UploadFile(c *gin.Context) (int, any) {
-file, header, err := c.Request.FormFile("file")
-if err != nil {
-return http.StatusBadRequest, errors.New("failed to get file")
-}
-defer file.Close()
-
-fileContent, err := io.ReadAll(file)
-if err != nil {
-return http.StatusInternalServerError, errors.New("failed to read file")
-}
-
-userId, err := utils.GetUserIdCtx(c)
-if err != nil {
-return http.StatusBadRequest, err
-}
-
-mimeType := header.Header.Get("Content-Type")
-node, err := ctr.app.Services.Ressource.UploadFile(
-header.Filename,
-header.Size,
-fileContent,
-mimeType,
-userId,
-ctr.app.Config.Cdn.MaxSize,
-ctr.app.Config.Cdn.MaxUploadsSize,
-ctr.app.Config.Cdn.SupportedTypes,
-ctr.app.MinioClient,
-)
-if err != nil {
-return http.StatusBadRequest, err
-}
-
-return http.StatusOK, node
+ file, header, err := c.Request.FormFile("file")
+ if err != nil {
+ return http.StatusBadRequest, errors.New("failed to get file")
+ }
+ defer file.Close()
+
+ fileContent, err := io.ReadAll(file)
+ if err != nil {
+ return http.StatusInternalServerError, errors.New("failed to read file")
+ }
+
+ userId, err := utils.GetUserIdCtx(c)
+ if err != nil {
+ return http.StatusBadRequest, err
+ }
+
+ mimeType := header.Header.Get("Content-Type")
+ node, err := ctr.app.Services.Ressource.UploadFile(
+ header.Filename,
+ header.Size,
+ fileContent,
+ mimeType,
+ userId,
+ ctr.app.Config.Cdn.MaxSize,
+ ctr.app.Config.Cdn.MaxUploadsSize,
+ ctr.app.Config.Cdn.SupportedTypes,
+ ctr.app.MinioClient,
+ )
+ if err != nil {
+ return http.StatusBadRequest, err
+ }
+
+ return http.StatusOK, node
}
func (ctr *Controller) UploadAvatar(c *gin.Context) (int, any) {
-file, header, err := c.Request.FormFile("file")
-if err != nil {
-return http.StatusBadRequest, errors.New("failed to get file")
-}
-defer file.Close()
-
-fileContent, err := io.ReadAll(file)
-if err != nil {
-return http.StatusInternalServerError, errors.New("failed to read file")
-}
-
-userId, err := utils.GetUserIdCtx(c)
-if err != nil {
-return http.StatusBadRequest, err
-}
-
-mimeType := header.Header.Get("Content-Type")
-err = ctr.app.Services.Ressource.UploadAvatar(
-header.Filename,
-header.Size,
-fileContent,
-mimeType,
-userId,
-ctr.app.Config.Cdn.MaxSize,
-ctr.app.Config.Cdn.SupportedTypesImages,
-ctr.app.MinioClient,
-)
-if err != nil {
-return http.StatusBadRequest, err
-}
-
-return http.StatusOK, "Avatar uploaded successfully"
-}
-
-func (ctr *Controller) DeleteUpload(c *gin.Context) (int, any) {
-connectedUserId, connectedUserRole, err := utils.GetUserContext(c)
-if err != nil {
-return http.StatusUnauthorized, err
-}
-nodeTargetId, err := utils.GetTargetId(c, c.Param("id"))
-if err != nil {
-return http.StatusBadRequest, errors.New("invalid id format")
-}
-
-err = ctr.app.Services.Ressource.DeleteUpload(nodeTargetId, connectedUserId, connectedUserRole, ctr.authorizer, ctr.app.MinioClient)
-if err != nil {
-return http.StatusUnauthorized, err
-}
-return http.StatusOK, "Ressource deleted successfully"
+ file, header, err := c.Request.FormFile("file")
+ if err != nil {
+ return http.StatusBadRequest, errors.New("failed to get file")
+ }
+ defer file.Close()
+
+ fileContent, err := io.ReadAll(file)
+ if err != nil {
+ return http.StatusInternalServerError, errors.New("failed to read file")
+ }
+
+ userId, err := utils.GetUserIdCtx(c)
+ if err != nil {
+ return http.StatusBadRequest, err
+ }
+
+ mimeType := header.Header.Get("Content-Type")
+ err = ctr.app.Services.Ressource.UploadAvatar(
+ header.Filename,
+ header.Size,
+ fileContent,
+ mimeType,
+ userId,
+ ctr.app.Config.Cdn.MaxSize,
+ ctr.app.Config.Cdn.SupportedTypesImages,
+ ctr.app.MinioClient,
+ )
+ if err != nil {
+ return http.StatusBadRequest, err
+ }
+
+ return http.StatusOK, "Avatar uploaded successfully"
}
diff --git a/backend/router/routes/ressources.go b/backend/router/routes/ressources.go
index c6ab3b80..7ca8163e 100644
--- a/backend/router/routes/ressources.go
+++ b/backend/router/routes/ressources.go
@@ -17,5 +17,4 @@ func Uploads(app *app.App, router *gin.RouterGroup) {
ressources.GET(("/backup"), utils.WP(ressourcesCtrl.GetBackup))
ressources.POST("", utils.WP(ressourcesCtrl.UploadFile))
ressources.POST("/avatar", utils.WP(ressourcesCtrl.UploadAvatar))
- ressources.DELETE("/:id", utils.WP(ressourcesCtrl.DeleteUpload))
}
diff --git a/backend/services/ressource.service.go b/backend/services/ressource.service.go
index d355a606..7ab74aae 100644
--- a/backend/services/ressource.service.go
+++ b/backend/services/ressource.service.go
@@ -2,7 +2,6 @@ package services
import (
"alexandrie/models"
- "alexandrie/permissions"
"alexandrie/repositories"
"alexandrie/types"
"alexandrie/utils"
@@ -23,7 +22,6 @@ type RessourceService interface {
CreateBackup(userId types.Snowflake, minioClient *minio.Client) (string, error)
UploadFile(filename string, fileSize int64, fileContent []byte, mimeType string, userId types.Snowflake, maxSize, maxUploadsSize float64, supportedTypes []string, minioClient *minio.Client) (*models.Node, error)
UploadAvatar(filename string, fileSize int64, fileContent []byte, mimeType string, userId types.Snowflake, maxSize float64, supportedTypes []string, minioClient *minio.Client) error
- DeleteUpload(nodeId types.Snowflake, connectedUserId types.Snowflake, connectedUserRole permissions.UserRole, authorizer permissions.Authorizer, minioClient *minio.Client) error
}
type ressourceService struct {
@@ -147,40 +145,3 @@ func (s *ressourceService) UploadAvatar(filename string, fileSize int64, fileCon
_, err := minioClient.PutObject(context.Background(), os.Getenv("MINIO_BUCKET"), objectName, bytes.NewReader(fileContent), fileSize, minio.PutObjectOptions{ContentType: mimeType})
return err
}
-
-func (s *ressourceService) DeleteUpload(nodeId types.Snowflake, connectedUserId types.Snowflake, connectedUserRole permissions.UserRole, authorizer permissions.Authorizer, minioClient *minio.Client) error {
- if minioClient == nil {
- return errors.New("minio client not initialized")
- }
-
- node, err := s.nodeRepo.GetByID(nodeId)
- if err != nil {
- return err
- }
- if node == nil {
- return errors.New("node not found")
- }
-
- if allowed, err := authorizer.CanAccessUser(connectedUserId, node.UserId, connectedUserRole); !allowed || err != nil {
- return errors.New("unauthorized")
- }
-
- prefix := fmt.Sprintf("%d/%d", node.UserId, node.Id)
- ctx := context.Background()
- objectCh := minioClient.ListObjects(ctx, os.Getenv("MINIO_BUCKET"), minio.ListObjectsOptions{
- Prefix: prefix,
- Recursive: true,
- })
-
- for object := range objectCh {
- if object.Err != nil {
- return object.Err
- }
- err = minioClient.RemoveObject(ctx, os.Getenv("MINIO_BUCKET"), object.Key, minio.RemoveObjectOptions{})
- if err != nil {
- return fmt.Errorf("failed to delete object: %v", err)
- }
- }
-
- return s.nodeRepo.Delete(nodeId)
-}