Fix button for uploading OPML

This commit is contained in:
Joakim Hellsén 2024-02-05 06:00:17 +01:00
commit 4cab55c211
6 changed files with 277 additions and 14 deletions

View file

@ -1,6 +1,7 @@
package main
import (
"io"
"log"
"net/http"
"strings"
@ -74,7 +75,7 @@ func IndexHandler(w http.ResponseWriter, _ *http.Request) {
CanonicalURL: "http://localhost:8000/",
Content: content,
}
html := fullHTML(htmlData, nil)
html := fullHTML(htmlData)
w.Write([]byte(html))
}
@ -87,7 +88,7 @@ func ApiHandler(w http.ResponseWriter, _ *http.Request) {
CanonicalURL: "http://localhost:8000/api",
Content: "<p>Here be dragons.</p>",
}
html := fullHTML(htmlData, nil)
html := fullHTML(htmlData)
w.Write([]byte(html))
}
func FeedsHandler(w http.ResponseWriter, _ *http.Request) {
@ -99,7 +100,7 @@ func FeedsHandler(w http.ResponseWriter, _ *http.Request) {
CanonicalURL: "http://localhost:8000/feeds",
Content: "<p>Here be feeds.</p>",
}
html := fullHTML(htmlData, nil)
html := fullHTML(htmlData)
w.Write([]byte(html))
}
@ -126,17 +127,73 @@ func AddFeedHandler(w http.ResponseWriter, r *http.Request) {
// "Add" the feed to the database
log.Println("Adding feed:", feed_url)
}
parseErrors = append(parseErrors, ParseResult{FeedURL: feed_url, Msg: "Added", IsError: false})
}
htmlData := HTMLData{
Title: "FeedVault - Add Feeds",
Description: "FeedVault - Add Feeds",
Title: "FeedVault",
Description: "FeedVault - A feed archive",
Keywords: "RSS, Atom, Feed, Archive",
Author: "TheLovinator",
CanonicalURL: "http://localhost:8000/add",
CanonicalURL: "http://localhost:8000/",
Content: "<p>Feeds added.</p>",
ParseResult: parseErrors,
}
html := fullHTML(htmlData, parseErrors)
html := fullHTML(htmlData)
w.Write([]byte(html))
}
func UploadOpmlHandler(w http.ResponseWriter, r *http.Request) {
// Parse the form and get the file
r.ParseMultipartForm(10 << 20) // 10 MB
file, _, err := r.FormFile("file")
if err != nil {
http.Error(w, "No file provided", http.StatusBadRequest)
return
}
defer file.Close()
// Read the file
all, err := io.ReadAll(file)
if err != nil {
http.Error(w, "Failed to read file", http.StatusInternalServerError)
return
}
// Convert the file to a string
opml := string(all)
// Parse the OPML file
parseResult := []ParseResult{}
links, err := ParseOpml(opml)
if err != nil {
parseResult = append(parseResult, ParseResult{FeedURL: "/upload_opml", Msg: err.Error(), IsError: true})
} else {
// Add the feeds to the database
for _, feed_url := range links.XMLLinks {
log.Println("Adding feed:", feed_url)
// Validate the URL
err := validateURL(feed_url)
if err != nil {
parseResult = append(parseResult, ParseResult{FeedURL: feed_url, Msg: err.Error(), IsError: true})
continue
}
parseResult = append(parseResult, ParseResult{FeedURL: feed_url, Msg: "Added", IsError: false})
}
}
// Return the results
htmlData := HTMLData{
Title: "FeedVault",
Description: "FeedVault - A feed archive",
Keywords: "RSS, Atom, Feed, Archive",
Author: "TheLovinator",
CanonicalURL: "http://localhost:8000/",
Content: "<p>Feeds added.</p>",
ParseResult: parseResult,
}
html := fullHTML(htmlData)
w.Write([]byte(html))
}