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")) tmpl = template.Must(template.ParseFS(filesTemplate, "templates/files.html"))
} }
fileStat, _ := os.Stat("." + file) realPath := filepath.Join(app.filesDir, filepath.Base(file))
fileContent, _ := os.ReadFile("." + file)
fileStat, _ := os.Stat("./" + realPath)
fileContent, _ := os.ReadFile("./" + realPath)
fileInfo := FileInfo{ fileInfo := FileInfo{
Name: file, 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) { 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) http.Error(w, "Wrong url", http.StatusBadRequest)
return return
} }
if fileInfo, err := os.Stat(path); err == nil && !fileInfo.IsDir() { if fileInfo, err := os.Stat(realPath); err == nil && !fileInfo.IsDir() {
http.ServeFile(w, r, path) http.ServeFile(w, r, realPath)
return return
} }
} }
@ -106,16 +107,16 @@ func (app *Application) indexHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
name := filepath.Clean(r.URL.Path) name := filepath.Base(r.URL.Path)
path := filepath.Join(app.filesDir, name) realPath := filepath.Join(app.filesDir, name)
if !filepath.IsLocal(path) { if !filepath.IsLocal(realPath) {
http.Error(w, "Wrong url", http.StatusBadRequest) http.Error(w, "Wrong url", http.StatusBadRequest)
return return
} }
if fileInfo, err := os.Stat(path); err == nil && !fileInfo.IsDir() { if fileInfo, err := os.Stat(realPath); err == nil && !fileInfo.IsDir() {
DisplayFile(app, "/"+path, w) DisplayFile(app, filepath.Join("/raw", name), w)
return return
} }

View File

@ -133,6 +133,10 @@
padding: 10px; padding: 10px;
} }
header {
display: flex;
}
.content { .content {
flex-grow: 1; flex-grow: 1;
display: flex; display: flex;
@ -193,14 +197,31 @@
} }
a { a {
color: #0288d1; color: #d4d4d4;
text-decoration: none; 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> </style>
</head> </head>
<body> <body>
<header>{{.Path}}</header> <header>
<nav>
<a href="/">Home</a>
</nav>
<a href="{{.Name}}" class="path">{{.Path}}</a>
</header>
<div class="content"> <div class="content">
{{if eq .Type "text"}} {{if eq .Type "text"}}
<pre>{{.Content}}</pre> <pre>{{.Content}}</pre>