taggart

Simple golang tagging filesystem webapp
Log | Files | Refs

commit cfc16f8d454ccab3ddaeb02ca8db60ce6f2b6810
parent 79d02d2c714ae396aa780551bb8c50d9931ae0e9
Author: breadcat <breadcat@users.noreply.github.com>
Date:   Wed, 24 Sep 2025 16:14:47 +0100

Search bar now preserves search string

Diffstat:
Mmain.go | 78++++++++++++++++++++++++++++++++++++++++++------------------------------------
Mtemplates/_header.html | 2+-
Mtemplates/search.html | 12++++++------
3 files changed, 49 insertions(+), 43 deletions(-)

diff --git a/main.go b/main.go @@ -48,8 +48,10 @@ type TagDisplay struct { type PageData struct { Title string Data interface{} + Query string IP string Port string + Files []File } // sanitizeFilename removes problematic characters from filenames @@ -252,46 +254,50 @@ func main() { func searchHandler(w http.ResponseWriter, r *http.Request) { - query := strings.TrimSpace(r.URL.Query().Get("q")) - - var files []File - var searchTitle string - - if query != "" { - // Convert wildcards to SQL LIKE pattern - // * becomes % and ? becomes _ (standard SQL wildcards) - sqlPattern := strings.ReplaceAll(query, "*", "%") - sqlPattern = strings.ReplaceAll(sqlPattern, "?", "_") - - // Search for files matching the pattern - rows, err := db.Query("SELECT id, filename, path FROM files WHERE filename LIKE ? ORDER BY filename", sqlPattern) - if err != nil { - http.Error(w, "Search failed", http.StatusInternalServerError) - return - } - defer rows.Close() + query := strings.TrimSpace(r.URL.Query().Get("q")) + + var files []File + var searchTitle string + + if query != "" { + // Convert wildcards to SQL LIKE pattern + sqlPattern := strings.ReplaceAll(query, "*", "%") + sqlPattern = strings.ReplaceAll(sqlPattern, "?", "_") + + rows, err := db.Query( + "SELECT id, filename, path FROM files WHERE filename LIKE ? ORDER BY filename", + sqlPattern, + ) + if err != nil { + http.Error(w, "Search failed", http.StatusInternalServerError) + return + } + defer rows.Close() - for rows.Next() { - var f File - rows.Scan(&f.ID, &f.Filename, &f.Path) - files = append(files, f) - } + for rows.Next() { + var f File + if err := rows.Scan(&f.ID, &f.Filename, &f.Path); err != nil { + http.Error(w, "Failed to read results", http.StatusInternalServerError) + return + } + f.EscapedFilename = url.PathEscape(f.Filename) + files = append(files, f) + } - searchTitle = fmt.Sprintf("Search Results for: %s", query) - } else { - searchTitle = "Search Files" - } + searchTitle = fmt.Sprintf("Search Results for: %s", query) + } else { + searchTitle = "Search Files" + } - // Always initialize the data structure properly - pageData := PageData{ - Title: searchTitle, - Data: struct { - Files []File - Query string - }{files, query}, - } + pageData := PageData{ + Title: searchTitle, + Query: query, + Files: files, + } - tmpl.ExecuteTemplate(w, "search.html", pageData) + if err := tmpl.ExecuteTemplate(w, "search.html", pageData); err != nil { + http.Error(w, "Template rendering failed", http.StatusInternalServerError) + } } // Upload file from URL diff --git a/templates/_header.html b/templates/_header.html @@ -44,7 +44,7 @@ span#searchToggle {cursor: pointer; color: lightblue} </ul> <div id="search-container"> <form method="get" action="/search" style="margin-bottom: 30px;"> - <input type="text" name="q" value="" placeholder="Search..." style="width: 400px; padding: 8px; font-size: 16px;"> + <input type="text" name="q" value="{{.Query}}" placeholder="Search..." style="width: 400px; padding: 8px; font-size: 16px;"> <span id="searchToggle">?</span> </form> </div> diff --git a/templates/search.html b/templates/search.html @@ -1,10 +1,10 @@ {{template "_header" .}} <h1>Search Files</h1> -{{if .Data.Files}} - <h2>Found {{len .Data.Files}} file{{if ne (len .Data.Files) 1}}s{{end}}</h2> +{{if .Files}} + <h2>Found {{len .Files}} file{{if ne (len .Files) 1}}s{{end}}</h2> <div class="file-grid"> - {{range .Data.Files}} + {{range .Files}} <div class="file-item"> <a href="/file/{{.ID}}">{{.Filename}}</a> {{if hasAnySuffix .Filename ".jpg" ".jpeg" ".png" ".gif" ".webp"}} @@ -13,9 +13,9 @@ </div> {{end}} </div> -{{else if .Data.Query}} - <p>No files found matching "<strong>{{.Data.Query}}</strong>"</p> - <p>Try using wildcards like <code>*{{.Data.Query}}*</code> for broader results.</p> +{{else if .Query}} + <p>No files found matching "<strong>{{.Query}}</strong>"</p> + <p>Try using wildcards like <code>*{{.Query}}*</code> for broader results.</p> {{end}} {{template "_footer"}} \ No newline at end of file