add readme.txt support
This commit is contained in:
parent
c0f0514512
commit
2943fd474a
21
handler.go
21
handler.go
@ -3,10 +3,12 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
@ -58,7 +60,6 @@ func toHTTPError(err error) (msg string, httpStatus int) {
|
||||
}
|
||||
|
||||
func serveFile(w http.ResponseWriter, r *http.Request, fs http.FileSystem, name string) {
|
||||
|
||||
f, err := fs.Open(name)
|
||||
if err != nil {
|
||||
msg, code := toHTTPError(err)
|
||||
@ -85,7 +86,7 @@ func serveFile(w http.ResponseWriter, r *http.Request, fs http.FileSystem, name
|
||||
|
||||
// Still a directory? (we didn't find an index.html file)
|
||||
if d.IsDir() {
|
||||
dirList(w, r, f, name)
|
||||
dirList(w, r, f, name, fs)
|
||||
return
|
||||
}
|
||||
|
||||
@ -94,7 +95,7 @@ func serveFile(w http.ResponseWriter, r *http.Request, fs http.FileSystem, name
|
||||
http.ServeContent(w, r, d.Name(), d.ModTime(), f)
|
||||
}
|
||||
|
||||
func dirList(w http.ResponseWriter, r *http.Request, f http.File, name string) {
|
||||
func dirList(w http.ResponseWriter, r *http.Request, f http.File, name string, fs http.FileSystem) {
|
||||
dirs, err := f.Readdir(-1)
|
||||
if err != nil {
|
||||
renderError(w, "Error reading directory", http.StatusInternalServerError)
|
||||
@ -119,6 +120,7 @@ func dirList(w http.ResponseWriter, r *http.Request, f http.File, name string) {
|
||||
Title: name,
|
||||
IsRoot: name == "/",
|
||||
Files: make([]*File, 0, len(dirs)),
|
||||
Readme: loadReadmeFile(name, fs),
|
||||
}
|
||||
|
||||
for _, d := range dirs {
|
||||
@ -150,6 +152,19 @@ func dirList(w http.ResponseWriter, r *http.Request, f http.File, name string) {
|
||||
|
||||
}
|
||||
|
||||
func loadReadmeFile(path string, fs http.FileSystem) string {
|
||||
f, err := fs.Open(filepath.Join(path, "readme.txt"))
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
defer f.Close()
|
||||
|
||||
data, err := ioutil.ReadAll(f)
|
||||
|
||||
return string(data)
|
||||
}
|
||||
|
||||
type fileHandler struct {
|
||||
root string
|
||||
}
|
||||
|
14
main.html
14
main.html
@ -50,6 +50,20 @@
|
||||
</tr>
|
||||
{{end}}
|
||||
</table>
|
||||
|
||||
{{if .Readme}}
|
||||
<div class="card">
|
||||
<div class="card-header">
|
||||
readme.txt
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<p class="card-text">{{.Readme}}</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<br>
|
||||
{{end}}
|
||||
|
||||
<div class="alert alert-primary" role="alert">
|
||||
<small>By dedal.qq (c) 2018</small>
|
||||
</div>
|
||||
|
Loading…
x
Reference in New Issue
Block a user