2025-12-02 22:08:50 +00:00
|
|
|
package catalog
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"github.com/get-drexa/drexa/internal/virtualfs"
|
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
|
"github.com/uptrace/bun"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type HTTPHandler struct {
|
|
|
|
|
vfs *virtualfs.VirtualFS
|
|
|
|
|
db *bun.DB
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-13 22:44:37 +00:00
|
|
|
// patchFileRequest represents a file update request
|
|
|
|
|
// @Description Request to update file properties
|
2025-12-02 22:08:50 +00:00
|
|
|
type patchFileRequest struct {
|
2025-12-13 22:44:37 +00:00
|
|
|
// New name for the file
|
|
|
|
|
Name string `json:"name" example:"renamed-document.pdf"`
|
2025-12-02 22:08:50 +00:00
|
|
|
}
|
|
|
|
|
|
2025-12-13 22:44:37 +00:00
|
|
|
// patchDirectoryRequest represents a directory update request
|
|
|
|
|
// @Description Request to update directory properties
|
2025-12-03 00:56:44 +00:00
|
|
|
type patchDirectoryRequest struct {
|
2025-12-13 22:44:37 +00:00
|
|
|
// New name for the directory
|
|
|
|
|
Name string `json:"name" example:"My Documents"`
|
2025-12-03 00:56:44 +00:00
|
|
|
}
|
|
|
|
|
|
2025-12-02 22:08:50 +00:00
|
|
|
func NewHTTPHandler(vfs *virtualfs.VirtualFS, db *bun.DB) *HTTPHandler {
|
|
|
|
|
return &HTTPHandler{vfs: vfs, db: db}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *HTTPHandler) RegisterRoutes(api fiber.Router) {
|
2025-12-15 00:13:10 +00:00
|
|
|
api.Delete("/files", h.deleteFiles)
|
|
|
|
|
|
2025-12-03 00:56:44 +00:00
|
|
|
fg := api.Group("/files/:fileID")
|
|
|
|
|
fg.Use(h.currentFileMiddleware)
|
|
|
|
|
fg.Get("/", h.fetchFile)
|
|
|
|
|
fg.Get("/content", h.downloadFile)
|
|
|
|
|
fg.Patch("/", h.patchFile)
|
|
|
|
|
fg.Delete("/", h.deleteFile)
|
|
|
|
|
|
|
|
|
|
api.Post("/directories", h.createDirectory)
|
2025-12-15 00:13:10 +00:00
|
|
|
api.Delete("/directories", h.deleteDirectories)
|
2025-12-03 00:56:44 +00:00
|
|
|
|
|
|
|
|
dg := api.Group("/directories/:directoryID")
|
|
|
|
|
dg.Use(h.currentDirectoryMiddleware)
|
|
|
|
|
dg.Get("/", h.fetchDirectory)
|
2025-12-13 19:24:54 +00:00
|
|
|
dg.Post("/content", h.moveItemsToDirectory)
|
2025-12-03 00:56:44 +00:00
|
|
|
dg.Get("/content", h.listDirectory)
|
|
|
|
|
dg.Patch("/", h.patchDirectory)
|
|
|
|
|
dg.Delete("/", h.deleteDirectory)
|
2025-12-02 22:08:50 +00:00
|
|
|
}
|
2025-12-15 00:13:10 +00:00
|
|
|
|
|
|
|
|
func fileInfoFromNode(node *virtualfs.Node) FileInfo {
|
|
|
|
|
return FileInfo{
|
|
|
|
|
Kind: DirItemKindFile,
|
|
|
|
|
ID: node.PublicID,
|
|
|
|
|
Name: node.Name,
|
|
|
|
|
Size: node.Size,
|
|
|
|
|
MimeType: node.MimeType,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func directoryInfoFromNode(node *virtualfs.Node) DirectoryInfo {
|
|
|
|
|
return DirectoryInfo{
|
|
|
|
|
Kind: DirItemKindDirectory,
|
|
|
|
|
ID: node.PublicID,
|
|
|
|
|
Name: node.Name,
|
|
|
|
|
CreatedAt: node.CreatedAt,
|
|
|
|
|
UpdatedAt: node.UpdatedAt,
|
|
|
|
|
DeletedAt: node.DeletedAt,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func toDirectoryItem(node *virtualfs.Node) any {
|
|
|
|
|
switch node.Kind {
|
|
|
|
|
default:
|
|
|
|
|
return FileInfo{}
|
|
|
|
|
case virtualfs.NodeKindDirectory:
|
|
|
|
|
return directoryInfoFromNode(node)
|
|
|
|
|
case virtualfs.NodeKindFile:
|
|
|
|
|
return fileInfoFromNode(node)
|
|
|
|
|
}
|
|
|
|
|
}
|