mirror of
				https://github.com/filebrowser/filebrowser.git
				synced 2025-11-04 03:12:57 +00:00 
			
		
		
		
	Better Plugin Parsing
Former-commit-id: 8f0d86770072714575223249ab59cb0bcb11d1af [formerly 745c8318de08dc06bd9eece0d185528e4ed0872c] [formerly 61cda90693313822ad2e106dbc5a64104f2b5716 [formerly 722ca4b47f6ba5a4c79e3c3573be8c8b8686063c]] Former-commit-id: c2dad3e81d44f3a92c61d3c21eb6d2113b333ed4 [formerly 898013a275bf0e2b86395f3fa8d5479b08c692d8] Former-commit-id: 6e7c0f06a65cecdb2307df35db361f24d2353e00
This commit is contained in:
		
							parent
							
								
									5968111f3e
								
							
						
					
					
						commit
						3c9762ee97
					
				@ -11,7 +11,7 @@
 | 
			
		||||
      <template v-for="plugin in plugins">
 | 
			
		||||
        <h2>{{ capitalize(plugin.name) }}</h2>
 | 
			
		||||
 | 
			
		||||
        <p v-for="field in plugin.fields" :key="field.name">
 | 
			
		||||
        <p v-for="field in plugin.fields" :key="field.variable">
 | 
			
		||||
          <label v-if="field.type !== 'checkbox'">{{ field.name }}</label>
 | 
			
		||||
          <input v-if="field.type === 'text'" type="text" v-model.trim="field.value">
 | 
			
		||||
          <input v-else-if="field.type === 'checkbox'" type="checkbox" v-model.trim="field.value">
 | 
			
		||||
