Skip to content

Commit

Permalink
add line ending conversion option
Browse files Browse the repository at this point in the history
  • Loading branch information
tenox7 committed May 22, 2024
1 parent fb87bf4 commit 6b193a1
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
29 changes: 27 additions & 2 deletions dialogs.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
package main

import (
"bytes"
"fmt"
"html"
"runtime"
"strings"

"github.com/dustin/go-humanize"
"github.com/spf13/afero"
)

func selOpt(s string, f ...struct{ v, n string }) string {
var o []string
var m = make(map[string]string)
m[s] = "SELECTED"
m[""] = "DISABLED"
for _, i := range f {
o = append(o, fmt.Sprintf("<OPTION VALUE=\"%v\" %v>%v</OPTION>", i.v, m[i.v], i.n))
}
return strings.Join(o, "\n")
}

func (r *wfmRequest) prompt(action string, mul []string) {
header(r.w, r.uDir, r.eSort, "", r.modern)

Expand Down Expand Up @@ -120,17 +133,29 @@ func (r *wfmRequest) editText() {
htErr(r.w, "Unable to read file", err)
return
}
le := *defLe
if bytes.IndexByte(f, '\r') != -1 {
le = "CRLF"
}
header(r.w, r.uDir, r.eSort, `html, body, table, textarea, form { box-sizing: border-box; height:98%; }`, r.modern)
r.w.Write([]byte(`
<TABLE BGCOLOR="#EEEEEE" BORDER="0" CELLSPACING="0" CELLPADDING="5" STYLE="width: 100%; height: 100%;">
<TR STYLE="height:1%;">
<TD ALIGN="LEFT" VALIGN="MIDDLE" BGCOLOR="#CCCCCC">File Editor: ` + html.EscapeString(r.uFbn) + `</TD>
<TD BGCOLOR="#CCCCCC" ALIGN="RIGHT">&nbsp;</TD>
<TD BGCOLOR="#CCCCCC" ALIGN="RIGHT">
Line Endings:
<SELECT NAME="crlf">
` + selOpt(le, []struct{ v, n string }{
{"LF", "LF (Unix)"},
{"CRLF", "CRLF (Windows)"},
}...) + `
</SELECT>
</TD>
</TR>
<TR STYLE="height:98%;">
<TD COLSPAN="2" ALIGN="CENTER" VALIGN="MIDDLE" STYLE="height:100%;">
<TEXTAREA NAME="text" SPELLCHECK="false" COLS="80" ROWS="24" STYLE="width: 99%; height: 99%;">` + html.EscapeString(string(f)) + `</TEXTAREA><P>
</TD></TR><TR STYLE="height:1%;"><TD ALIGN="RIGHT">
</TD></TR><TR STYLE="height:1%;"><TD>&nbsp;</TD><TD ALIGN="RIGHT">
<INPUT TYPE="SUBMIT" NAME="save" VALUE="Save" ` + disTag[r.rwAccess] + `>&nbsp;
<INPUT TYPE="SUBMIT" NAME="cancel" VALUE="Cancel">
<INPUT TYPE="HIDDEN" NAME="dir" VALUE="` + html.EscapeString(r.uDir) + `">
Expand Down
8 changes: 7 additions & 1 deletion fileio.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func (r *wfmRequest) uploadFile(h *multipart.FileHeader, f multipart.File) {
redirect(r.w, wfmPfx+"?dir="+url.PathEscape(r.uDir)+"&sort="+r.eSort+"&hi="+url.PathEscape(h.Filename))
}

func (r *wfmRequest) saveText(uData string) {
func (r *wfmRequest) saveText(uData, crlf string) {
if !r.rwAccess {
htErr(r.w, "permission", fmt.Errorf("read only"))
return
Expand All @@ -151,6 +151,12 @@ func (r *wfmRequest) saveText(uData string) {
htErr(r.w, "text save", fmt.Errorf("zero lenght data"))
return
}
switch crlf {
case "CRLF":
uData = strings.ReplaceAll(uData, "\n", "\r\n")
case "LF":
uData = strings.ReplaceAll(uData, "\r\n", "\n")
}
fp := r.uDir + "/" + r.uFbn
tmpName := fp + ".tmp"
err := afero.WriteFile(r.fs, tmpName, []byte(uData), 0644)
Expand Down
2 changes: 1 addition & 1 deletion handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func wfmMain(w http.ResponseWriter, r *http.Request) {
wfm.uploadFile(h, f)
return
case r.FormValue("save") != "":
wfm.saveText(r.FormValue("text"))
wfm.saveText(r.FormValue("text"), r.FormValue("crlf"))
return
case r.FormValue("up") != "":
up, err := url.JoinPath(wfmPfx, filepath.Dir(wfm.uDir))
Expand Down
1 change: 1 addition & 0 deletions wfm.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ var (
rateLim = flag.Int("rate_limit", 0, "rate limit for upload/download in MB/s, 0 no limit")
formMaxMem = flag.Int64("form_maxmem", 10<<20, "maximum memory used for form parsing, increase for large uploads")
prefix = flag.String("prefix", "/:/", "Prefix for WFM access, /fsdir:/htpath eg.: /var/files:/myfiles")
defLe = flag.String("txt_le", "LF", "default line endings when editing text files")
wfmFs afero.Fs
wfmPfx string
cacheCtl = flag.String("cache_ctl", "no-cache", "HTTP Header Cache Control")
Expand Down

0 comments on commit 6b193a1

Please sign in to comment.