fix: /raw api endpoint and different fileDir

This commit is contained in:
jabuxas 2025-02-10 12:09:22 -03:00
parent 99ff8180f8
commit e7dac3d397
3 changed files with 37 additions and 13 deletions

View File

@ -47,8 +47,10 @@ func DisplayFile(app *Application, file string, w http.ResponseWriter) {
tmpl = template.Must(template.ParseFS(filesTemplate, "templates/files.html"))
}
fileStat, _ := os.Stat("." + file)
fileContent, _ := os.ReadFile("." + file)
realPath := filepath.Join(app.filesDir, filepath.Base(file))
fileStat, _ := os.Stat("./" + realPath)
fileContent, _ := os.ReadFile("./" + realPath)
fileInfo := FileInfo{
Name: file,

View File

@ -81,15 +81,16 @@ func (app *Application) fileListingHandler(w http.ResponseWriter, r *http.Reques
}
func (app *Application) fileHandler(w http.ResponseWriter, r *http.Request) {
path := fmt.Sprintf(".%s", filepath.Clean(r.URL.Path))
path := fmt.Sprintf("%s", filepath.Base(r.URL.Path))
realPath := filepath.Join(app.filesDir, path)
if !filepath.IsLocal(path) {
if !filepath.IsLocal(realPath) {
http.Error(w, "Wrong url", http.StatusBadRequest)
return
}
if fileInfo, err := os.Stat(path); err == nil && !fileInfo.IsDir() {
http.ServeFile(w, r, path)
if fileInfo, err := os.Stat(realPath); err == nil && !fileInfo.IsDir() {
http.ServeFile(w, r, realPath)
return
}
}
@ -106,16 +107,16 @@ func (app *Application) indexHandler(w http.ResponseWriter, r *http.Request) {
return
}
name := filepath.Clean(r.URL.Path)
path := filepath.Join(app.filesDir, name)
name := filepath.Base(r.URL.Path)
realPath := filepath.Join(app.filesDir, name)
if !filepath.IsLocal(path) {
if !filepath.IsLocal(realPath) {
http.Error(w, "Wrong url", http.StatusBadRequest)
return
}
if fileInfo, err := os.Stat(path); err == nil && !fileInfo.IsDir() {
DisplayFile(app, "/"+path, w)
if fileInfo, err := os.Stat(realPath); err == nil && !fileInfo.IsDir() {
DisplayFile(app, filepath.Join("/raw", name), w)
return
}

View File

@ -133,6 +133,10 @@
padding: 10px;
}
header {
display: flex;
}
.content {
flex-grow: 1;
display: flex;
@ -193,14 +197,31 @@
}
a {
color: #0288d1;
color: #d4d4d4;
text-decoration: none;
filter: brightness(0.7);
transition: filter 0.2s;
}
a:hover {
filter: brightness(1)
}
.path {
position: relative;
margin: 0 auto;
left: -1.85%;
}
</style>
</head>
<body>
<header>{{.Path}}</header>
<header>
<nav>
<a href="/">Home</a>
</nav>
<a href="{{.Name}}" class="path">{{.Path}}</a>
</header>
<div class="content">
{{if eq .Type "text"}}
<pre>{{.Content}}</pre>