From 6753a113ad426de6aa4d4f9a4aa4407bc177b5c6 Mon Sep 17 00:00:00 2001 From: Nazar Kanaev Date: Fri, 10 Jul 2020 16:55:25 +0100 Subject: [PATCH] opml file import/export handlers --- server/handlers.go | 28 ++++++++++++++++++++++++++++ server/server.go | 2 ++ template/index.html | 10 ++++++++++ template/static/javascripts/api.js | 6 ++++++ template/static/javascripts/app.js | 4 ++++ 5 files changed, 50 insertions(+) diff --git a/server/handlers.go b/server/handlers.go index 92c151d..a5b32ec 100644 --- a/server/handlers.go +++ b/server/handlers.go @@ -8,11 +8,13 @@ import ( "os" "log" "io" + "io/ioutil" "mime" "strings" "path/filepath" "strconv" "math" + "fmt" ) func IndexHandler(rw http.ResponseWriter, req *http.Request) { @@ -348,3 +350,29 @@ func SettingsHandler(rw http.ResponseWriter, req *http.Request) { } } } + +func OPMLImportHandler(rw http.ResponseWriter, req *http.Request) { + if req.Method == "POST" { + file, _, err := req.FormFile("opml") + if err != nil { + log.Print(err) + return + } + content, err := ioutil.ReadAll(file) + if err != nil { + log.Print(err) + return + } + fmt.Println(string(content)) + } else { + rw.WriteHeader(http.StatusMethodNotAllowed) + } +} + +func OPMLExportHandler(rw http.ResponseWriter, req *http.Request) { + if req.Method == "GET" { + rw.Header().Set("Content-Type", "application/xml; charset=utf-8") + rw.Header().Set("Content-Disposition", `attachment; filename="subscriptions.opml"`) + rw.Write([]byte("content")) + } +} diff --git a/server/server.go b/server/server.go index bc3a0fb..e5d3935 100644 --- a/server/server.go +++ b/server/server.go @@ -79,6 +79,8 @@ var routes []Route = []Route{ p("/api/items", ItemListHandler), p("/api/items/:id", ItemHandler), p("/api/settings", SettingsHandler), + p("/opml/import", OPMLImportHandler), + p("/opml/export", OPMLExportHandler), } func Vars(req *http.Request) map[string]string { diff --git a/template/index.html b/template/index.html index db086d5..6d4f012 100644 --- a/template/index.html +++ b/template/index.html @@ -228,6 +228,16 @@
+
+ +
+ + +
+
+ +
+ Download
diff --git a/template/static/javascripts/api.js b/template/static/javascripts/api.js index 07e6460..d3f556d 100644 --- a/template/static/javascripts/api.js +++ b/template/static/javascripts/api.js @@ -74,5 +74,11 @@ return api('put', '/api/settings', data) }, }, + upload_opml: function(form) { + return fetch('/opml/import', { + method: 'post', + body: new FormData(form), + }) + }, } })() diff --git a/template/static/javascripts/app.js b/template/static/javascripts/app.js index c50f19f..394ad23 100644 --- a/template/static/javascripts/app.js +++ b/template/static/javascripts/app.js @@ -240,5 +240,9 @@ var vm = new Vue({ } api.items.update(item.id, {status: item.status}) }, + importOPML: function(event) { + var form = document.querySelector('#opml-import-form') + api.upload_opml(form) + }, } })