Reformat code with go fmt

git-svn-id: file:///srv/svn/repo/marisa/trunk@31 d6811dac-2434-b64a-9ddc-f563ab233461
This commit is contained in:
dev
2021-10-19 10:20:42 +00:00
parent 6f7e7c7515
commit 57df304b1b

View File

@@ -1,52 +1,52 @@
package main package main
import ( import (
"fmt" "encoding/json"
"flag" "flag"
"fmt"
"html/template"
"io" "io"
"io/ioutil" "io/ioutil"
"log" "log"
"net/http" "net/http"
"os" "os"
"os/user" "os/user"
"time"
"path" "path"
"syscall"
"strconv"
"path/filepath" "path/filepath"
"html/template" "strconv"
"encoding/json" "syscall"
"time"
"github.com/dustin/go-humanize" "github.com/dustin/go-humanize"
"gopkg.in/ini.v1" "gopkg.in/ini.v1"
) )
type templatedata struct { type templatedata struct {
Links []string Links []string
Size string Size string
Maxsize string Maxsize string
} }
type metadata struct { type metadata struct {
Filename string Filename string
Size int64 Size int64
Expiry int64 Expiry int64
} }
var conf struct { var conf struct {
user string user string
group string group string
chroot string chroot string
bind string bind string
baseuri string baseuri string
rootdir string rootdir string
tmplpath string tmplpath string
filepath string filepath string
metapath string metapath string
filectx string filectx string
metactx string metactx string
maxsize int64 maxsize int64
expiry int64 expiry int64
} }
var verbose bool var verbose bool
@@ -92,12 +92,12 @@ func writemeta(filename string, expiry int64) error {
meta := metadata{ meta := metadata{
Filename: filepath.Base(filename), Filename: filepath.Base(filename),
Size: size, Size: size,
Expiry: time.Now().Unix() + expiry, Expiry: time.Now().Unix() + expiry,
} }
if verbose { if verbose {
log.Printf("Saving metadata for %s in %s", meta.Filename, conf.metapath + "/" + meta.Filename + ".json") log.Printf("Saving metadata for %s in %s", meta.Filename, conf.metapath+"/"+meta.Filename+".json")
} }
f, err := os.Create(conf.metapath + "/" + meta.Filename + ".json") f, err := os.Create(conf.metapath + "/" + meta.Filename + ".json")
@@ -196,13 +196,12 @@ func uploaderPost(w http.ResponseWriter, r *http.Request) {
writemeta(tmp.Name(), conf.expiry) writemeta(tmp.Name(), conf.expiry)
link := conf.baseuri + conf.filectx + filepath.Base(tmp.Name()) link := conf.baseuri + conf.filectx + filepath.Base(tmp.Name())
links = append(links, link) links = append(links, link)
} }
if (r.PostFormValue("output") == "html") { if r.PostFormValue("output") == "html" {
data := templatedata{ Links: links } data := templatedata{Links: links}
servetemplate(w, "/upload.html", data) servetemplate(w, "/upload.html", data)
return return
} else { } else {
@@ -216,16 +215,16 @@ func uploaderGet(w http.ResponseWriter, r *http.Request) {
// r.URL.Path is sanitized regarding "." and ".." // r.URL.Path is sanitized regarding "." and ".."
filename := r.URL.Path filename := r.URL.Path
if r.URL.Path == "/" || r.URL.Path == "/index.html" { if r.URL.Path == "/" || r.URL.Path == "/index.html" {
data := templatedata{ Maxsize: humanize.IBytes(uint64(conf.maxsize))} data := templatedata{Maxsize: humanize.IBytes(uint64(conf.maxsize))}
servetemplate(w, "/index.html", data) servetemplate(w, "/index.html", data)
return return
} }
if verbose { if verbose {
log.Printf("Serving file %s", conf.rootdir + filename) log.Printf("Serving file %s", conf.rootdir+filename)
} }
http.ServeFile(w, r, conf.rootdir + filename) http.ServeFile(w, r, conf.rootdir+filename)
} }
func uploader(w http.ResponseWriter, r *http.Request) { func uploader(w http.ResponseWriter, r *http.Request) {
@@ -245,21 +244,21 @@ func uploader(w http.ResponseWriter, r *http.Request) {
func main() { func main() {
var file string var file string
flag.StringVar(&file, "f", "", "Configuration file") flag.StringVar(&file, "f", "", "Configuration file")
flag.BoolVar(&verbose, "v", false, "Verbose logging") flag.BoolVar(&verbose, "v", false, "Verbose logging")
flag.Parse() flag.Parse()
/* default values */ /* default values */
conf.bind = "0.0.0.0:8080" conf.bind = "0.0.0.0:8080"
conf.baseuri = "http://127.0.0.1:8080" conf.baseuri = "http://127.0.0.1:8080"
conf.rootdir = "/htdocs" conf.rootdir = "/htdocs"
conf.tmplpath = "/htdocs/templates" conf.tmplpath = "/htdocs/templates"
conf.filepath = "/htdocs/files" conf.filepath = "/htdocs/files"
conf.metapath = "/htdocs/meta" conf.metapath = "/htdocs/meta"
conf.filectx = "/f/" conf.filectx = "/f/"
conf.metactx = "/m/" conf.metactx = "/m/"
conf.maxsize = 34359738368 conf.maxsize = 34359738368
conf.expiry = 86400 conf.expiry = 86400
if file != "" { if file != "" {
if verbose { if verbose {
@@ -272,26 +271,26 @@ func main() {
return return
} }
conf.bind = cfg.Section("").Key("bind").String() conf.bind = cfg.Section("").Key("bind").String()
conf.user = cfg.Section("").Key("user").String() conf.user = cfg.Section("").Key("user").String()
conf.group = cfg.Section("").Key("group").String() conf.group = cfg.Section("").Key("group").String()
conf.baseuri = cfg.Section("").Key("baseuri").String() conf.baseuri = cfg.Section("").Key("baseuri").String()
conf.filepath = cfg.Section("").Key("filepath").String() conf.filepath = cfg.Section("").Key("filepath").String()
conf.metapath = cfg.Section("").Key("metapath").String() conf.metapath = cfg.Section("").Key("metapath").String()
conf.filectx = cfg.Section("").Key("filectx").String() conf.filectx = cfg.Section("").Key("filectx").String()
conf.metactx = cfg.Section("").Key("metactx").String() conf.metactx = cfg.Section("").Key("metactx").String()
conf.rootdir = cfg.Section("").Key("rootdir").String() conf.rootdir = cfg.Section("").Key("rootdir").String()
conf.chroot = cfg.Section("").Key("chroot").String() conf.chroot = cfg.Section("").Key("chroot").String()
conf.tmplpath = cfg.Section("").Key("tmplpath").String() conf.tmplpath = cfg.Section("").Key("tmplpath").String()
conf.maxsize, _ = cfg.Section("").Key("maxsize").Int64() conf.maxsize, _ = cfg.Section("").Key("maxsize").Int64()
conf.expiry, _ = cfg.Section("").Key("expiry").Int64() conf.expiry, _ = cfg.Section("").Key("expiry").Int64()
} }
if verbose { if verbose {
log.Printf("Applied configuration:\n%s", conf) log.Printf("Applied configuration:\n%s", conf)
} }
if (conf.chroot != "") { if conf.chroot != "" {
if verbose { if verbose {
log.Printf("Changing root to %s", conf.chroot) log.Printf("Changing root to %s", conf.chroot)
} }