diff --git a/go_cloud/internal/http/wopi_handlers.go b/go_cloud/internal/http/wopi_handlers.go index dfaa221..6829f83 100644 --- a/go_cloud/internal/http/wopi_handlers.go +++ b/go_cloud/internal/http/wopi_handlers.go @@ -7,6 +7,7 @@ import ( "io" "net/http" "net/url" + "path" "strings" "sync" "time" @@ -345,6 +346,7 @@ func wopiGetFileHandler(w http.ResponseWriter, r *http.Request, db *database.DB, // Verify user has access to this file canAccess := false var webDAVClient *storage.WebDAVClient + var remotePath string if file.UserID != nil && *file.UserID == userID { canAccess = true @@ -355,13 +357,23 @@ func wopiGetFileHandler(w http.ResponseWriter, r *http.Request, db *database.DB, errors.WriteError(w, errors.CodeInternal, "Storage error", http.StatusInternalServerError) return } + // User files: path is relative to user's WebDAV root + remotePath = file.Path } else if file.OrgID != nil { // Check if user is member of the org member, err := db.GetOrgMember(r.Context(), *file.OrgID, userID) if err == nil && member != nil { canAccess = true - // Create admin WebDAV client for org files - use config - webDAVClient = storage.NewWebDAVClient(cfg) + // Use user's WebDAV client for org files too + webDAVClient, err = getUserWebDAVClient(r.Context(), db, userID, cfg.NextcloudURL, cfg.NextcloudUser, cfg.NextcloudPass) + if err != nil { + fmt.Printf("[WOPI-STORAGE] Failed to get user WebDAV client: %v\n", err) + errors.WriteError(w, errors.CodeInternal, "Storage error", http.StatusInternalServerError) + return + } + // Org files: stored under /orgs/{orgID}/ prefix + rel := strings.TrimPrefix(file.Path, "/") + remotePath = path.Join("/orgs", file.OrgID.String(), rel) } } @@ -372,7 +384,8 @@ func wopiGetFileHandler(w http.ResponseWriter, r *http.Request, db *database.DB, } // Download file from storage - resp, err := webDAVClient.Download(r.Context(), file.Path, "") + fmt.Printf("[WOPI-STORAGE] GetFile downloading: file=%s remotePath=%s\n", fileID, remotePath) + resp, err := webDAVClient.Download(r.Context(), remotePath, "") if err != nil { fmt.Printf("[WOPI-STORAGE] Failed to download file: file=%s path=%s error=%v\n", fileID, file.Path, err) errors.WriteError(w, errors.CodeNotFound, "File not found in storage", http.StatusNotFound) @@ -436,6 +449,7 @@ func wopiPutFileHandler(w http.ResponseWriter, r *http.Request, db *database.DB, // Verify user has access to this file canAccess := false var webDAVClient *storage.WebDAVClient + var remotePath string if file.UserID != nil && *file.UserID == userID { canAccess = true @@ -445,12 +459,22 @@ func wopiPutFileHandler(w http.ResponseWriter, r *http.Request, db *database.DB, errors.WriteError(w, errors.CodeInternal, "Storage error", http.StatusInternalServerError) return } + // User files: path is relative to user's WebDAV root + remotePath = file.Path } else if file.OrgID != nil { member, err := db.GetOrgMember(r.Context(), *file.OrgID, userID) if err == nil && member != nil { canAccess = true - // Create admin WebDAV client for org files - use config - webDAVClient = storage.NewWebDAVClient(cfg) + // Use user's WebDAV client for org files too + webDAVClient, err = getUserWebDAVClient(r.Context(), db, userID, cfg.NextcloudURL, cfg.NextcloudUser, cfg.NextcloudPass) + if err != nil { + fmt.Printf("[WOPI-STORAGE] Failed to get user WebDAV client: %v\n", err) + errors.WriteError(w, errors.CodeInternal, "Storage error", http.StatusInternalServerError) + return + } + // Org files: stored under /orgs/{orgID}/ prefix + rel := strings.TrimPrefix(file.Path, "/") + remotePath = path.Join("/orgs", file.OrgID.String(), rel) } } @@ -478,7 +502,8 @@ func wopiPutFileHandler(w http.ResponseWriter, r *http.Request, db *database.DB, defer r.Body.Close() // Upload to storage - err = webDAVClient.Upload(r.Context(), file.Path, strings.NewReader(string(content)), int64(len(content))) + fmt.Printf("[WOPI-STORAGE] PutFile uploading: file=%s remotePath=%s\n", fileID, remotePath) + err = webDAVClient.Upload(r.Context(), remotePath, strings.NewReader(string(content)), int64(len(content))) if err != nil { fmt.Printf("[WOPI-STORAGE] Failed to upload file: file=%s path=%s error=%v\n", fileID, file.Path, err) errors.WriteError(w, errors.CodeInternal, "Failed to save file", http.StatusInternalServerError)