123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- package movies
- import (
- "fmt"
- "net/http"
- "os"
- "path/filepath"
- "strings"
- )
- type FileSource struct {
- ID string
- Files []string
- }
- func (fs *FileSource) Scan(root string) error {
- var files []string
- if err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
- if err != nil {
- return err
- }
- if info.IsDir() {
- return nil
- }
- files = append(files, path)
- return nil
- }); err != nil {
- return fmt.Errorf("error while walking directory %q: %w", root, err)
- }
- fs.Files = files
- return nil
- }
- func (fs *FileSource) Search(query string) []string {
- if fs == nil {
- return nil
- }
- split := strings.Split(query, " ")
- var res []string
- for _, path := range fs.Files {
- if stringContainsAll(path, split) {
- res = append(res, path)
- }
- }
- return res
- }
- func stringContainsAll(path string, split []string) bool {
- path = strings.ToLower(path)
- for _, s := range split {
- if !strings.Contains(path, strings.ToLower(s)) {
- return false
- }
- }
- return true
- }
- func (fs *FileSource) Serve(w http.ResponseWriter, r *http.Request, file string) {
- http.ServeFile(w, r, file)
- }
|