本文整理汇总了Golang中net/http.File.Readdir方法的典型用法代码示例。如果您正苦于以下问题:Golang File.Readdir方法的具体用法?Golang File.Readdir怎么用?Golang File.Readdir使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类net/http.File
的用法示例。
在下文中一共展示了File.Readdir方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: dirList
func dirList(w http.ResponseWriter, f http.File, name string) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
fmt.Fprintf(w, "<pre>\n")
switch name {
case "/":
fmt.Fprintf(w, "<a href=\"%s\">%s</a>\n", "/", ".")
default:
fmt.Fprintf(w, "<a href=\"%s\">%s</a>\n", path.Clean(name+"/.."), "..")
}
for {
dirs, err := f.Readdir(100)
if err != nil || len(dirs) == 0 {
break
}
sort.Sort(byName(dirs))
for _, d := range dirs {
name := d.Name()
if d.IsDir() {
name += "/"
}
// name may contain '?' or '#', which must be escaped to remain
// part of the URL path, and not indicate the start of a query
// string or fragment.
url := url.URL{Path: name}
fmt.Fprintf(w, "<a href=\"%s\">%s</a>\n", url.String(), html.EscapeString(name))
}
}
fmt.Fprintf(w, "</pre>\n")
}
示例2: dirList
func dirList(ci inject.CopyInject, logger termlog.Logger, w http.ResponseWriter, name string, f http.File, templates *template.Template) {
w.Header().Set("Cache-Control", "no-store, must-revalidate")
files, err := f.Readdir(0)
if err != nil {
logger.Shout("Error reading directory for listing: %s", err)
return
}
data := dirData{Name: name, Files: files}
buff := bytes.NewBuffer(make([]byte, 0, 0))
err = templates.Lookup("dirlist.html").Execute(buff, data)
length := buff.Len()
if err != nil {
logger.Shout("Error producing directory listing: %s", err)
}
inj, err := ci.Sniff(buff)
if err != nil {
logger.Shout("Failed to inject in dir listing: %s", err)
return
}
w.Header().Set(
"Content-Length", fmt.Sprintf("%d", length+inj.Extra()),
)
_, err = inj.Copy(w)
if err != nil {
logger.Shout("Failed to inject in dir listing: %s", err)
return
}
}
示例3: dirList
func dirList(w http.ResponseWriter, f http.File) {
dirs, err := f.Readdir(-1)
if err != nil {
// TODO: log err.Error() to the Server.ErrorLog, once it's possible
// for a handler to get at its Server via the ResponseWriter. See
// Issue 12438.
http.Error(w, "Error reading directory", http.StatusInternalServerError)
return
}
sort.Sort(byName(dirs))
w.Header().Set("Content-Type", "text/html; charset=utf-8")
fmt.Fprintf(w, "<pre>\n")
for _, d := range dirs {
name := d.Name()
if d.IsDir() {
name += "/"
}
// name may contain '?' or '#', which must be escaped to remain
// part of the URL path, and not indicate the start of a query
// string or fragment.
url := url.URL{Path: name}
fmt.Fprintf(w, "<a href=\"%s\">%s</a>\n", url.String(), htmlReplacer.Replace(name))
}
fmt.Fprintf(w, "</pre>\n")
}
示例4: readAllFiles
func readAllFiles(file http.File) ([]os.FileInfo, error) {
files := []os.FileInfo{}
for {
more, err := file.Readdir(100)
if err == io.EOF {
return files, nil
} else if err != nil {
return nil, err
}
files = append(files, more...)
}
}
示例5: dirList
func dirList(ci inject.CopyInject, logger termlog.Logger, w http.ResponseWriter, name string, f http.File, templates *template.Template) {
w.Header().Set("Cache-Control", "no-store, must-revalidate")
files, err := f.Readdir(0)
if err != nil {
logger.Shout("Error reading directory for listing: %s", err)
return
}
data := dirData{Name: name, Files: files}
err = ci.ServeTemplate(http.StatusOK, w, templates.Lookup("dirlist.html"), data)
if err != nil {
logger.Shout("Failed to generate dir listing: %s", err)
}
}
示例6: dirList
func dirList(w http.ResponseWriter, f http.File, fs Filesystemhttp, name string, prefix string, auth bool) {
//w.Header().Set("Content-Type", "text/html; charset=utf-8")
//fmt.Fprintf(w, "<pre>\n")
//finfo, _ := f.Stat()
out := make(map[string]interface{})
out["prefix"] = prefix
out["path"] = name
out["auth"] = auth
authRead := auth || (!auth && !fs.ReadNeedsAuth)
authList := auth || (!auth && !fs.ListNeedsAuth)
authZip := auth || (!auth && !fs.ZipNeedsAuth)
authDelete := auth || (!auth && !fs.DeleteNeedsAuth)
authUpload := auth || (!auth && !fs.UploadNeedsAuth)
authCreateFolder := auth || (!auth && !fs.CreateFolderNeedsAuth)
out["authRead"] = authRead
out["authList"] = authList
out["authZip"] = authZip
out["authDelete"] = authDelete
out["authUpload"] = authUpload
out["authCreateFolder"] = authCreateFolder
if authList {
files := make([]os.FileInfo, 0, 50)
for {
dirs, err := f.Readdir(100)
if err != nil || len(dirs) == 0 {
break
}
for _, d := range dirs {
name := d.Name()
if d.IsDir() {
name += "/"
}
files = append(files, d)
}
}
out["files"] = files
}
Template(TemplateFiles).Execute(w, out)
//fmt.Fprintf(w, "</pre>\n")
}
示例7: dirList
func dirList(w http.ResponseWriter, f http.File) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
fmt.Fprintf(w, "<pre>\n")
for {
dirs, err := f.Readdir(100)
if err != nil || len(dirs) == 0 {
break
}
for _, d := range dirs {
name := d.Name()
if d.IsDir() {
name += "/"
}
escaped := html.EscapeString(name)
fmt.Fprintf(w, "<a href=\"%s\">%s</a>\n", escaped, escaped)
}
}
fmt.Fprintf(w, "</pre>\n")
}
示例8: loadDirectoryContents
func (b Browse) loadDirectoryContents(requestedFilepath http.File, urlPath string) (*Listing, bool, error) {
files, err := requestedFilepath.Readdir(-1)
if err != nil {
return nil, false, err
}
// Determine if user can browse up another folder
var canGoUp bool
curPathDir := path.Dir(strings.TrimSuffix(urlPath, "/"))
for _, other := range b.Configs {
if strings.HasPrefix(curPathDir, other.PathScope) {
canGoUp = true
break
}
}
// Assemble listing of directory contents
listing, hasIndex := directoryListing(files, canGoUp, urlPath)
return &listing, hasIndex, nil
}
示例9: listDir
func listDir(d http.File, c *Context) (err error) {
dirs, err := d.Readdir(-1)
if err != nil {
return err
}
// Create directory index
w := c.Response()
w.Header().Set(ContentType, TextHTMLCharsetUTF8)
fmt.Fprintf(w, "<pre>\n")
for _, d := range dirs {
name := d.Name()
color := "#212121"
if d.IsDir() {
color = "#e91e63"
name += "/"
}
fmt.Fprintf(w, "<a href=\"%s\" style=\"color: %s;\">%s</a>\n", name, color, name)
}
fmt.Fprintf(w, "</pre>\n")
return
}
示例10: dirList
func dirList(w http.ResponseWriter, f http.File) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
fmt.Fprint(w, "<style>table{border-spacing: 0;}th{text-align:left;background-color:rgba(0,0,0,.05);}th,td{padding:0.5em;border-bottom: 1px solid #cbcbcb;}tr:nth-child(odd){background-color:#f2f2f2;}</style>")
fmt.Fprintf(w, "<table style='width:100%%;'>\n<tr><th>File</th><th>Size</th><th>Date</th></tr>\n")
for {
dirs, err := f.Readdir(100)
if err != nil || len(dirs) == 0 {
break
}
for _, d := range dirs {
name := d.Name()
if d.IsDir() {
name += "/"
}
size := ByteSize(d.Size())
date := d.ModTime().Format("2006-01-02 15:04:05")
// TODO htmlescape
fmt.Fprintf(w, "<tr><td style='width:60%%;'><a href=\"%s\">%s</a></td><td style='width:20%%;'>%s</td><td>%s</td></tr>\n", name, name, size.String(), date)
}
}
fmt.Fprintf(w, "</table>\n")
}
示例11: dirList
func dirList(w http.ResponseWriter, f http.File) error {
dirs, err := f.Readdir(-1)
if err != nil {
return err
}
sort.Sort(byName(dirs))
w.Header().Set("Content-Type", "text/html; charset=utf-8")
fmt.Fprintf(w, "<pre>\n")
for _, d := range dirs {
name := d.Name()
if d.IsDir() {
name += "/"
}
// name may contain '?' or '#', which must be escaped to remain
// part of the URL path, and not indicate the start of a query
// string or fragment.
url := url.URL{Path: name}
fmt.Fprintf(w, "<a href=\"%s\">%s</a>\n", url.String(), replacer.Replace(name))
}
fmt.Fprintf(w, "</pre>\n")
return nil
}
示例12: dirList
func (fserver *FileServer) dirList(logger termlog.Logger, w http.ResponseWriter, name string, f http.File) {
w.Header().Set("Cache-Control", "no-store, must-revalidate")
files, err := f.Readdir(0)
if err != nil {
logger.Shout("Error reading directory for listing: %s", err)
return
}
sortedFiles := fileSlice(files)
sort.Sort(sortedFiles)
data := dirData{
Version: fserver.Version,
Name: name,
Files: sortedFiles,
}
err = fserver.Inject.ServeTemplate(
http.StatusOK,
w,
fserver.Templates.Lookup("dirlist.html"),
data,
)
if err != nil {
logger.Shout("Failed to generate dir listing: %s", err)
}
}
示例13: dirList
func dirList(reply *Reply, f http.File) {
reply.SetHeader("Content-Type", "text/html; charset=utf-8")
buf := bytes.NewBuffer(nil)
buf.WriteString("<pre>\n")
for {
dirs, err := f.Readdir(100)
if err != nil || len(dirs) == 0 {
break
}
for _, d := range dirs {
name := d.Name()
if d.IsDir() {
name += "/"
}
// name may contain '?' or '#', which must be escaped to remain
// part of the URL path, and not indicate the start of a query
// string or fragment.
url := url.URL{Path: name}
fmt.Fprintf(buf, "<a href=\"%s\">%s</a>\n", url.String(), htmlReplacer.Replace(name))
}
}
fmt.Fprintf(buf, "</pre>\n")
reply.With(buf)
}
示例14: readFilesRecursive
func (f *Files) readFilesRecursive(dirname string, file http.File, results map[string][]byte, recursive bool) error {
files, err := file.Readdir(-1)
if err != nil {
return err
}
var fpath string
for _, fi := range files {
fpath = dirname + fi.Name()
newFile, err := f.dir.Open(fpath)
if err != nil {
return err
}
if fi.IsDir() {
if !recursive {
continue
}
err := f.readFilesRecursive(fpath+pathSep, newFile, results, recursive)
if err != nil {
return err
}
continue
}
if fi.Mode()&os.ModeSymlink == os.ModeSymlink {
link, err := filepath.EvalSymlinks(fpath)
if err != nil {
log.Panic("Error Resolving Symlink", err)
}
fi, err := os.Stat(link)
if err != nil {
log.Panic(err)
}
if fi.IsDir() {
if !recursive {
continue
}
err := f.readFilesRecursive(fpath+pathSep, newFile, results, recursive)
if err != nil {
return err
}
continue
}
}
if !f.dir.useStaticFiles {
fpath = strings.Replace(fpath, f.dir.absPkgPath, "", 1)
}
results[fpath], err = ioutil.ReadAll(newFile)
if err != nil {
return err
}
}
return nil
}
示例15: dirList
// dirList writes the directory list to the HTTP response.
//
// glog convention is that log files are created in the following format:
// "ingest.skia-testing-b.perf.log.ERROR.20141015-133007.3273"
// where the first word is the name of the app.
// glog also creates symlinks that look like "ingest.ERROR". These
// symlinks point to the latest log type.
// This method displays sorted symlinks first and then displays sorted sections for
// all apps. Files and directories not in the glog format are bucketed into an
// "unknown" app.
func dirList(w http.ResponseWriter, f http.File) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
fmt.Fprintf(w, "<pre>\n")
// Datastructures to populate and output.
topLevelSymlinks := make([]os.FileInfo, 0)
topLevelSymlinkMaxFileName := 0
appToLogs := make(map[string][]os.FileInfo)
appToMaxFileName := make(map[string]int)
for {
fileInfos, err := f.Readdir(10000)
if err != nil || len(fileInfos) == 0 {
break
}
// Prepopulate the datastructures.
for _, fileInfo := range fileInfos {
name := fileInfo.Name()
nameTokens := strings.Split(name, ".")
if len(nameTokens) == 2 {
topLevelSymlinks = append(topLevelSymlinks, fileInfo)
if len(fileInfo.Name()) > topLevelSymlinkMaxFileName {
topLevelSymlinkMaxFileName = len(fileInfo.Name())
}
} else if len(nameTokens) > 1 {
appToLogs[nameTokens[0]] = append(appToLogs[nameTokens[0]], fileInfo)
if len(fileInfo.Name()) > appToMaxFileName[nameTokens[0]] {
appToMaxFileName[nameTokens[0]] = len(fileInfo.Name())
}
} else {
// File all directories or files created by something other than
// glog under "unknown" app.
appToLogs["unknown"] = append(appToLogs["unknown"], fileInfo)
}
}
}
// First output the top level symlinks.
sort.Sort(FileInfoModifiedSlice{fileInfos: topLevelSymlinks, reverseSort: true})
for _, fileInfo := range topLevelSymlinks {
writeFileInfo(w, fileInfo, topLevelSymlinkMaxFileName)
}
// Second output app links to their anchors.
var keys []string
for k := range appToLogs {
keys = append(keys, k)
}
sort.Strings(keys)
if len(keys) != 0 {
fmt.Fprint(w, "\nJump to sections:\n")
}
for _, app := range keys {
fmt.Fprintf(w, "<a href=\"#%s\">%s</a>\n", app, template.HTMLEscapeString(app))
}
fmt.Fprint(w, "\n")
// Then output the logs of all the different apps.
for _, app := range keys {
appFileInfos := appToLogs[app]
sort.Sort(FileInfoModifiedSlice{fileInfos: appFileInfos, reverseSort: true})
fmt.Fprintf(w, "\n===== <a name=\"%s\">%s</a> =====\n\n", app, template.HTMLEscapeString(app))
for _, fileInfo := range appFileInfos {
writeFileInfo(w, fileInfo, appToMaxFileName[app])
}
}
fmt.Fprintf(w, "</pre>\n")
}