Fix WOPI: Use correct WebDAV path for org files (/orgs/{orgId}/ prefix)
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
@@ -345,6 +346,7 @@ func wopiGetFileHandler(w http.ResponseWriter, r *http.Request, db *database.DB,
|
|||||||
// Verify user has access to this file
|
// Verify user has access to this file
|
||||||
canAccess := false
|
canAccess := false
|
||||||
var webDAVClient *storage.WebDAVClient
|
var webDAVClient *storage.WebDAVClient
|
||||||
|
var remotePath string
|
||||||
|
|
||||||
if file.UserID != nil && *file.UserID == userID {
|
if file.UserID != nil && *file.UserID == userID {
|
||||||
canAccess = true
|
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)
|
errors.WriteError(w, errors.CodeInternal, "Storage error", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
// User files: path is relative to user's WebDAV root
|
||||||
|
remotePath = file.Path
|
||||||
} else if file.OrgID != nil {
|
} else if file.OrgID != nil {
|
||||||
// Check if user is member of the org
|
// Check if user is member of the org
|
||||||
member, err := db.GetOrgMember(r.Context(), *file.OrgID, userID)
|
member, err := db.GetOrgMember(r.Context(), *file.OrgID, userID)
|
||||||
if err == nil && member != nil {
|
if err == nil && member != nil {
|
||||||
canAccess = true
|
canAccess = true
|
||||||
// Create admin WebDAV client for org files - use config
|
// Use user's WebDAV client for org files too
|
||||||
webDAVClient = storage.NewWebDAVClient(cfg)
|
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
|
// 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 {
|
if err != nil {
|
||||||
fmt.Printf("[WOPI-STORAGE] Failed to download file: file=%s path=%s error=%v\n", fileID, file.Path, err)
|
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)
|
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
|
// Verify user has access to this file
|
||||||
canAccess := false
|
canAccess := false
|
||||||
var webDAVClient *storage.WebDAVClient
|
var webDAVClient *storage.WebDAVClient
|
||||||
|
var remotePath string
|
||||||
|
|
||||||
if file.UserID != nil && *file.UserID == userID {
|
if file.UserID != nil && *file.UserID == userID {
|
||||||
canAccess = true
|
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)
|
errors.WriteError(w, errors.CodeInternal, "Storage error", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
// User files: path is relative to user's WebDAV root
|
||||||
|
remotePath = file.Path
|
||||||
} else if file.OrgID != nil {
|
} else if file.OrgID != nil {
|
||||||
member, err := db.GetOrgMember(r.Context(), *file.OrgID, userID)
|
member, err := db.GetOrgMember(r.Context(), *file.OrgID, userID)
|
||||||
if err == nil && member != nil {
|
if err == nil && member != nil {
|
||||||
canAccess = true
|
canAccess = true
|
||||||
// Create admin WebDAV client for org files - use config
|
// Use user's WebDAV client for org files too
|
||||||
webDAVClient = storage.NewWebDAVClient(cfg)
|
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()
|
defer r.Body.Close()
|
||||||
|
|
||||||
// Upload to storage
|
// 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 {
|
if err != nil {
|
||||||
fmt.Printf("[WOPI-STORAGE] Failed to upload file: file=%s path=%s error=%v\n", fileID, file.Path, err)
|
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)
|
errors.WriteError(w, errors.CodeInternal, "Failed to save file", http.StatusInternalServerError)
|
||||||
|
|||||||
Reference in New Issue
Block a user