You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
78 lines
1.7 KiB
78 lines
1.7 KiB
2 years ago
|
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
|
||
|
}
|