Changeset 31 in code for trunk/partage.go
- Timestamp:
- Oct 19, 2021, 10:20:42 AM (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/partage.go
r30 r31 2 2 3 3 import ( 4 "encoding/json" 5 "flag" 4 6 "fmt" 5 " flag"7 "html/template" 6 8 "io" 7 9 "io/ioutil" … … 10 12 "os" 11 13 "os/user" 14 "path" 15 "path/filepath" 16 "strconv" 17 "syscall" 12 18 "time" 13 "path"14 "syscall"15 "strconv"16 "path/filepath"17 "html/template"18 "encoding/json"19 19 20 20 "github.com/dustin/go-humanize" … … 23 23 24 24 type templatedata struct { 25 Links []string26 Size string25 Links []string 26 Size string 27 27 Maxsize string 28 28 } … … 30 30 type metadata struct { 31 31 Filename string 32 Size int6433 Expiry int6432 Size int64 33 Expiry int64 34 34 } 35 35 36 36 var conf struct { 37 user 38 group 39 chroot 40 bind 41 baseuri 42 rootdir 43 tmplpath 44 filepath 45 metapath 46 filectx 47 metactx 48 maxsize 49 expiry 37 user string 38 group string 39 chroot string 40 bind string 41 baseuri string 42 rootdir string 43 tmplpath string 44 filepath string 45 metapath string 46 filectx string 47 metactx string 48 maxsize int64 49 expiry int64 50 50 } 51 51 … … 93 93 meta := metadata{ 94 94 Filename: filepath.Base(filename), 95 Size: size,96 Expiry: time.Now().Unix() + expiry,97 } 98 99 if verbose { 100 log.Printf("Saving metadata for %s in %s", meta.Filename, conf.metapath + "/" + meta.Filename +".json")95 Size: size, 96 Expiry: time.Now().Unix() + expiry, 97 } 98 99 if verbose { 100 log.Printf("Saving metadata for %s in %s", meta.Filename, conf.metapath+"/"+meta.Filename+".json") 101 101 } 102 102 … … 197 197 writemeta(tmp.Name(), conf.expiry) 198 198 199 200 199 link := conf.baseuri + conf.filectx + filepath.Base(tmp.Name()) 201 200 links = append(links, link) 202 201 } 203 202 204 if (r.PostFormValue("output") == "html"){205 data := templatedata{ Links: links}203 if r.PostFormValue("output") == "html" { 204 data := templatedata{Links: links} 206 205 servetemplate(w, "/upload.html", data) 207 206 return … … 217 216 filename := r.URL.Path 218 217 if r.URL.Path == "/" || r.URL.Path == "/index.html" { 219 data := templatedata{ 218 data := templatedata{Maxsize: humanize.IBytes(uint64(conf.maxsize))} 220 219 servetemplate(w, "/index.html", data) 221 220 return … … 223 222 224 223 if verbose { 225 log.Printf("Serving file %s", conf.rootdir +filename)226 } 227 228 http.ServeFile(w, r, conf.rootdir +filename)224 log.Printf("Serving file %s", conf.rootdir+filename) 225 } 226 227 http.ServeFile(w, r, conf.rootdir+filename) 229 228 } 230 229 … … 246 245 func main() { 247 246 var file string 248 flag.StringVar(&file, 247 flag.StringVar(&file, "f", "", "Configuration file") 249 248 flag.BoolVar(&verbose, "v", false, "Verbose logging") 250 249 flag.Parse() 251 250 252 251 /* default values */ 253 conf.bind 254 conf.baseuri 255 conf.rootdir 252 conf.bind = "0.0.0.0:8080" 253 conf.baseuri = "http://127.0.0.1:8080" 254 conf.rootdir = "/htdocs" 256 255 conf.tmplpath = "/htdocs/templates" 257 256 conf.filepath = "/htdocs/files" 258 257 conf.metapath = "/htdocs/meta" 259 conf.filectx 260 conf.metactx 261 conf.maxsize 262 conf.expiry 258 conf.filectx = "/f/" 259 conf.metactx = "/m/" 260 conf.maxsize = 34359738368 261 conf.expiry = 86400 263 262 264 263 if file != "" { … … 273 272 } 274 273 275 conf.bind 276 conf.user 277 conf.group 278 conf.baseuri 279 conf.filepath 280 conf.metapath 281 conf.filectx 282 conf.metactx 283 conf.rootdir 284 conf.chroot 285 conf.tmplpath 286 conf.maxsize, _ 287 conf.expiry, _ 274 conf.bind = cfg.Section("").Key("bind").String() 275 conf.user = cfg.Section("").Key("user").String() 276 conf.group = cfg.Section("").Key("group").String() 277 conf.baseuri = cfg.Section("").Key("baseuri").String() 278 conf.filepath = cfg.Section("").Key("filepath").String() 279 conf.metapath = cfg.Section("").Key("metapath").String() 280 conf.filectx = cfg.Section("").Key("filectx").String() 281 conf.metactx = cfg.Section("").Key("metactx").String() 282 conf.rootdir = cfg.Section("").Key("rootdir").String() 283 conf.chroot = cfg.Section("").Key("chroot").String() 284 conf.tmplpath = cfg.Section("").Key("tmplpath").String() 285 conf.maxsize, _ = cfg.Section("").Key("maxsize").Int64() 286 conf.expiry, _ = cfg.Section("").Key("expiry").Int64() 288 287 } 289 288 … … 292 291 } 293 292 294 if (conf.chroot != ""){293 if conf.chroot != "" { 295 294 if verbose { 296 295 log.Printf("Changing root to %s", conf.chroot)
Note:
See TracChangeset
for help on using the changeset viewer.