Skip to content

Instantly share code, notes, and snippets.

@autf
Created October 18, 2021 08:08
Show Gist options
  • Save autf/e13be7899c258871c39781a9830f6c55 to your computer and use it in GitHub Desktop.
Save autf/e13be7899c258871c39781a9830f6c55 to your computer and use it in GitHub Desktop.
package main
import (
"io/fs"
"log"
"net/http"
"strings"
)
func isVideoFile(name string) bool {
if strings.HasSuffix(name, ".mp4") {
return true
}
return strings.HasSuffix(name, ".mkv")
}
type videoFile struct {
http.File
}
func (f videoFile) Readdir(n int) (fis []fs.FileInfo, err error) {
files, err := f.File.Readdir(n)
for _, file := range files {
if isVideoFile(file.Name()) {
fis = append(fis, file)
}
}
return
}
type videoFileFS struct {
http.FileSystem
}
func (fsys videoFileFS) Open(name string) (http.File, error) {
if !(name == "/" || isVideoFile(name)) {
return nil, fs.ErrPermission
}
file, err := fsys.FileSystem.Open(name)
if err != nil {
return nil, err
}
log.Println("open", name)
return videoFile{file}, err
}
func main() {
fsys := videoFileFS{http.Dir(".")}
log.Fatal(http.ListenAndServe(":8080", http.FileServer(fsys)))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment