- Timestamp:
- Oct 18, 2021, 5:59:02 PM (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/partage.go
r19 r20 8 8 "net/http" 9 9 "os" 10 "time" 10 11 "path" 11 12 "path/filepath" 12 13 "html/template" 14 "encoding/json" 13 15 14 16 "github.com/dustin/go-humanize" … … 21 23 } 22 24 25 type metadata struct { 26 Filename string 27 Size int64 28 Expiry int64 29 } 30 23 31 var conf struct { 24 32 bind string 25 33 baseuri string 26 34 filepath string 35 metapath string 27 36 rootdir string 28 37 templatedir string 29 38 filectx string 39 metactx string 30 40 maxsize int64 41 expiry int64 31 42 } 32 43 … … 77 88 } 78 89 90 func writemeta(filename string, expiry int64) error { 91 92 f, _ := os.Open(filename) 93 stat, _ := f.Stat() 94 size := stat.Size() 95 f.Close() 96 97 meta := metadata{ 98 Filename: filepath.Base(filename), 99 Size: size, 100 Expiry: time.Now().Unix() + expiry, 101 } 102 103 f, err := os.Create(conf.metapath + "/" + meta.Filename + ".json") 104 if err != nil { 105 return err 106 } 107 defer f.Close() 108 109 j, err := json.Marshal(meta) 110 if err != nil { 111 return err 112 } 113 114 _, err = f.Write(j) 115 116 return err 117 } 118 79 119 func servetemplate(w http.ResponseWriter, f string, d templatedata) { 80 120 t, err := template.ParseFiles(conf.templatedir + "/" + f) … … 110 150 return 111 151 } 152 writemeta(tmp.Name(), conf.expiry) 112 153 113 154 resp := conf.baseuri + conf.filectx + filepath.Base(tmp.Name()) … … 148 189 } 149 190 150 link := conf.baseuri + conf.filectx + filepath.Base(tmp.Name()) 191 writemeta(tmp.Name(), conf.expiry) 192 193 194 //link := conf.baseuri + conf.filectx + filepath.Base(tmp.Name()) 195 link := conf.baseuri + conf.metactx + filepath.Base(tmp.Name()) + ".json" 151 196 links = append(links, link) 152 197 } … … 189 234 flag.StringVar(&conf.bind, "l", "0.0.0.0:8080", "Address to bind to (default: 0.0.0.0:8080)") 190 235 flag.StringVar(&conf.baseuri, "b", "http://127.0.0.1:8080", "Base URI to use for links (default: http://127.0.0.1:8080)") 191 flag.StringVar(&conf.filepath, "f", "/tmp", "Path to save files to (default: /tmp)") 236 flag.StringVar(&conf.filepath, "f", "./files", "Path to save files to (default: ./files)") 237 flag.StringVar(&conf.metapath, "m", "./meta", "Path to save metadata to (default: ./meta)") 192 238 flag.StringVar(&conf.filectx, "c", "/f/", "Context to serve files from (default: /f/)") 239 flag.StringVar(&conf.metactx, "d", "/m/", "Context to serve metadata from (default: /m/)") 193 240 flag.StringVar(&conf.rootdir, "r", "./static", "Root directory (default: ./static)") 194 241 flag.StringVar(&conf.templatedir, "t", "./templates", "Templates directory (default: ./templates)") 195 242 flag.Int64Var(&conf.maxsize, "s", 30064771072, "Maximum file size (default: 28Gib)") 243 flag.Int64Var(&conf.expiry, "e", 86400, "Link expiration time (default: 24h)") 196 244 197 245 flag.Parse() … … 199 247 http.HandleFunc("/", uploader) 200 248 http.Handle(conf.filectx, http.StripPrefix(conf.filectx, http.FileServer(http.Dir(conf.filepath)))) 249 http.Handle(conf.metactx, http.StripPrefix(conf.metactx, http.FileServer(http.Dir(conf.metapath)))) 201 250 http.ListenAndServe("0.0.0.0:8080", nil) 202 251 }
Note:
See TracChangeset
for help on using the changeset viewer.