mirror of
https://github.com/halejohn/Cloudreve.git
synced 2026-01-26 09:34:57 +08:00
feat(wopi): fetch discover endpoint
This commit is contained in:
77
pkg/wopi/discovery.go
Normal file
77
pkg/wopi/discovery.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package wopi
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
"github.com/cloudreve/Cloudreve/v3/pkg/cache"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type ActonType string
|
||||
|
||||
var (
|
||||
ActionPreview = ActonType("embedview")
|
||||
ActionEdit = ActonType("edit")
|
||||
)
|
||||
|
||||
const (
|
||||
DiscoverResponseCacheKey = "wopi_discover"
|
||||
DiscoverRefreshDuration = 24 * 3600 // 24 hrs
|
||||
)
|
||||
|
||||
// checkDiscovery checks if discovery content is needed to be refreshed.
|
||||
// If so, it will refresh discovery content.
|
||||
func (c *client) checkDiscovery() error {
|
||||
c.mu.RLock()
|
||||
if c.discovery == nil {
|
||||
c.mu.RUnlock()
|
||||
return c.refreshDiscovery()
|
||||
}
|
||||
|
||||
c.mu.RUnlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
// refresh Discovery action configs.
|
||||
func (c *client) refreshDiscovery() error {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
|
||||
cached, exist := cache.Get(DiscoverResponseCacheKey)
|
||||
if exist {
|
||||
cachedDiscovery := cached.(WopiDiscovery)
|
||||
c.discovery = &cachedDiscovery
|
||||
} else {
|
||||
res, err := c.http.Request("GET", c.config.discoveryEndpoint.String(), nil).
|
||||
CheckHTTPResponse(http.StatusOK).GetResponse()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to request discovery endpoint: %s", err)
|
||||
}
|
||||
|
||||
if err := xml.Unmarshal([]byte(res), &c.discovery); err != nil {
|
||||
return fmt.Errorf("failed to parse response discovery endpoint: %s", err)
|
||||
}
|
||||
|
||||
if err := cache.Set(DiscoverResponseCacheKey, *c.discovery, DiscoverRefreshDuration); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// construct actions map
|
||||
c.actions = make(map[string]map[string]Action)
|
||||
for _, app := range c.discovery.NetZone.App {
|
||||
for _, action := range app.Action {
|
||||
if action.Ext == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
if _, ok := c.actions["."+action.Ext]; !ok {
|
||||
c.actions["."+action.Ext] = make(map[string]Action)
|
||||
}
|
||||
|
||||
c.actions["."+action.Ext][action.Name] = action
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user