You've already forked filebrowser
mirror of
https://github.com/filebrowser/filebrowser.git
synced 2025-11-26 14:25:26 +08:00
updates
This commit is contained in:
363
editor/editor.go
363
editor/editor.go
@@ -25,186 +25,15 @@ type editor struct {
|
||||
FrontMatter interface{}
|
||||
}
|
||||
|
||||
// Execute sth
|
||||
func Execute(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||
// ServeHTTP is...
|
||||
func ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||
filename := strings.Replace(r.URL.Path, "/admin/edit/", "", 1)
|
||||
|
||||
if r.Method == "POST" {
|
||||
// Get the JSON information sent using a buffer
|
||||
rawBuffer := new(bytes.Buffer)
|
||||
rawBuffer.ReadFrom(r.Body)
|
||||
|
||||
// Creates the raw file "map" using the JSON
|
||||
var rawFile map[string]interface{}
|
||||
json.Unmarshal(rawBuffer.Bytes(), &rawFile)
|
||||
|
||||
// Initializes the file content to write
|
||||
var file []byte
|
||||
|
||||
switch r.Header.Get("X-Content-Type") {
|
||||
case "frontmatter-only":
|
||||
frontmatter := strings.TrimPrefix(filepath.Ext(filename), ".")
|
||||
var mark rune
|
||||
|
||||
switch frontmatter {
|
||||
case "toml":
|
||||
mark = rune('+')
|
||||
case "json":
|
||||
mark = rune('{')
|
||||
case "yaml":
|
||||
mark = rune('-')
|
||||
default:
|
||||
return 400, nil
|
||||
}
|
||||
|
||||
f, err := parser.InterfaceToFrontMatter(rawFile, mark)
|
||||
fString := string(f)
|
||||
|
||||
// If it's toml or yaml, strip frontmatter identifier
|
||||
if frontmatter == "toml" {
|
||||
fString = strings.TrimSuffix(fString, "+++\n")
|
||||
fString = strings.TrimPrefix(fString, "+++\n")
|
||||
}
|
||||
|
||||
if frontmatter == "yaml" {
|
||||
fString = strings.TrimSuffix(fString, "---\n")
|
||||
fString = strings.TrimPrefix(fString, "---\n")
|
||||
}
|
||||
|
||||
f = []byte(fString)
|
||||
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
return 500, err
|
||||
}
|
||||
|
||||
file = f
|
||||
case "content-only":
|
||||
// The main content of the file
|
||||
mainContent := rawFile["content"].(string)
|
||||
mainContent = "\n\n" + strings.TrimSpace(mainContent)
|
||||
|
||||
file = []byte(mainContent)
|
||||
case "full":
|
||||
// The main content of the file
|
||||
mainContent := rawFile["content"].(string)
|
||||
mainContent = "\n\n" + strings.TrimSpace(mainContent)
|
||||
|
||||
// Removes the main content from the rest of the frontmatter
|
||||
delete(rawFile, "content")
|
||||
|
||||
// Converts the frontmatter in JSON
|
||||
jsonFrontmatter, err := json.Marshal(rawFile)
|
||||
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
return 500, err
|
||||
}
|
||||
|
||||
// Indents the json
|
||||
frontMatterBuffer := new(bytes.Buffer)
|
||||
json.Indent(frontMatterBuffer, jsonFrontmatter, "", " ")
|
||||
|
||||
// Generates the final file
|
||||
f := new(bytes.Buffer)
|
||||
f.Write(frontMatterBuffer.Bytes())
|
||||
f.Write([]byte(mainContent))
|
||||
file = f.Bytes()
|
||||
default:
|
||||
return 400, nil
|
||||
}
|
||||
|
||||
// Write the file
|
||||
err := ioutil.WriteFile(filename, file, 0666)
|
||||
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
return 500, err
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write([]byte("{}"))
|
||||
} else {
|
||||
// Check if the file format is supported. If not, send a "Not Acceptable"
|
||||
// header and an error
|
||||
if !CanBeEdited(filename) {
|
||||
return 406, errors.New("File format not supported.")
|
||||
}
|
||||
|
||||
// Check if the file exists. If it doesn't, send a "Not Found" message
|
||||
if _, err := os.Stat(filename); os.IsNotExist(err) {
|
||||
log.Print(err)
|
||||
return 404, nil
|
||||
}
|
||||
|
||||
// Open the file and check if there was some error while opening
|
||||
file, err := ioutil.ReadFile(filename)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
return 500, err
|
||||
}
|
||||
|
||||
// Create a new editor variable and set the extension
|
||||
page := new(editor)
|
||||
page.Extension = strings.TrimPrefix(filepath.Ext(filename), ".")
|
||||
page.Name = filename
|
||||
|
||||
// Handle the content depending on the file extension
|
||||
switch page.Extension {
|
||||
case "markdown", "md":
|
||||
if hasFrontMatterRune(file) {
|
||||
// Starts a new buffer and parses the file using Hugo's functions
|
||||
buffer := bytes.NewBuffer(file)
|
||||
file, err := parser.ReadFrom(buffer)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
return 500, err
|
||||
}
|
||||
|
||||
// Parses the page content and the frontmatter
|
||||
page.Content = strings.TrimSpace(string(file.Content()))
|
||||
page.FrontMatter, err = frontmatter.Pretty(file.FrontMatter())
|
||||
page.Class = "full"
|
||||
} else {
|
||||
// The editor will handle only content
|
||||
page.Class = "content-only"
|
||||
page.Content = string(file)
|
||||
}
|
||||
case "json", "toml", "yaml":
|
||||
// Defines the class and declares an error
|
||||
page.Class = "frontmatter-only"
|
||||
var err error
|
||||
|
||||
// Checks if the file already has the frontmatter rune and parses it
|
||||
if hasFrontMatterRune(file) {
|
||||
page.FrontMatter, err = frontmatter.Pretty(file)
|
||||
} else {
|
||||
page.FrontMatter, err = frontmatter.Pretty(appendFrontMatterRune(file, page.Extension))
|
||||
}
|
||||
|
||||
// Check if there were any errors
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
return 500, err
|
||||
}
|
||||
default:
|
||||
// The editor will handle only content
|
||||
page.Class = "content-only"
|
||||
page.Content = string(file)
|
||||
}
|
||||
|
||||
// Create the functions map, then the template, check for erros and
|
||||
// execute the template if there aren't errors
|
||||
functions := template.FuncMap{"splitCapitalize": utils.SplitCapitalize}
|
||||
tpl, err := utils.GetTemplate(r, functions, "editor", "frontmatter")
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
return 500, err
|
||||
}
|
||||
tpl.Execute(w, page)
|
||||
return post(w, r, filename)
|
||||
}
|
||||
|
||||
return 200, nil
|
||||
return get(w, r, filename)
|
||||
}
|
||||
|
||||
// CanBeEdited checks if a file has a supported extension
|
||||
@@ -225,6 +54,190 @@ func CanBeEdited(filename string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func post(w http.ResponseWriter, r *http.Request, filename string) (int, error) {
|
||||
// Get the JSON information sent using a buffer
|
||||
rawBuffer := new(bytes.Buffer)
|
||||
rawBuffer.ReadFrom(r.Body)
|
||||
|
||||
// Creates the raw file "map" using the JSON
|
||||
var rawFile map[string]interface{}
|
||||
json.Unmarshal(rawBuffer.Bytes(), &rawFile)
|
||||
|
||||
// Initializes the file content to write
|
||||
var file []byte
|
||||
|
||||
switch r.Header.Get("X-Content-Type") {
|
||||
case "frontmatter-only":
|
||||
frontmatter := strings.TrimPrefix(filepath.Ext(filename), ".")
|
||||
var mark rune
|
||||
|
||||
switch frontmatter {
|
||||
case "toml":
|
||||
mark = rune('+')
|
||||
case "json":
|
||||
mark = rune('{')
|
||||
case "yaml":
|
||||
mark = rune('-')
|
||||
default:
|
||||
return 400, nil
|
||||
}
|
||||
|
||||
f, err := parser.InterfaceToFrontMatter(rawFile, mark)
|
||||
fString := string(f)
|
||||
|
||||
// If it's toml or yaml, strip frontmatter identifier
|
||||
if frontmatter == "toml" {
|
||||
fString = strings.TrimSuffix(fString, "+++\n")
|
||||
fString = strings.TrimPrefix(fString, "+++\n")
|
||||
}
|
||||
|
||||
if frontmatter == "yaml" {
|
||||
fString = strings.TrimSuffix(fString, "---\n")
|
||||
fString = strings.TrimPrefix(fString, "---\n")
|
||||
}
|
||||
|
||||
f = []byte(fString)
|
||||
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
return 500, err
|
||||
}
|
||||
|
||||
file = f
|
||||
case "content-only":
|
||||
// The main content of the file
|
||||
mainContent := rawFile["content"].(string)
|
||||
mainContent = "\n\n" + strings.TrimSpace(mainContent)
|
||||
|
||||
file = []byte(mainContent)
|
||||
case "full":
|
||||
// The main content of the file
|
||||
mainContent := rawFile["content"].(string)
|
||||
mainContent = "\n\n" + strings.TrimSpace(mainContent)
|
||||
|
||||
// Removes the main content from the rest of the frontmatter
|
||||
delete(rawFile, "content")
|
||||
|
||||
// Converts the frontmatter in JSON
|
||||
jsonFrontmatter, err := json.Marshal(rawFile)
|
||||
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
return 500, err
|
||||
}
|
||||
|
||||
// Indents the json
|
||||
frontMatterBuffer := new(bytes.Buffer)
|
||||
json.Indent(frontMatterBuffer, jsonFrontmatter, "", " ")
|
||||
|
||||
// Generates the final file
|
||||
f := new(bytes.Buffer)
|
||||
f.Write(frontMatterBuffer.Bytes())
|
||||
f.Write([]byte(mainContent))
|
||||
file = f.Bytes()
|
||||
default:
|
||||
return 400, nil
|
||||
}
|
||||
|
||||
// Write the file
|
||||
err := ioutil.WriteFile(filename, file, 0666)
|
||||
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
return 500, err
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write([]byte("{}"))
|
||||
return 200, nil
|
||||
}
|
||||
|
||||
func get(w http.ResponseWriter, r *http.Request, filename string) (int, error) {
|
||||
// Check if the file format is supported. If not, send a "Not Acceptable"
|
||||
// header and an error
|
||||
if !CanBeEdited(filename) {
|
||||
return 406, errors.New("File format not supported.")
|
||||
}
|
||||
|
||||
// Check if the file exists. If it doesn't, send a "Not Found" message
|
||||
if _, err := os.Stat(filename); os.IsNotExist(err) {
|
||||
log.Print(err)
|
||||
return 404, nil
|
||||
}
|
||||
|
||||
// Open the file and check if there was some error while opening
|
||||
file, err := ioutil.ReadFile(filename)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
return 500, err
|
||||
}
|
||||
|
||||
// Create a new editor variable and set the extension
|
||||
page := new(editor)
|
||||
page.Extension = strings.TrimPrefix(filepath.Ext(filename), ".")
|
||||
page.Name = filename
|
||||
|
||||
// Handle the content depending on the file extension
|
||||
switch page.Extension {
|
||||
case "markdown", "md":
|
||||
page.Extension = "markdown"
|
||||
if hasFrontMatterRune(file) {
|
||||
// Starts a new buffer and parses the file using Hugo's functions
|
||||
buffer := bytes.NewBuffer(file)
|
||||
file, err := parser.ReadFrom(buffer)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
return 500, err
|
||||
}
|
||||
|
||||
// Parses the page content and the frontmatter
|
||||
page.Content = strings.TrimSpace(string(file.Content()))
|
||||
page.FrontMatter, err = frontmatter.Pretty(file.FrontMatter())
|
||||
page.Class = "full"
|
||||
} else {
|
||||
// The editor will handle only content
|
||||
page.Class = "content-only"
|
||||
page.Content = string(file)
|
||||
}
|
||||
case "json", "toml", "yaml":
|
||||
// Defines the class and declares an error
|
||||
page.Class = "frontmatter-only"
|
||||
var err error
|
||||
|
||||
// Checks if the file already has the frontmatter rune and parses it
|
||||
if hasFrontMatterRune(file) {
|
||||
page.FrontMatter, err = frontmatter.Pretty(file)
|
||||
} else {
|
||||
page.FrontMatter, err = frontmatter.Pretty(appendFrontMatterRune(file, page.Extension))
|
||||
}
|
||||
|
||||
// Check if there were any errors
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
return 500, err
|
||||
}
|
||||
default:
|
||||
// The editor will handle only content
|
||||
page.Class = "content-only"
|
||||
page.Content = string(file)
|
||||
}
|
||||
|
||||
// Create the functions map, then the template, check for erros and
|
||||
// execute the template if there aren't errors
|
||||
functions := template.FuncMap{
|
||||
"splitCapitalize": utils.SplitCapitalize,
|
||||
"Defined": utils.Defined,
|
||||
}
|
||||
|
||||
tpl, err := utils.GetTemplate(r, functions, "editor", "frontmatter")
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
return 500, err
|
||||
}
|
||||
|
||||
return 200, tpl.Execute(w, page)
|
||||
}
|
||||
|
||||
func hasFrontMatterRune(file []byte) bool {
|
||||
return strings.HasPrefix(string(file), "---") ||
|
||||
strings.HasPrefix(string(file), "+++") ||
|
||||
|
||||
Reference in New Issue
Block a user