schedule in a function
parent
62ed665af2
commit
a717ac9fad
|
@ -72,7 +72,6 @@ func post(w http.ResponseWriter, r *http.Request) (int, error) {
|
|||
if r.Header.Get("X-Upload") == "true" {
|
||||
// Parse the multipart form in the request
|
||||
err := r.ParseMultipartForm(100000)
|
||||
|
||||
if err != nil {
|
||||
w.Write([]byte(err.Error()))
|
||||
return 500, err
|
||||
|
@ -146,7 +145,6 @@ func post(w http.ResponseWriter, r *http.Request) (int, error) {
|
|||
// Check if the archetype ending with .markdown exists
|
||||
if _, err := os.Stat(archetype + ".markdown"); err == nil {
|
||||
err = utils.CopyFile(archetype+".markdown", filename)
|
||||
|
||||
if err != nil {
|
||||
w.Write([]byte(err.Error()))
|
||||
return 500, err
|
||||
|
@ -161,7 +159,6 @@ func post(w http.ResponseWriter, r *http.Request) (int, error) {
|
|||
// Check if the archetype ending with .md exists
|
||||
if _, err := os.Stat(archetype + ".md"); err == nil {
|
||||
err = utils.CopyFile(archetype+".md", filename)
|
||||
|
||||
if err != nil {
|
||||
w.Write([]byte(err.Error()))
|
||||
return 500, err
|
||||
|
@ -175,7 +172,6 @@ func post(w http.ResponseWriter, r *http.Request) (int, error) {
|
|||
}
|
||||
|
||||
wf, err := os.Create(filename)
|
||||
|
||||
if err != nil {
|
||||
w.Write([]byte(err.Error()))
|
||||
return 500, err
|
||||
|
|
101
editor/editor.go
101
editor/editor.go
|
@ -33,14 +33,17 @@ type editor struct {
|
|||
func ServeHTTP(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
|
||||
filename := strings.Replace(r.URL.Path, "/admin/edit/", "", 1)
|
||||
|
||||
if r.Method == "POST" {
|
||||
return servePost(w, r, c, filename)
|
||||
switch r.Method {
|
||||
case "POST":
|
||||
return post(w, r, c, filename)
|
||||
case "GET":
|
||||
return get(w, r, c, filename)
|
||||
default:
|
||||
return 400, nil
|
||||
}
|
||||
|
||||
return serveGet(w, r, c, filename)
|
||||
}
|
||||
|
||||
func servePost(w http.ResponseWriter, r *http.Request, c *config.Config, filename string) (int, error) {
|
||||
func post(w http.ResponseWriter, r *http.Request, c *config.Config, filename string) (int, error) {
|
||||
// Get the JSON information sent using a buffer
|
||||
rawBuffer := new(bytes.Buffer)
|
||||
rawBuffer.ReadFrom(r.Body)
|
||||
|
@ -106,45 +109,12 @@ func servePost(w http.ResponseWriter, r *http.Request, c *config.Config, filenam
|
|||
|
||||
// Schedule the post
|
||||
if r.Header.Get("X-Schedule") == "true" {
|
||||
t, err := time.Parse("2006-01-02 15:04:05-07:00", rawFile["date"].(string))
|
||||
_, err := schedule(w, c, filename, rawFile)
|
||||
|
||||
if err != nil {
|
||||
w.Write([]byte(err.Error()))
|
||||
return 500, err
|
||||
}
|
||||
|
||||
scheduler := cron.New()
|
||||
scheduler.AddFunc(t.In(time.Now().Location()).Format("05 04 15 02 01 *"), func() {
|
||||
// Set draft to false
|
||||
rawFile["draft"] = false
|
||||
|
||||
// Converts the frontmatter in JSON
|
||||
jsonFrontmatter, err := json.Marshal(rawFile)
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// 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()
|
||||
|
||||
// Write the file
|
||||
err = ioutil.WriteFile(filename, file, 0666)
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
utils.RunHugo(c)
|
||||
})
|
||||
scheduler.Start()
|
||||
}
|
||||
|
||||
// Converts the frontmatter in JSON
|
||||
|
@ -181,7 +151,58 @@ func servePost(w http.ResponseWriter, r *http.Request, c *config.Config, filenam
|
|||
return 200, nil
|
||||
}
|
||||
|
||||
func serveGet(w http.ResponseWriter, r *http.Request, c *config.Config, filename string) (int, error) {
|
||||
func schedule(w http.ResponseWriter, c *config.Config, filename string, raw map[string]interface{}) (int, error) {
|
||||
// The main content of the file
|
||||
content := raw["content"].(string)
|
||||
content = "\n\n" + strings.TrimSpace(content)
|
||||
|
||||
// Removes the main content from the rest of the frontmatter
|
||||
delete(raw, "content")
|
||||
|
||||
// Parse the time
|
||||
t, err := time.Parse("2006-01-02 15:04:05-07:00", raw["date"].(string))
|
||||
|
||||
if err != nil {
|
||||
w.Write([]byte(err.Error()))
|
||||
return 500, err
|
||||
}
|
||||
|
||||
scheduler := cron.New()
|
||||
scheduler.AddFunc(t.In(time.Now().Location()).Format("05 04 15 02 01 *"), func() {
|
||||
// Set draft to false
|
||||
raw["draft"] = false
|
||||
|
||||
// Converts the frontmatter in JSON
|
||||
jsonFrontmatter, err := json.Marshal(raw)
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// 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(content))
|
||||
file := f.Bytes()
|
||||
|
||||
// Write the file
|
||||
err = ioutil.WriteFile(filename, file, 0666)
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
utils.RunHugo(c)
|
||||
})
|
||||
scheduler.Start()
|
||||
return 200, nil
|
||||
}
|
||||
|
||||
func get(w http.ResponseWriter, r *http.Request, c *config.Config, filename string) (int, error) {
|
||||
// Check if the file format is supported. If not, send a "Not Acceptable"
|
||||
// header and an error
|
||||
if !utils.CanBeEdited(filename) {
|
||||
|
|
Loading…
Reference in New Issue