add readme.txt support

This commit is contained in:
Timofey.Kovalev 2020-01-14 15:16:03 +03:00
parent c0f0514512
commit 2943fd474a
3 changed files with 33 additions and 3 deletions

View File

@ -3,10 +3,12 @@ package main
import ( import (
"fmt" "fmt"
"html/template" "html/template"
"io/ioutil"
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
"path" "path"
"path/filepath"
"sort" "sort"
"strings" "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) { func serveFile(w http.ResponseWriter, r *http.Request, fs http.FileSystem, name string) {
f, err := fs.Open(name) f, err := fs.Open(name)
if err != nil { if err != nil {
msg, code := toHTTPError(err) 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) // Still a directory? (we didn't find an index.html file)
if d.IsDir() { if d.IsDir() {
dirList(w, r, f, name) dirList(w, r, f, name, fs)
return 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) 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) dirs, err := f.Readdir(-1)
if err != nil { if err != nil {
renderError(w, "Error reading directory", http.StatusInternalServerError) 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, Title: name,
IsRoot: name == "/", IsRoot: name == "/",
Files: make([]*File, 0, len(dirs)), Files: make([]*File, 0, len(dirs)),
Readme: loadReadmeFile(name, fs),
} }
for _, d := range dirs { 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 { type fileHandler struct {
root string root string
} }

View File

@ -50,6 +50,20 @@
</tr> </tr>
{{end}} {{end}}
</table> </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"> <div class="alert alert-primary" role="alert">
<small>By dedal.qq (c) 2018</small> <small>By dedal.qq (c) 2018</small>
</div> </div>

View File

@ -14,4 +14,5 @@ type Data struct {
Title string Title string
IsRoot bool IsRoot bool
Files []*File Files []*File
Readme string
} }