52 lines
1.2 KiB
Go
52 lines
1.2 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"time"
|
|
)
|
|
|
|
const fileElement = "<p><a href='https://darkstorm.tech%v'>%v</a><div style='float:right;'>%v</div></p>"
|
|
|
|
func filesRequest(w http.ResponseWriter, r *http.Request) {
|
|
partPath := filepath.Clean(r.URL.Path)
|
|
path := filepath.Join(*webRoot, partPath)
|
|
var pageContent string
|
|
fil, err := os.Open(path)
|
|
if err != nil {
|
|
if os.IsNotExist(err) {
|
|
pageContent = "<p>404 Not Found</p>"
|
|
w.WriteHeader(http.StatusNotFound)
|
|
} else {
|
|
pageContent = "<p>Server error!</p>"
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
log.Println("error serving files:", err)
|
|
}
|
|
} else {
|
|
stat, _ := fil.Stat()
|
|
if stat.IsDir() {
|
|
var dirs []os.DirEntry
|
|
dirs, err = fil.ReadDir(-1)
|
|
if err != nil {
|
|
pageContent = "<p>Server error!</p>"
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
log.Println("error serving files:", err)
|
|
}
|
|
for _, f := range dirs {
|
|
if f.IsDir() {
|
|
continue
|
|
}
|
|
inf, _ := f.Info()
|
|
pageContent += fmt.Sprintf(fileElement, filepath.Join(partPath, f.Name()), f.Name(), inf.ModTime().Format(time.DateOnly))
|
|
}
|
|
} else {
|
|
http.ServeFile(w, r, path)
|
|
return
|
|
}
|
|
}
|
|
sendContent(w, r, pageContent, "Files", "")
|
|
}
|