@ -69,47 +69,8 @@ export default {
 | 
			
		||||
 | 
			
		||||
    api.getPlugins()
 | 
			
		||||
      .then(plugins => {
 | 
			
		||||
        console.log(plugins)
 | 
			
		||||
        let plugin = {}
 | 
			
		||||
 | 
			
		||||
        for (let key in plugins) {
 | 
			
		||||
          plugin.name = key
 | 
			
		||||
          plugin.fields = []
 | 
			
		||||
 | 
			
		||||
          for (let field in plugins[key]) {
 | 
			
		||||
            let value = plugins[key][field]
 | 
			
		||||
 | 
			
		||||
            if (Array.isArray(value)) {
 | 
			
		||||
              plugin.fields.push({
 | 
			
		||||
                name: field,
 | 
			
		||||
                type: 'text',
 | 
			
		||||
                original: 'array',
 | 
			
		||||
                value: value.join(' ')
 | 
			
		||||
              })
 | 
			
		||||
 | 
			
		||||
              continue
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            switch (typeof value) {
 | 
			
		||||
              case 'boolean':
 | 
			
		||||
                plugin.fields.push({
 | 
			
		||||
                  name: field,
 | 
			
		||||
                  type: 'checkbox',
 | 
			
		||||
                  original: 'boolean',
 | 
			
		||||
                  value: value
 | 
			
		||||
                })
 | 
			
		||||
                break
 | 
			
		||||
              default:
 | 
			
		||||
                plugin.fields.push({
 | 
			
		||||
                  name: field,
 | 
			
		||||
                  type: 'text',
 | 
			
		||||
                  original: 'text',
 | 
			
		||||
                  value: value
 | 
			
		||||
                })
 | 
			
		||||
            }
 | 
			
		||||
          }
 | 
			
		||||
 | 
			
		||||
          this.plugins.push(plugin)
 | 
			
		||||
          this.plugins.push(this.parsePlugin(key, plugins[key]))
 | 
			
		||||
        }
 | 
			
		||||
      })
 | 
			
		||||
      .catch(error => { this.showError(error) })
 | 
			
		||||
@ -153,7 +114,7 @@ export default {
 | 
			
		||||
        let p = {}
 | 
			
		||||
 | 
			
		||||
        for (let field of plugin.fields) {
 | 
			
		||||
          p[field.name] = field.value
 | 
			
		||||
          p[field.variable] = field.value
 | 
			
		||||
 | 
			
		||||
          if (field.original === 'array') {
 | 
			
		||||
            let val = field.value.split(' ')
 | 
			
		||||
@ -161,7 +122,7 @@ export default {
 | 
			
		||||
              val.shift()
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            p[field.name] = val
 | 
			
		||||
            p[field.variable] = val
 | 
			
		||||
          }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@ -173,6 +134,43 @@ export default {
 | 
			
		||||
      api.updatePlugins(plugins)
 | 
			
		||||
        .then(() => { this.showSuccess('Plugins settings updated!') })
 | 
			
		||||
        .catch(error => { this.showError(error) })
 | 
			
		||||
    },
 | 
			
		||||
    parsePlugin (name, plugin) {
 | 
			
		||||
      let obj = {
 | 
			
		||||
        name: name,
 | 
			
		||||
        fields: []
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      for (let option of plugin) {
 | 
			
		||||
        let value = option.value
 | 
			
		||||
 | 
			
		||||
        let field = {
 | 
			
		||||
          name: option.name,
 | 
			
		||||
          variable: option.variable,
 | 
			
		||||
          type: 'text',
 | 
			
		||||
          original: 'text',
 | 
			
		||||
          value: value
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (Array.isArray(value)) {
 | 
			
		||||
          field.original = 'array'
 | 
			
		||||
          field.value = value.join(' ')
 | 
			
		||||
 | 
			
		||||
          obj.fields.push(field)
 | 
			
		||||
          continue
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        switch (typeof value) {
 | 
			
		||||
          case 'boolean':
 | 
			
		||||
            field.type = 'checkbox'
 | 
			
		||||
            field.original = 'boolean'
 | 
			
		||||
            break
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        obj.fields.push(field)
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      return obj
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -38,15 +38,15 @@ var (
 | 
			
		||||
// Hugo is a hugo (https://gohugo.io) plugin.
 | 
			
		||||
type Hugo struct {
 | 
			
		||||
	// Website root
 | 
			
		||||
	Root string `description:"The relative or absolute path to the place where your website is located."`
 | 
			
		||||
	Root string `name:"Website Root"`
 | 
			
		||||
	// Public folder
 | 
			
		||||
	Public string `description:"The relative or absolute path to the public folder."`
 | 
			
		||||
	Public string `name:"Public Directory"`
 | 
			
		||||
	// Hugo executable path
 | 
			
		||||
	Exe string `description:"The absolute path to the Hugo executable or the command to execute."`
 | 
			
		||||
	Exe string `name:"Hugo Executable"`
 | 
			
		||||
	// Hugo arguments
 | 
			
		||||
	Args []string `description:"The arguments to run when running Hugo"`
 | 
			
		||||
	Args []string `name:"Hugo Arguments"`
 | 
			
		||||
	// Indicates if we should clean public before a new publish.
 | 
			
		||||
	CleanPublic bool `description:"Indicates if the public folder should be cleaned before publishing the website."`
 | 
			
		||||
	CleanPublic bool `name:"Clean Public"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (h *Hugo) Find() error {
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										24
									
								
								settings.go
									
									
									
									
									
								
							
							
						
						
									
										24
									
								
								settings.go
									
									
									
									
									
								
							@ -4,6 +4,7 @@ import (
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"errors"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"reflect"
 | 
			
		||||
 | 
			
		||||
	"github.com/mitchellh/mapstructure"
 | 
			
		||||
)
 | 
			
		||||
@ -63,12 +64,33 @@ func pluginsHandler(c *RequestContext, w http.ResponseWriter, r *http.Request) (
 | 
			
		||||
	return http.StatusMethodNotAllowed, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type pluginOption struct {
 | 
			
		||||
	Variable string      `json:"variable"`
 | 
			
		||||
	Name     string      `json:"name"`
 | 
			
		||||
	Value    interface{} `json:"value"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func pluginsGetHandler(c *RequestContext, w http.ResponseWriter, r *http.Request) (int, error) {
 | 
			
		||||
	if !c.User.Admin {
 | 
			
		||||
		return http.StatusForbidden, nil
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return renderJSON(w, c.FM.Plugins)
 | 
			
		||||
	plugins := map[string][]pluginOption{}
 | 
			
		||||
 | 
			
		||||
	for name, p := range c.FM.Plugins {
 | 
			
		||||
		plugins[name] = []pluginOption{}
 | 
			
		||||
 | 
			
		||||
		t := reflect.TypeOf(p).Elem()
 | 
			
		||||
		for i := 0; i < t.NumField(); i++ {
 | 
			
		||||
			plugins[name] = append(plugins[name], pluginOption{
 | 
			
		||||
				Variable: t.Field(i).Name,
 | 
			
		||||
				Name:     t.Field(i).Tag.Get("name"),
 | 
			
		||||
				Value:    reflect.ValueOf(p).Elem().FieldByName(t.Field(i).Name).Interface(),
 | 
			
		||||
			})
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return renderJSON(w, plugins)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func pluginsPutHandler(c *RequestContext, w http.ResponseWriter, r *http.Request) (int, error) {
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user