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
|
templateID = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
if templateID == 0 {
|
if templateID == 0 { // empty new watch
|
||||||
web.db.Create(&watch)
|
web.db.Create(&watch)
|
||||||
c.Redirect(http.StatusSeeOther, fmt.Sprintf("/watch/edit/%d", watch.ID))
|
c.Redirect(http.StatusSeeOther, fmt.Sprintf("/watch/edit/%d", watch.ID))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
templateFiles, err := EMBED_FS.ReadDir("watchTemplates")
|
var jsn []byte
|
||||||
if err != nil {
|
if templateID == -1 { // watch from url template
|
||||||
log.Fatalln("Could not load templates from embed FS")
|
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) {
|
if templateID >= len(templateFiles) {
|
||||||
log.Println("/watch/create POSTed with", templateID, "but only", len(templateFiles), "templates")
|
log.Println("/watch/create POSTed with", templateID, "but only", len(templateFiles), "templates")
|
||||||
c.AbortWithError(http.StatusBadRequest, err)
|
c.AbortWithError(http.StatusBadRequest, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
template := templateFiles[templateID-1] // -1 because of "None" option
|
template := templateFiles[templateID-1] // -1 because of "None" option
|
||||||
templatePath := fmt.Sprintf("watchTemplates/%s", template.Name())
|
templatePath := fmt.Sprintf("watchTemplates/%s", template.Name())
|
||||||
jsn, err := EMBED_FS.ReadFile(templatePath)
|
_jsn, err := EMBED_FS.ReadFile(templatePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Could not read template from embed.FS:", err)
|
log.Println("Could not read template from embed.FS:", err)
|
||||||
c.AbortWithError(http.StatusBadRequest, err)
|
c.AbortWithError(http.StatusBadRequest, err)
|
||||||
return
|
return
|
||||||
|
}
|
||||||
|
jsn = _jsn
|
||||||
}
|
}
|
||||||
|
|
||||||
export := WatchExport{}
|
export := WatchExport{}
|
||||||
|
|
|
@ -35,6 +35,14 @@ GoWatch {{ .Watch.Name }}
|
||||||
</label>
|
</label>
|
||||||
</div>
|
</div>
|
||||||
{{ end }}
|
{{ 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>
|
||||||
<div class="col-2"> </div>
|
<div class="col-2"> </div>
|
||||||
</div>
|
</div>
|
||||||
|
|
Loading…
Add table
Reference in a new issue