plugins: restructure and add tests for pluginsctl/* calls
This commit is contained in:
parent
cf68e61f40
commit
09b79679cd
5 changed files with 547 additions and 404 deletions
|
@ -1,24 +1,22 @@
|
|||
package webgui
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/rclone/rclone/fs"
|
||||
"github.com/rclone/rclone/fs/config"
|
||||
"github.com/rclone/rclone/fs/rc"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/rclone/rclone/fs"
|
||||
"github.com/rclone/rclone/fs/config"
|
||||
)
|
||||
|
||||
// PackageJSON is the structure of package.json of a plugin
|
||||
type PackageJSON struct {
|
||||
Name string `json:"name"`
|
||||
Version string `json:"version"`
|
||||
|
@ -36,23 +34,33 @@ type PackageJSON struct {
|
|||
Bugs struct {
|
||||
URL string `json:"url"`
|
||||
} `json:"bugs"`
|
||||
|
||||
//RcloneHandlesType []string `json:"rcloneHandlesType"`
|
||||
Rclone RcloneConfig `json:"rclone"`
|
||||
}
|
||||
|
||||
// RcloneConfig represents the rclone specific config
|
||||
type RcloneConfig struct {
|
||||
HandlesType []string `json:"handlesType"`
|
||||
PluginType string `json:"pluginType"`
|
||||
HandlesType []string `json:"handlesType"`
|
||||
PluginType string `json:"pluginType"`
|
||||
RedirectReferrer bool `json:"redirectReferrer"`
|
||||
Test bool `json:"-"`
|
||||
}
|
||||
|
||||
func (r *PackageJSON) isTesting() bool {
|
||||
return r.Rclone.Test
|
||||
}
|
||||
|
||||
var (
|
||||
loadedTestPlugins *Plugins
|
||||
cachePath string
|
||||
PluginsPath string
|
||||
pluginsConfigPath string
|
||||
loadedPlugins *Plugins
|
||||
pluginsProxy = &httputil.ReverseProxy{}
|
||||
//loadedTestPlugins *Plugins
|
||||
cachePath string
|
||||
|
||||
loadedPlugins *Plugins
|
||||
pluginsProxy = &httputil.ReverseProxy{}
|
||||
// PluginsMatch is used for matching author and plugin name in the url path
|
||||
PluginsMatch = regexp.MustCompile(`^plugins\/([^\/]*)\/([^\/\?]+)[\/]?(.*)$`)
|
||||
// PluginsPath is the base path where webgui plugins are stored
|
||||
PluginsPath string
|
||||
pluginsConfigPath string
|
||||
availablePluginsJSONPath = "availablePlugins.json"
|
||||
)
|
||||
|
||||
func init() {
|
||||
|
@ -60,19 +68,14 @@ func init() {
|
|||
PluginsPath = filepath.Join(cachePath, "plugins")
|
||||
pluginsConfigPath = filepath.Join(PluginsPath, "config")
|
||||
|
||||
loadedPlugins = newPlugins("availablePlugins.json")
|
||||
loadedPlugins = newPlugins(availablePluginsJSONPath)
|
||||
err := loadedPlugins.readFromFile()
|
||||
if err != nil {
|
||||
fs.Errorf(nil, "error reading available plugins", err)
|
||||
}
|
||||
loadedTestPlugins = newPlugins("testPlugins.json")
|
||||
err = loadedTestPlugins.readFromFile()
|
||||
|
||||
if err != nil {
|
||||
fs.Errorf(nil, "error reading test plugins", err)
|
||||
fs.Errorf(nil, "error reading available plugins: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Plugins represents the structure how plugins are saved onto disk
|
||||
type Plugins struct {
|
||||
mutex sync.Mutex
|
||||
LoadedPlugins map[string]PackageJSON `json:"loadedPlugins"`
|
||||
|
@ -93,8 +96,8 @@ func (p *Plugins) readFromFile() (err error) {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
availablePluginsJson := filepath.Join(pluginsConfigPath, p.fileName)
|
||||
data, err := ioutil.ReadFile(availablePluginsJson)
|
||||
availablePluginsJSON := filepath.Join(pluginsConfigPath, p.fileName)
|
||||
data, err := ioutil.ReadFile(availablePluginsJSON)
|
||||
if err != nil {
|
||||
// create a file ?
|
||||
}
|
||||
|
@ -105,19 +108,19 @@ func (p *Plugins) readFromFile() (err error) {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (p *Plugins) addPlugin(pluginName string, packageJsonPath string) (err error) {
|
||||
func (p *Plugins) addPlugin(pluginName string, packageJSONPath string) (err error) {
|
||||
p.mutex.Lock()
|
||||
defer p.mutex.Unlock()
|
||||
data, err := ioutil.ReadFile(packageJsonPath)
|
||||
data, err := ioutil.ReadFile(packageJSONPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var pkgJson = PackageJSON{}
|
||||
err = json.Unmarshal(data, &pkgJson)
|
||||
var pkgJSON = PackageJSON{}
|
||||
err = json.Unmarshal(data, &pkgJSON)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
p.LoadedPlugins[pluginName] = pkgJson
|
||||
p.LoadedPlugins[pluginName] = pkgJSON
|
||||
|
||||
err = p.writeToFile()
|
||||
if err != nil {
|
||||
|
@ -135,15 +138,16 @@ func (p *Plugins) addTestPlugin(pluginName string, testURL string, handlesType [
|
|||
return err
|
||||
}
|
||||
|
||||
var pkgJson = PackageJSON{
|
||||
var pkgJSON = PackageJSON{
|
||||
Name: pluginName,
|
||||
TestURL: testURL,
|
||||
Rclone: RcloneConfig{
|
||||
HandlesType: handlesType,
|
||||
Test: true,
|
||||
},
|
||||
}
|
||||
|
||||
p.LoadedPlugins[pluginName] = pkgJson
|
||||
p.LoadedPlugins[pluginName] = pkgJSON
|
||||
|
||||
err = p.writeToFile()
|
||||
if err != nil {
|
||||
|
@ -156,11 +160,11 @@ func (p *Plugins) addTestPlugin(pluginName string, testURL string, handlesType [
|
|||
func (p *Plugins) writeToFile() (err error) {
|
||||
//p.mutex.Lock()
|
||||
//defer p.mutex.Unlock()
|
||||
availablePluginsJson := filepath.Join(pluginsConfigPath, p.fileName)
|
||||
availablePluginsJSON := filepath.Join(pluginsConfigPath, p.fileName)
|
||||
|
||||
file, err := json.MarshalIndent(p, "", " ")
|
||||
|
||||
err = ioutil.WriteFile(availablePluginsJson, file, 0755)
|
||||
err = ioutil.WriteFile(availablePluginsJSON, file, 0755)
|
||||
if err != nil {
|
||||
fs.Logf(nil, "%s", err)
|
||||
}
|
||||
|
@ -177,7 +181,7 @@ func (p *Plugins) removePlugin(name string) (err error) {
|
|||
|
||||
_, ok := p.LoadedPlugins[name]
|
||||
if !ok {
|
||||
return errors.New(fmt.Sprintf("plugin %s not loaded", name))
|
||||
return fmt.Errorf("plugin %s not loaded", name)
|
||||
}
|
||||
delete(p.LoadedPlugins, name)
|
||||
|
||||
|
@ -188,219 +192,18 @@ func (p *Plugins) removePlugin(name string) (err error) {
|
|||
return nil
|
||||
}
|
||||
|
||||
// GetPluginByName returns the plugin object for the key (author/plugin-name)
|
||||
func (p *Plugins) GetPluginByName(name string) (out *PackageJSON, err error) {
|
||||
p.mutex.Lock()
|
||||
defer p.mutex.Unlock()
|
||||
po, ok := p.LoadedPlugins[name]
|
||||
if !ok {
|
||||
return nil, errors.New(fmt.Sprintf("plugin %s not loaded", name))
|
||||
return nil, fmt.Errorf("plugin %s not loaded", name)
|
||||
}
|
||||
return &po, nil
|
||||
|
||||
}
|
||||
|
||||
func init() {
|
||||
rc.Add(rc.Call{
|
||||
Path: "pluginsctl/addTestPlugin",
|
||||
AuthRequired: true,
|
||||
Fn: rcAddTestPlugin,
|
||||
Title: "Show current mount points",
|
||||
Help: `This shows currently mounted points, which can be used for performing an unmount
|
||||
|
||||
This takes no parameters and returns
|
||||
|
||||
- mountPoints: list of current mount points
|
||||
|
||||
Eg
|
||||
|
||||
rclone rc pluginsctl/addTestPlugin
|
||||
`,
|
||||
})
|
||||
}
|
||||
|
||||
func rcAddTestPlugin(_ context.Context, in rc.Params) (out rc.Params, err error) {
|
||||
name, err := in.GetString("name")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
loadUrl, err := in.GetString("loadUrl")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var handlesTypes []string
|
||||
err = in.GetStructMissingOK("handlesTypes", &handlesTypes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = loadedTestPlugins.addTestPlugin(name, loadUrl, handlesTypes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
rc.Add(rc.Call{
|
||||
Path: "pluginsctl/listTestPlugins",
|
||||
AuthRequired: true,
|
||||
Fn: rcGetLoadedPlugins,
|
||||
Title: "Show current mount points",
|
||||
Help: `This shows currently mounted points, which can be used for performing an unmount
|
||||
|
||||
This takes no parameters and returns
|
||||
|
||||
- mountPoints: list of current mount points
|
||||
|
||||
Eg
|
||||
|
||||
rclone rc pluginsctl/listTestPlugins
|
||||
`,
|
||||
})
|
||||
}
|
||||
|
||||
func rcGetLoadedPlugins(_ context.Context, in rc.Params) (out rc.Params, err error) {
|
||||
return rc.Params{
|
||||
"loadedTestPlugins": loadedTestPlugins.LoadedPlugins,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
rc.Add(rc.Call{
|
||||
Path: "pluginsctl/removeTestPlugin",
|
||||
AuthRequired: true,
|
||||
Fn: rcRemoveTestPlugin,
|
||||
Title: "Show current mount points",
|
||||
Help: `This shows currently mounted points, which can be used for performing an unmount
|
||||
|
||||
This takes no parameters and returns
|
||||
|
||||
- mountPoints: list of current mount points
|
||||
|
||||
Eg
|
||||
|
||||
rclone rc pluginsctl/removeTestPlugin
|
||||
`,
|
||||
})
|
||||
}
|
||||
|
||||
func rcRemoveTestPlugin(_ context.Context, in rc.Params) (out rc.Params, err error) {
|
||||
name, err := in.GetString("name")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = loadedTestPlugins.removePlugin(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
rc.Add(rc.Call{
|
||||
Path: "pluginsctl/addPlugin",
|
||||
AuthRequired: true,
|
||||
Fn: rcAddPlugin,
|
||||
Title: "Show current mount points",
|
||||
Help: `This shows currently mounted points, which can be used for performing an unmount
|
||||
|
||||
This takes no parameters and returns
|
||||
|
||||
- mountPoints: list of current mount points
|
||||
|
||||
Eg
|
||||
|
||||
rclone rc pluginsctl/addPlugin
|
||||
`,
|
||||
})
|
||||
}
|
||||
|
||||
func rcAddPlugin(_ context.Context, in rc.Params) (out rc.Params, err error) {
|
||||
pluginUrl, err := in.GetString("url")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
author, repoName, repoBranch, err := getAuthorRepoBranchGithub(pluginUrl)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
branch, err := in.GetString("branch")
|
||||
if err != nil || branch == "" {
|
||||
branch = repoBranch
|
||||
}
|
||||
|
||||
version, err := in.GetString("version")
|
||||
if err != nil || version == "" {
|
||||
version = "latest"
|
||||
}
|
||||
|
||||
err = CreatePathIfNotExist(PluginsPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// fetch and package.json
|
||||
// https://raw.githubusercontent.com/rclone/rclone-webui-react/master/package.json
|
||||
|
||||
pluginID := fmt.Sprintf("%s/%s", author, repoName)
|
||||
|
||||
currentPluginPath := filepath.Join(PluginsPath, pluginID)
|
||||
|
||||
err = CreatePathIfNotExist(currentPluginPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
packageJsonUrl := fmt.Sprintf("https://raw.githubusercontent.com/%s/%s/%s/package.json", author, repoName, branch)
|
||||
packageJsonFilePath := filepath.Join(currentPluginPath, "package.json")
|
||||
err = DownloadFile(packageJsonFilePath, packageJsonUrl)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// register in plugins
|
||||
|
||||
// download release and save in plugins/<author>/repo-name/app
|
||||
// https://api.github.com/repos/rclone/rclone-webui-react/releases/latest
|
||||
releaseUrl, tag, _, err := GetLatestReleaseURL(fmt.Sprintf("https://api.github.com/repos/%s/%s/releases/%s", author, repoName, version))
|
||||
zipName := tag + ".zip"
|
||||
zipPath := filepath.Join(currentPluginPath, zipName)
|
||||
|
||||
err = DownloadFile(zipPath, releaseUrl)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
extractPath := filepath.Join(currentPluginPath, "app")
|
||||
|
||||
err = CreatePathIfNotExist(extractPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = os.RemoveAll(extractPath)
|
||||
if err != nil {
|
||||
fs.Logf(nil, "No previous downloads to remove")
|
||||
}
|
||||
|
||||
fs.Logf(nil, "Unzipping plugin binary")
|
||||
|
||||
err = Unzip(zipPath, extractPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = loadedPlugins.addPlugin(pluginID, packageJsonFilePath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
|
||||
}
|
||||
|
||||
// getAuthorRepoBranchGithub gives author, repoName and branch from a github.com url
|
||||
// url examples:
|
||||
// https://github.com/rclone/rclone-webui-react/
|
||||
|
@ -409,14 +212,14 @@ func rcAddPlugin(_ context.Context, in rc.Params) (out rc.Params, err error) {
|
|||
// github.com/rclone/rclone-webui-react
|
||||
//
|
||||
func getAuthorRepoBranchGithub(url string) (author string, repoName string, branch string, err error) {
|
||||
repoUrl := url
|
||||
repoUrl = strings.Replace(repoUrl, "https://", "", 1)
|
||||
repoUrl = strings.Replace(repoUrl, "http://", "", 1)
|
||||
repoURL := url
|
||||
repoURL = strings.Replace(repoURL, "https://", "", 1)
|
||||
repoURL = strings.Replace(repoURL, "http://", "", 1)
|
||||
|
||||
urlSplits := strings.Split(repoUrl, "/")
|
||||
urlSplits := strings.Split(repoURL, "/")
|
||||
|
||||
if len(urlSplits) < 3 || len(urlSplits) > 5 || urlSplits[0] != "github.com" {
|
||||
return "", "", "", errors.New(fmt.Sprintf("Invalid github url: %s", url))
|
||||
return "", "", "", fmt.Errorf("invalid github url: %s", url)
|
||||
}
|
||||
|
||||
// get branch name
|
||||
|
@ -427,94 +230,6 @@ func getAuthorRepoBranchGithub(url string) (author string, repoName string, bran
|
|||
return urlSplits[1], urlSplits[2], "master", nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
rc.Add(rc.Call{
|
||||
Path: "pluginsctl/listPlugins",
|
||||
AuthRequired: true,
|
||||
Fn: rcGetPlugins,
|
||||
Title: "Get the list of currently loaded plugins",
|
||||
Help: `This allows you to get the currently enabled plugins and their details.
|
||||
|
||||
This takes no parameters and returns
|
||||
|
||||
- loadedPlugins: list of current production plugins
|
||||
- testPlugins: list of temporarily loaded development plugins, usually running on a different server.
|
||||
|
||||
Eg
|
||||
|
||||
rclone rc pluginsctl/listPlugins
|
||||
`,
|
||||
})
|
||||
}
|
||||
|
||||
func rcGetPlugins(_ context.Context, in rc.Params) (out rc.Params, err error) {
|
||||
err = loadedPlugins.readFromFile()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = loadedTestPlugins.readFromFile()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return rc.Params{
|
||||
"loadedPlugins": loadedPlugins.LoadedPlugins,
|
||||
"loadedTestPlugins": loadedTestPlugins.LoadedPlugins,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
rc.Add(rc.Call{
|
||||
Path: "pluginsctl/removePlugin",
|
||||
AuthRequired: true,
|
||||
Fn: rcRemovePlugin,
|
||||
Title: "Get the list of currently loaded plugins",
|
||||
Help: `This allows you to get the currently enabled plugins and their details.
|
||||
|
||||
This takes parameters
|
||||
|
||||
- name: name of the plugin in the format <author>/<plugin_name>
|
||||
|
||||
Eg
|
||||
|
||||
rclone rc pluginsctl/removePlugin name=rclone/video-plugin
|
||||
`,
|
||||
})
|
||||
}
|
||||
|
||||
func rcRemovePlugin(_ context.Context, in rc.Params) (out rc.Params, err error) {
|
||||
name, err := in.GetString("name")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = loadedPlugins.removePlugin(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
rc.Add(rc.Call{
|
||||
Path: "pluginsctl/getPluginsForType",
|
||||
AuthRequired: true,
|
||||
Fn: rcGetPluginsForType,
|
||||
Title: "Get the list of currently loaded plugins",
|
||||
Help: `This allows you to get the currently enabled plugins and their details.
|
||||
|
||||
This takes no parameters and returns
|
||||
|
||||
- loadedPlugins: list of current production plugins
|
||||
- testPlugins: list of temporarily loaded development plugins, usually running on a different server.
|
||||
|
||||
Eg
|
||||
|
||||
rclone rc pluginsctl/getPlugins
|
||||
`,
|
||||
})
|
||||
}
|
||||
|
||||
func filterPlugins(plugins *Plugins, compare func(packageJSON *PackageJSON) bool) map[string]PackageJSON {
|
||||
output := map[string]PackageJSON{}
|
||||
|
||||
|
@ -527,58 +242,7 @@ func filterPlugins(plugins *Plugins, compare func(packageJSON *PackageJSON) bool
|
|||
return output
|
||||
}
|
||||
|
||||
func rcGetPluginsForType(_ context.Context, in rc.Params) (out rc.Params, err error) {
|
||||
handlesType, err := in.GetString("type")
|
||||
if err != nil {
|
||||
handlesType = ""
|
||||
}
|
||||
|
||||
pluginType, err := in.GetString("pluginType")
|
||||
if err != nil {
|
||||
pluginType = ""
|
||||
}
|
||||
var loadedPluginsResult map[string]PackageJSON
|
||||
|
||||
var loadedTestPluginsResult map[string]PackageJSON
|
||||
|
||||
if pluginType == "" || pluginType == "FileHandler" {
|
||||
|
||||
loadedPluginsResult = filterPlugins(loadedPlugins, func(packageJSON *PackageJSON) bool {
|
||||
for i := range packageJSON.Rclone.HandlesType {
|
||||
if packageJSON.Rclone.HandlesType[i] == handlesType {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
})
|
||||
|
||||
loadedTestPluginsResult = filterPlugins(loadedTestPlugins, func(packageJSON *PackageJSON) bool {
|
||||
for i := range packageJSON.Rclone.HandlesType {
|
||||
if packageJSON.Rclone.HandlesType[i] == handlesType {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
})
|
||||
} else {
|
||||
loadedPluginsResult = filterPlugins(loadedPlugins, func(packageJSON *PackageJSON) bool {
|
||||
return packageJSON.Rclone.PluginType == pluginType
|
||||
})
|
||||
|
||||
loadedTestPluginsResult = filterPlugins(loadedTestPlugins, func(packageJSON *PackageJSON) bool {
|
||||
return packageJSON.Rclone.PluginType == pluginType
|
||||
})
|
||||
}
|
||||
|
||||
return rc.Params{
|
||||
"loadedPlugins": loadedPluginsResult,
|
||||
"testPlugins": loadedTestPluginsResult,
|
||||
}, nil
|
||||
|
||||
}
|
||||
|
||||
var PluginsMatch = regexp.MustCompile(`^plugins\/([^\/]*)\/([^\/\?]+)[\/]?(.*)$`)
|
||||
|
||||
// getDirectorForProxy is a helper function for reverse proxy of test plugins
|
||||
func getDirectorForProxy(origin *url.URL) func(req *http.Request) {
|
||||
return func(req *http.Request) {
|
||||
req.Header.Add("X-Forwarded-Host", req.Host)
|
||||
|
@ -589,12 +253,15 @@ func getDirectorForProxy(origin *url.URL) func(req *http.Request) {
|
|||
}
|
||||
}
|
||||
|
||||
// ServePluginOK checks the plugin url and uses reverse proxy to allow redirection for content not being served by rclone
|
||||
func ServePluginOK(w http.ResponseWriter, r *http.Request, pluginsMatchResult []string) (ok bool) {
|
||||
testPlugin, err := loadedTestPlugins.GetPluginByName(fmt.Sprintf("%s/%s", pluginsMatchResult[1], pluginsMatchResult[2]))
|
||||
testPlugin, err := loadedPlugins.GetPluginByName(fmt.Sprintf("%s/%s", pluginsMatchResult[1], pluginsMatchResult[2]))
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
if !testPlugin.Rclone.Test {
|
||||
return false
|
||||
}
|
||||
origin, _ := url.Parse(fmt.Sprintf("%s/%s", testPlugin.TestURL, pluginsMatchResult[3]))
|
||||
|
||||
director := getDirectorForProxy(origin)
|
||||
|
@ -603,3 +270,30 @@ func ServePluginOK(w http.ResponseWriter, r *http.Request, pluginsMatchResult []
|
|||
pluginsProxy.ServeHTTP(w, r)
|
||||
return true
|
||||
}
|
||||
|
||||
var referrerPathReg = regexp.MustCompile("^(https?)://(.+):([0-9]+)?/(.*)$")
|
||||
|
||||
// ServePluginWithReferrerOK check if redirectReferrer is set for the referred a plugin, if yes,
|
||||
// sends a redirect to actual url. This function is useful for plugins to refer to absolute paths when
|
||||
// the referrer in http.Request is set
|
||||
func ServePluginWithReferrerOK(w http.ResponseWriter, r *http.Request, path string) (ok bool) {
|
||||
referrer := r.Referer()
|
||||
referrerPathMatch := referrerPathReg.FindStringSubmatch(referrer)
|
||||
|
||||
if referrerPathMatch != nil {
|
||||
referrerPluginMatch := PluginsMatch.FindStringSubmatch(referrerPathMatch[4])
|
||||
pluginKey := fmt.Sprintf("%s/%s", referrerPluginMatch[1], referrerPluginMatch[2])
|
||||
currentPlugin, err := loadedPlugins.GetPluginByName(pluginKey)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
if referrerPluginMatch != nil && currentPlugin.Rclone.RedirectReferrer {
|
||||
path = fmt.Sprintf("/plugins/%s/%s/%s", referrerPluginMatch[1], referrerPluginMatch[2], path)
|
||||
|
||||
http.Redirect(w, r, path, http.StatusMovedPermanently)
|
||||
//s.pluginsHandler.ServeHTTP(w, r)
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue