feat: link public folders to office viewer
This commit is contained in:
@@ -101,6 +101,7 @@ func main() {
|
|||||||
r.Get("/public/{token}/download", nodeH.PublicDownload)
|
r.Get("/public/{token}/download", nodeH.PublicDownload)
|
||||||
r.Get("/public/{token}/nodes/{id}", nodeH.PublicChildMeta)
|
r.Get("/public/{token}/nodes/{id}", nodeH.PublicChildMeta)
|
||||||
r.Get("/public/{token}/nodes/{id}/download", nodeH.PublicChildDownload)
|
r.Get("/public/{token}/nodes/{id}/download", nodeH.PublicChildDownload)
|
||||||
|
r.Get("/public/{token}/nodes/{id}/office", nodeH.PublicOfficeInfo)
|
||||||
|
|
||||||
srv := &http.Server{
|
srv := &http.Server{
|
||||||
Addr: ":" + cfg.ServerPort,
|
Addr: ":" + cfg.ServerPort,
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"html"
|
"html"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -398,6 +399,12 @@ func (h *NodeHandler) PublicChildMeta(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
token := chi.URLParam(r, "token")
|
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 {
|
if node.NodeType == model.NodeTypeFile {
|
||||||
h.renderPublicPreview(w, node, h.publicNodeURL(token, node.ID)+"/download")
|
h.renderPublicPreview(w, node, h.publicNodeURL(token, node.ID)+"/download")
|
||||||
return
|
return
|
||||||
@@ -414,6 +421,23 @@ func (h *NodeHandler) PublicChildMeta(w http.ResponseWriter, r *http.Request) {
|
|||||||
h.renderPublicUnavailable(w, node)
|
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) {
|
func (h *NodeHandler) PublicChildDownload(w http.ResponseWriter, r *http.Request) {
|
||||||
node, ok := h.publicNodeByID(w, r)
|
node, ok := h.publicNodeByID(w, r)
|
||||||
if !ok {
|
if !ok {
|
||||||
@@ -552,6 +576,11 @@ func (h *NodeHandler) renderPublicFolder(w http.ResponseWriter, r *http.Request,
|
|||||||
}
|
}
|
||||||
kind := publicKind(child)
|
kind := publicKind(child)
|
||||||
href := h.publicNodeURL(token, child.ID)
|
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(`<a class="item" href="` + html.EscapeString(href) + `">`)
|
||||||
b.WriteString(`<span class="badge">` + html.EscapeString(kind) + `</span>`)
|
b.WriteString(`<span class="badge">` + html.EscapeString(kind) + `</span>`)
|
||||||
b.WriteString(`<span class="title">` + html.EscapeString(title) + `</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
|
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 {
|
func publicKind(node model.Node) string {
|
||||||
switch node.NodeType {
|
switch node.NodeType {
|
||||||
case model.NodeTypeFolder:
|
case model.NodeTypeFolder:
|
||||||
|
|||||||
Reference in New Issue
Block a user