feat: link public folders to office viewer
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"html"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -398,6 +399,12 @@ func (h *NodeHandler) PublicChildMeta(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
token := chi.URLParam(r, "token")
|
||||
if node.NodeType == model.NodeTypeOfficeDocument {
|
||||
if officeID := officeIDFromNode(node); officeID != "" {
|
||||
http.Redirect(w, r, h.publicOfficeURL(token, node.ID, officeID), http.StatusFound)
|
||||
return
|
||||
}
|
||||
}
|
||||
if node.NodeType == model.NodeTypeFile {
|
||||
h.renderPublicPreview(w, node, h.publicNodeURL(token, node.ID)+"/download")
|
||||
return
|
||||
@@ -414,6 +421,23 @@ func (h *NodeHandler) PublicChildMeta(w http.ResponseWriter, r *http.Request) {
|
||||
h.renderPublicUnavailable(w, node)
|
||||
}
|
||||
|
||||
func (h *NodeHandler) PublicOfficeInfo(w http.ResponseWriter, r *http.Request) {
|
||||
node, ok := h.publicNodeByID(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
officeID := officeIDFromNode(node)
|
||||
if node.NodeType != model.NodeTypeOfficeDocument || officeID == "" {
|
||||
writeError(w, http.StatusBadRequest, "node is not an office document")
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]string{
|
||||
"node_id": node.ID,
|
||||
"office_id": officeID,
|
||||
"title": node.Title,
|
||||
})
|
||||
}
|
||||
|
||||
func (h *NodeHandler) PublicChildDownload(w http.ResponseWriter, r *http.Request) {
|
||||
node, ok := h.publicNodeByID(w, r)
|
||||
if !ok {
|
||||
@@ -552,6 +576,11 @@ func (h *NodeHandler) renderPublicFolder(w http.ResponseWriter, r *http.Request,
|
||||
}
|
||||
kind := publicKind(child)
|
||||
href := h.publicNodeURL(token, child.ID)
|
||||
if child.NodeType == model.NodeTypeOfficeDocument {
|
||||
if officeID := officeIDFromNode(&child); officeID != "" {
|
||||
href = h.publicOfficeURL(token, child.ID, officeID)
|
||||
}
|
||||
}
|
||||
b.WriteString(`<a class="item" href="` + html.EscapeString(href) + `">`)
|
||||
b.WriteString(`<span class="badge">` + html.EscapeString(kind) + `</span>`)
|
||||
b.WriteString(`<span class="title">` + html.EscapeString(title) + `</span>`)
|
||||
@@ -619,6 +648,23 @@ func (h *NodeHandler) publicNodeURL(token, nodeID string) string {
|
||||
return h.publicURL(token) + "/nodes/" + nodeID
|
||||
}
|
||||
|
||||
func (h *NodeHandler) publicOfficeURL(token, nodeID, officeID string) string {
|
||||
values := url.Values{}
|
||||
values.Set("files_token", token)
|
||||
values.Set("node_id", nodeID)
|
||||
return strings.TrimRight(h.cfg.PublicBaseURL, "/") + "/office/public/" + url.PathEscape(officeID) + "?" + values.Encode()
|
||||
}
|
||||
|
||||
func officeIDFromNode(node *model.Node) string {
|
||||
if node == nil || node.ExternalURL == nil {
|
||||
return ""
|
||||
}
|
||||
if match := strings.TrimPrefix(*node.ExternalURL, "/office/"); match != *node.ExternalURL {
|
||||
return strings.TrimSpace(match)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func publicKind(node model.Node) string {
|
||||
switch node.NodeType {
|
||||
case model.NodeTypeFolder:
|
||||
|
||||
Reference in New Issue
Block a user