|
|
|
@ -14,10 +14,11 @@ import ( |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
type editSession struct { |
|
|
|
|
clients uint |
|
|
|
|
name string |
|
|
|
|
doc *acedoc.Document |
|
|
|
|
tmpdir string |
|
|
|
|
clients uint |
|
|
|
|
docname string |
|
|
|
|
filename string |
|
|
|
|
doc *acedoc.Document |
|
|
|
|
tmpdir string |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (ch *CaddyHugo) newEditSession(docName string) (*editSession, error) { |
|
|
|
@ -32,25 +33,32 @@ func (ch *CaddyHugo) newEditSession(docName string) (*editSession, error) { |
|
|
|
|
draftPrefix := fmt.Sprintf("draft-%s", base64.RawURLEncoding.EncodeToString([]byte(docName))) |
|
|
|
|
tmpdir := path.Join(os.TempDir(), draftPrefix) |
|
|
|
|
|
|
|
|
|
ref := &editSession{ |
|
|
|
|
name: docName, |
|
|
|
|
doc: acedoc.NewString(string(contents)), |
|
|
|
|
tmpdir: tmpdir, |
|
|
|
|
es := &editSession{ |
|
|
|
|
docname: docName, |
|
|
|
|
filename: filename, |
|
|
|
|
doc: acedoc.NewString(string(contents)), |
|
|
|
|
tmpdir: tmpdir, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
err = ref.doc.LogToFile(path.Join(ch.Dir, "logs", docName)) |
|
|
|
|
err = es.doc.LogToFile(path.Join(ch.Dir, "logs", docName)) |
|
|
|
|
if err != nil { |
|
|
|
|
fmt.Println(err) |
|
|
|
|
return nil, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
ch.docs[filename] = ref |
|
|
|
|
ch.docs[filename] = es |
|
|
|
|
|
|
|
|
|
hugoCmd := exec.Command("hugo", "--watch", "-D", "-d", ref.tmpdir) |
|
|
|
|
ch.renderDraft(es) |
|
|
|
|
|
|
|
|
|
return es, nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (ch *CaddyHugo) renderDraft(es *editSession) error { |
|
|
|
|
hugoCmd := exec.Command("hugo", "--watch", "-D", "-d", es.tmpdir) |
|
|
|
|
hugoCmd.Dir = ch.Dir |
|
|
|
|
err = hugoCmd.Start() |
|
|
|
|
err := hugoCmd.Start() |
|
|
|
|
if err != nil { |
|
|
|
|
return nil, fmt.Errorf("error starting hugo: %v", err) |
|
|
|
|
return fmt.Errorf("error starting hugo: %v", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
go func() { |
|
|
|
@ -72,12 +80,12 @@ func (ch *CaddyHugo) newEditSession(docName string) (*editSession, error) { |
|
|
|
|
<-ticker.C |
|
|
|
|
ch.mtx.Lock() |
|
|
|
|
|
|
|
|
|
err := ioutil.WriteFile(filename, []byte(ref.doc.Contents()), 0644) |
|
|
|
|
err := ioutil.WriteFile(es.filename, []byte(es.doc.Contents()), 0644) |
|
|
|
|
if err != nil { |
|
|
|
|
fmt.Println("error saving document contents:", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if ref.clients == 0 { |
|
|
|
|
if es.clients == 0 { |
|
|
|
|
idleTicks++ |
|
|
|
|
idleTime := time.Duration(idleTicks) * WebsocketFileTicker |
|
|
|
|
if idleTime >= IdleWebsocketTimeout { |
|
|
|
@ -87,11 +95,13 @@ func (ch *CaddyHugo) newEditSession(docName string) (*editSession, error) { |
|
|
|
|
fmt.Printf(", error publishing: %v\n", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
ref.doc.Close() |
|
|
|
|
os.RemoveAll(tmpdir) |
|
|
|
|
delete(ch.docs, filename) |
|
|
|
|
es.doc.Close() |
|
|
|
|
os.RemoveAll(es.tmpdir) |
|
|
|
|
delete(ch.docs, es.filename) |
|
|
|
|
ch.mtx.Unlock() |
|
|
|
|
return |
|
|
|
|
} else { |
|
|
|
|
fmt.Println("idle for", idleTime) |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
idleTicks = 0 |
|
|
|
@ -100,7 +110,7 @@ func (ch *CaddyHugo) newEditSession(docName string) (*editSession, error) { |
|
|
|
|
} |
|
|
|
|
}() |
|
|
|
|
|
|
|
|
|
return ref, nil |
|
|
|
|
return nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (ch *CaddyHugo) hasEditSession(docName string) (*editSession, bool) { |
|
|
|
|