Changeset 20 in code for trunk/partage.go


Ignore:
Timestamp:
Oct 18, 2021, 5:59:02 PM (4 years ago)
Author:
dev
Message:

Write metadata about the file along with it

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/partage.go

    r19 r20  
    88        "net/http"
    99        "os"
     10        "time"
    1011        "path"
    1112        "path/filepath"
    1213        "html/template"
     14        "encoding/json"
    1315
    1416        "github.com/dustin/go-humanize"
     
    2123}
    2224
     25type metadata struct {
     26        Filename string
     27        Size int64
     28        Expiry int64
     29}
     30
    2331var conf struct {
    2432        bind     string
    2533        baseuri  string
    2634        filepath string
     35        metapath string
    2736        rootdir  string
    2837        templatedir string
    2938        filectx  string
     39        metactx  string
    3040        maxsize  int64
     41        expiry   int64
    3142}
    3243
     
    7788}
    7889
     90func 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
    79119func servetemplate(w http.ResponseWriter, f string, d templatedata) {
    80120        t, err := template.ParseFiles(conf.templatedir + "/" + f)
     
    110150                return
    111151        }
     152        writemeta(tmp.Name(), conf.expiry)
    112153
    113154        resp := conf.baseuri + conf.filectx + filepath.Base(tmp.Name())
     
    148189                }
    149190
    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"
    151196                links = append(links, link)
    152197        }
     
    189234        flag.StringVar(&conf.bind,        "l", "0.0.0.0:8080", "Address to bind to (default: 0.0.0.0:8080)")
    190235        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)")
    192238        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/)")
    193240        flag.StringVar(&conf.rootdir,     "r", "./static", "Root directory (default: ./static)")
    194241        flag.StringVar(&conf.templatedir, "t", "./templates", "Templates directory (default: ./templates)")
    195242        flag.Int64Var(&conf.maxsize,      "s", 30064771072, "Maximum file size (default: 28Gib)")
     243        flag.Int64Var(&conf.expiry,       "e", 86400, "Link expiration time (default: 24h)")
    196244
    197245        flag.Parse()
     
    199247        http.HandleFunc("/", uploader)
    200248        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))))
    201250        http.ListenAndServe("0.0.0.0:8080", nil)
    202251}
Note: See TracChangeset for help on using the changeset viewer.