mirror of
https://github.com/filebrowser/filebrowser.git
synced 2025-05-08 19:22:57 +00:00

- Use cobra in order to provide subcommands `serve` and `db`. - Subdir `cmd` is removed. - Subdir `cli` is created, which is a standard cobra structure. - Sources related to the core are moved to subdir `lib`. - #497 and #504 are merged. - Deprecated flags are added. See https://github.com/filebrowser/filebrowser/pull/497#discussion_r209428120. - [`viper.BindPFlags`](https://godoc.org/github.com/spf13/viper#BindPFlags) is used in order to reduce the verbosity in `serve.go`. Former-commit-id: 4b37ad82e91e01f7718cd389469814674bdf7032 [formerly c84d7fcf9c362b2aa1f9e5b57196152f53835e61] [formerly 2fef43c0382f3cc7d13e0297ccb467e38fac6982 [formerly 69a3f853bd2821d2c52a435277aaac68a468d39b]] Former-commit-id: 2f7dc1b8ee6735382cedae2053f40c546c21de45 [formerly b438417178b47ad5f7caf9cb728f4a5011a09f5e] Former-commit-id: 07bc58ab2e1ab10c30be8d0a5e760288bfc4d4dc
84 lines
1.9 KiB
Go
84 lines
1.9 KiB
Go
package http
|
|
|
|
import (
|
|
"bytes"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"regexp"
|
|
|
|
fb "github.com/filebrowser/filebrowser/lib"
|
|
)
|
|
|
|
func subtitlesHandler(c *fb.Context, w http.ResponseWriter, r *http.Request) (int, error) {
|
|
files, err := readDir(filepath.Dir(c.File.Path))
|
|
if err != nil {
|
|
return http.StatusInternalServerError, err
|
|
}
|
|
subtitles := make([]map[string]string, 0)
|
|
for _, file := range files {
|
|
ext := filepath.Ext(file.Name())
|
|
if ext == ".vtt" || ext == ".srt" {
|
|
sub := make(map[string]string)
|
|
sub["src"] = filepath.Dir(c.File.Path) + "/" + file.Name()
|
|
sub["kind"] = "subtitles"
|
|
sub["label"] = file.Name()
|
|
subtitles = append(subtitles, sub)
|
|
}
|
|
}
|
|
return renderJSON(w, subtitles)
|
|
}
|
|
|
|
func subtitleHandler(c *fb.Context, w http.ResponseWriter, r *http.Request) (int, error) {
|
|
str, err := cleanSubtitle(c.File.Path)
|
|
if err != nil {
|
|
return http.StatusInternalServerError, err
|
|
}
|
|
|
|
file, err := os.Open(c.File.Path)
|
|
if err != nil {
|
|
return http.StatusInternalServerError, err
|
|
}
|
|
defer file.Close()
|
|
|
|
stat, err := file.Stat()
|
|
if err != nil {
|
|
return http.StatusInternalServerError, err
|
|
}
|
|
|
|
w.Header().Set("Content-Disposition", "inline")
|
|
w.Header().Set("Content-Type", "text/vtt")
|
|
http.ServeContent(w, r, stat.Name(), stat.ModTime(), bytes.NewReader([]byte(str)))
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
func cleanSubtitle(filename string) (string, error) {
|
|
b, err := ioutil.ReadFile(filename)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
str := string(b) // convert content to a 'string'
|
|
ext := filepath.Ext(filename)
|
|
if ext == ".srt" {
|
|
re := regexp.MustCompile("([0-9]{2}:[0-9]{2}:[0-9]{2}),([0-9]{3})")
|
|
str = "WEBVTT\n\n" + re.ReplaceAllString(str, "$1.$2")
|
|
}
|
|
return str, err
|
|
}
|
|
|
|
func readDir(dirname string) ([]os.FileInfo, error) {
|
|
f, err := os.Open(dirname)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
list, err := f.Readdir(-1)
|
|
f.Close()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return list, nil
|
|
}
|