added ability to create new watch from url template
This commit is contained in:
parent
d39fff4d57
commit
e9dde77233
2 changed files with 46 additions and 17 deletions
55
main.go
55
main.go
|
@ -280,30 +280,51 @@ func (web *Web) watchCreatePost(c *gin.Context) {
|
|||
templateID = 0
|
||||
}
|
||||
|
||||
if templateID == 0 {
|
||||
if templateID == 0 { // empty new watch
|
||||
web.db.Create(&watch)
|
||||
c.Redirect(http.StatusSeeOther, fmt.Sprintf("/watch/edit/%d", watch.ID))
|
||||
return
|
||||
}
|
||||
|
||||
templateFiles, err := EMBED_FS.ReadDir("watchTemplates")
|
||||
if err != nil {
|
||||
log.Fatalln("Could not load templates from embed FS")
|
||||
}
|
||||
var jsn []byte
|
||||
if templateID == -1 { // watch from url template
|
||||
url := c.PostForm("url")
|
||||
if len(url) == 0 {
|
||||
c.AbortWithError(http.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
c.AbortWithError(http.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
c.AbortWithError(http.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
jsn = body
|
||||
} else {
|
||||
templateFiles, err := EMBED_FS.ReadDir("watchTemplates")
|
||||
if err != nil {
|
||||
log.Fatalln("Could not load templates from embed FS")
|
||||
}
|
||||
|
||||
if templateID >= len(templateFiles) {
|
||||
log.Println("/watch/create POSTed with", templateID, "but only", len(templateFiles), "templates")
|
||||
c.AbortWithError(http.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
if templateID >= len(templateFiles) {
|
||||
log.Println("/watch/create POSTed with", templateID, "but only", len(templateFiles), "templates")
|
||||
c.AbortWithError(http.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
|
||||
template := templateFiles[templateID-1] // -1 because of "None" option
|
||||
templatePath := fmt.Sprintf("watchTemplates/%s", template.Name())
|
||||
jsn, err := EMBED_FS.ReadFile(templatePath)
|
||||
if err != nil {
|
||||
log.Println("Could not read template from embed.FS:", err)
|
||||
c.AbortWithError(http.StatusBadRequest, err)
|
||||
return
|
||||
template := templateFiles[templateID-1] // -1 because of "None" option
|
||||
templatePath := fmt.Sprintf("watchTemplates/%s", template.Name())
|
||||
_jsn, err := EMBED_FS.ReadFile(templatePath)
|
||||
if err != nil {
|
||||
log.Println("Could not read template from embed.FS:", err)
|
||||
c.AbortWithError(http.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
jsn = _jsn
|
||||
}
|
||||
|
||||
export := WatchExport{}
|
||||
|
|
|
@ -35,6 +35,14 @@ GoWatch {{ .Watch.Name }}
|
|||
</label>
|
||||
</div>
|
||||
{{ end }}
|
||||
<div class="form-check d-flex my-2">
|
||||
<div>
|
||||
<input class="form-check-input" type="radio" name="template" value="-1">
|
||||
</div>
|
||||
<div class="form-check-label mx-2">
|
||||
<input type="text" class="form-control" name="url" size="15" placeholder="From URL">
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-2"> </div>
|
||||
</div>
|
||||
|
|
Loading…
Add table
Reference in a new issue