Пример #1
0
func listDirectory(name string, ref *block.Ref) error {
	files, err := dir.LoadDirectory(ref)
	if err != nil {
		return err
	}
	for _, f := range files {
		fullpath := filepath.Join(name, f.Name)
		fmt.Printf("%s  %s  %s  %s\n", f.Mode, f.ModTime.Local().Format("02 Jan 2006 15:04"),
			sizeString(f.Size), fullpath)
		if f.Mode.IsDir() {
			if err := listDirectory(fullpath, f.Ref); err != nil {
				return err
			}
		}
	}
	return nil
}
Пример #2
0
func dirHandler(w http.ResponseWriter, req *http.Request) {
	refName := path.Base(req.URL.Path)
	//TODO reject other paths.
	dirRef := block.RefFromHex([]byte(refName))
	if dirRef == nil {
		http.Error(w, fmt.Sprintf("Bad ref"), http.StatusBadRequest)
		return
	}
	files, err := dir.LoadDirectory(dirRef)
	if err != nil {
		http.Error(w, err.Error(), http.StatusNotFound)
		return
	}
	rows := make([]fileDesc, len(files))
	for i, f := range files {
		var r fileDesc
		r.IsDir = f.Mode.IsDir()
		r.Name = f.Name
		r.Mode = f.Mode.String()
		r.Time = f.ModTime.Local().Format("02 Jan 2006 15:04")
		r.Size = sizeString(f.Size)
		r.Ref = f.Ref.String()
		rows[i] = r
	}
	sort.Sort(fileDescSlice(rows))
	var b bytes.Buffer
	if err := dirTemplate.Execute(&b,
		&struct {
			Title  string
			DirRef *block.Ref
			Files  []fileDesc
		}{
			"Directory",
			dirRef,
			rows,
		}); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	b.WriteTo(w)
}