PrivateGER
3 years ago
9 changed files with 207 additions and 140 deletions
@ -0,0 +1,43 @@ |
|||||
|
package PageHandlers |
||||
|
|
||||
|
import ( |
||||
|
_ "embed" |
||||
|
"fmt" |
||||
|
"html/template" |
||||
|
"net/http" |
||||
|
"strconv" |
||||
|
"ytdlp-viewer/DirectoryIndexers" |
||||
|
) |
||||
|
|
||||
|
//go:embed templates/index.html
|
||||
|
var indexTmplSource string |
||||
|
var indexTmpl *template.Template |
||||
|
|
||||
|
type IndexPageData struct { |
||||
|
FileCount string |
||||
|
Files map[string]DirectoryIndexers.VideoFile |
||||
|
} |
||||
|
|
||||
|
func init() { |
||||
|
var err error |
||||
|
indexTmpl = template.New("index.tmpl") |
||||
|
indexTmpl, err = indexTmpl.Parse(indexTmplSource) |
||||
|
if err != nil { |
||||
|
fmt.Println(err) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
func Index(writer http.ResponseWriter, request *http.Request, FL *DirectoryIndexers.FileList) { |
||||
|
FL.RLock() |
||||
|
defer FL.RUnlock() |
||||
|
|
||||
|
data := IndexPageData{ |
||||
|
FileCount: strconv.Itoa(len(FL.Files)), |
||||
|
Files: FL.Files, |
||||
|
} |
||||
|
|
||||
|
err := indexTmpl.Execute(writer, data) |
||||
|
if err != nil { |
||||
|
fmt.Println(err) |
||||
|
} |
||||
|
} |
@ -0,0 +1,66 @@ |
|||||
|
package PageHandlers |
||||
|
|
||||
|
import ( |
||||
|
_ "embed" |
||||
|
"fmt" |
||||
|
"html/template" |
||||
|
"log" |
||||
|
"net/http" |
||||
|
"strconv" |
||||
|
"strings" |
||||
|
"ytdlp-viewer/DirectoryIndexers" |
||||
|
) |
||||
|
|
||||
|
//go:embed templates/search.html
|
||||
|
var searchTmplSource string |
||||
|
var searchTmpl *template.Template |
||||
|
|
||||
|
func init() { |
||||
|
var err error |
||||
|
searchTmpl = template.New("search.tmpl") |
||||
|
searchTmpl, err = searchTmpl.Parse(searchTmplSource) |
||||
|
if err != nil { |
||||
|
fmt.Println(err) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
type SearchPageData struct { |
||||
|
Results []DirectoryIndexers.VideoFile |
||||
|
ResultCount string |
||||
|
SearchTerm string |
||||
|
} |
||||
|
|
||||
|
func SearchHandler(writer http.ResponseWriter, request *http.Request, FL *DirectoryIndexers.FileList) { |
||||
|
FL.RLock() |
||||
|
defer FL.RUnlock() |
||||
|
|
||||
|
keys, ok := request.URL.Query()["term"] |
||||
|
if !ok || len(keys[0]) < 1 { |
||||
|
log.Println("Url Param 'term' is missing") |
||||
|
return |
||||
|
} |
||||
|
|
||||
|
var results []DirectoryIndexers.VideoFile |
||||
|
|
||||
|
for _, video := range FL.Files { |
||||
|
if video.Id == keys[0] { |
||||
|
results = append(results, video) |
||||
|
break |
||||
|
} |
||||
|
if strings.Contains(strings.ToUpper(video.Title), strings.ToUpper(keys[0])) { |
||||
|
results = append(results, video) |
||||
|
continue |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
data := SearchPageData{ |
||||
|
Results: results, |
||||
|
ResultCount: strconv.Itoa(len(results)), |
||||
|
SearchTerm: keys[0], |
||||
|
} |
||||
|
|
||||
|
err := searchTmpl.Execute(writer, data) |
||||
|
if err != nil { |
||||
|
fmt.Println(err) |
||||
|
} |
||||
|
} |
@ -0,0 +1,58 @@ |
|||||
|
package PageHandlers |
||||
|
|
||||
|
import ( |
||||
|
_ "embed" |
||||
|
"fmt" |
||||
|
"html/template" |
||||
|
"log" |
||||
|
"net/http" |
||||
|
"ytdlp-viewer/DirectoryIndexers" |
||||
|
) |
||||
|
|
||||
|
//go:embed templates/view.html
|
||||
|
var viewTmplSource string |
||||
|
var viewTmpl *template.Template |
||||
|
|
||||
|
type ViewPageData struct { |
||||
|
Title string |
||||
|
Filename string |
||||
|
Id string |
||||
|
Extension string |
||||
|
} |
||||
|
|
||||
|
func init() { |
||||
|
var err error |
||||
|
viewTmpl = template.New("view.tmpl") |
||||
|
viewTmpl, err = viewTmpl.Parse(viewTmplSource) |
||||
|
if err != nil { |
||||
|
fmt.Println(err) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
func View(writer http.ResponseWriter, request *http.Request, FL *DirectoryIndexers.FileList) { |
||||
|
FL.RLock() |
||||
|
defer FL.RUnlock() |
||||
|
|
||||
|
keys, ok := request.URL.Query()["id"] |
||||
|
if !ok || len(keys[0]) < 1 { |
||||
|
log.Println("Url Param 'id' is missing") |
||||
|
return |
||||
|
} |
||||
|
|
||||
|
if _, ok := FL.Files[keys[0]]; !ok { |
||||
|
return |
||||
|
} |
||||
|
|
||||
|
video := FL.Files[keys[0]] |
||||
|
data := ViewPageData{ |
||||
|
Title: video.Title, |
||||
|
Filename: video.Filename, |
||||
|
Id: video.Id, |
||||
|
Extension: video.Extension, |
||||
|
} |
||||
|
|
||||
|
err := viewTmpl.Execute(writer, data) |
||||
|
if err != nil { |
||||
|
fmt.Println(err) |
||||
|
} |
||||
|
} |
Loading…
Reference in new issue