func dirListHtml(w http.ResponseWriter, r *http.Request, f fsi.File) { w.Header().Set("Content-Type", "text/html; charset=utf-8") for { dirs, err := f.Readdir(100) if err != nil || len(dirs) == 0 { break } for _, d := range dirs { name := d.Name() suffix := "" if d.IsDir() { suffix = "/" } linktitle := htmlReplacer.Replace(name) linktitle = stringspb.Ellipsoider(linktitle, 40) if d.IsDir() { linktitle = common.Directorify(linktitle) } surl := path.Join(r.URL.Path, name) + suffix + "?fmt=html" oneLine := spf("<a style='display:inline-block;min-width:600px;' href=\"%s\">%s</a>", surl, linktitle) // wpf(w, " %v", d.ModTime().Format("2006-01-02 15:04:05 MST")) oneLine += spf(" %v<br>", d.ModTime().Format(time.RFC1123Z)) wpf(w, oneLine) } } }
func checkSize(t *testing.T, f fsi.File, size int64) { dir, err := f.Stat() if err != nil { t.Fatalf("Stat %q (looking for size %d): %s", f.Name(), size, err) } if dir.Size() != size { t.Errorf("Stat %q: size %d want %d", f.Name(), dir.Size(), size) } }
func dirListJson(w http.ResponseWriter, r *http.Request, f fsi.File) { r.Header.Set("Content-Type", "application/json") mp := []map[string]string{} for { dirs, err := f.Readdir(100) if err != nil || len(dirs) == 0 { break } for _, d := range dirs { name := d.Name() if d.IsDir() { name = common.Directorify(name) } name = htmlReplacer.Replace(name) url := url.URL{Path: name} mpl := map[string]string{ "path": url.String(), "mod": d.ModTime().Format(time.RFC1123Z), } mp = append(mp, mpl) } } bdirListHtml, err := json.MarshalIndent(mp, "", "\t") if err != nil { wpf(w, "marshalling to []byte failed - mp was %v", mp) return } w.Write(bdirListHtml) }
func (m *memMapFs) lookupUnderlyingFS( nameMemFS string, origName string, // orig name has no mountname prefix á la mnt02 ) (fsi.File, error) { // log.Printf("check shadowFS %q %q\n", nameMemFS, origName) if m.shadow == nil { // no underlying filesystem return nil, fsi.ErrFileNotFound } fshad, err := m.shadow.Open(origName) if err != nil { return nil, fsi.ErrFileNotFound } defer fshad.Close() // log.Printf("found in shadoFS %q %q\n", nameMemFS, origName) inf, err := fshad.Stat() if err != nil { return nil, fmt.Errorf("fileinfo from shadow failed: %v", err) } // // special case // resource is a directory if inf.IsDir() { // return nil, fmt.Errorf("is dir") err = m.MkdirAll(origName, 0755) if err != nil && err != fsi.ErrFileExists { return nil, err } m.rlock() dir, ok := m.fos[common.Directorify(path.Dir(nameMemFS))] m.runlock() if !ok { return nil, fmt.Errorf("dir created with MkDir, but not in fos map %q %q", nameMemFS, origName) } // Now we try to cache the index-file. // Yes. I tried to keep it simple! idx, err := m.shadow.Open(path.Join(origName, "index.html")) if err != fsi.ErrFileNotFound { return dir, nil } if err != nil { return dir, nil } defer idx.Close() fshad = idx } // // nameMemFS = origName var dst fsi.File // regular file err = m.MkdirAll(path.Dir(nameMemFS), 0755) if err != nil && err != fsi.ErrFileExists { return nil, err } log.Printf(" from underlying: created front dir %q \n", path.Dir(nameMemFS)) dst, err = m.Create(nameMemFS) if err != nil { return nil, err } log.Printf(" from underlying: created front file %q \n", nameMemFS) n, err := io.Copy(dst, fshad) _ = n if err != nil { return nil, err } // log.Printf("copied %v for %v\n", n, name) err = dst.Close() if err != nil { return nil, err } // // reopen ff, okConv := dst.(*InMemoryFile) if okConv { ff.Open() } else { return nil, fmt.Errorf("could not convert opened file into InMemoryFile 2") } return dst, nil }