Changeset 0.37.18 (#230)
Co-authored-by: gobha <jasafpro@gmail.com> Co-committed-by: gobha <jasafpro@gmail.com>
This commit is contained in:
@@ -85,6 +85,55 @@ func (h *WorkspaceHandler) Create(c *gin.Context) {
|
||||
c.JSON(http.StatusCreated, w)
|
||||
}
|
||||
|
||||
// GetDefault returns the current user's personal workspace, creating it on first access.
|
||||
// The personal workspace is the first user-owned workspace (by created_at) or a new
|
||||
// "My Files" workspace if none exists. Idempotent — safe to call on every page load.
|
||||
func (h *WorkspaceHandler) GetDefault(c *gin.Context) {
|
||||
userID := getUserID(c)
|
||||
ctx := c.Request.Context()
|
||||
|
||||
w, err := h.stores.Workspaces.GetByOwner(ctx, models.WorkspaceOwnerUser, userID)
|
||||
if err == nil {
|
||||
// Enrich with stats
|
||||
stats, _ := h.stores.Workspaces.GetStats(ctx, w.ID)
|
||||
if stats != nil {
|
||||
w.FileCount = stats.FileCount
|
||||
w.TotalBytes = stats.TotalBytes
|
||||
}
|
||||
c.JSON(http.StatusOK, w)
|
||||
return
|
||||
}
|
||||
if err != sql.ErrNoRows {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to fetch workspace"})
|
||||
log.Printf("workspace getDefault: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Auto-create personal workspace
|
||||
w = &models.Workspace{
|
||||
OwnerType: models.WorkspaceOwnerUser,
|
||||
OwnerID: userID,
|
||||
Name: "My Files",
|
||||
Status: models.WorkspaceStatusActive,
|
||||
}
|
||||
w.ID = store.NewID()
|
||||
w.RootPath = "workspaces/" + w.ID
|
||||
|
||||
if err := h.stores.Workspaces.Create(ctx, w); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to create default workspace"})
|
||||
log.Printf("workspace getDefault create: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.wfs.CreateDir(w); err != nil {
|
||||
log.Printf("workspace getDefault mkdir: %v", err)
|
||||
// Workspace record exists but directory failed — still return the workspace.
|
||||
// Directory will be created on first file upload.
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, w)
|
||||
}
|
||||
|
||||
func (h *WorkspaceHandler) Get(c *gin.Context) {
|
||||
w, ok := h.loadAndAuthorize(c)
|
||||
if !ok {
|
||||
|
||||
Reference in New Issue
Block a user