feat: handle subtitles for video streaming (format code)

This commit is contained in:
root 2018-07-25 13:13:30 +02:00
parent d82bce8e33
commit 4b2f3feca1
2 changed files with 48 additions and 48 deletions

View File

@ -1,24 +1,24 @@
package http package http
import ( import (
"os"
"io/ioutil"
"net/http"
"path/filepath"
"regexp"
"bytes" "bytes"
fb "github.com/filebrowser/filebrowser" fb "github.com/filebrowser/filebrowser"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"regexp"
) )
func subtitlesHandler(c *fb.Context, w http.ResponseWriter, r *http.Request) (int, error) { func subtitlesHandler(c *fb.Context, w http.ResponseWriter, r *http.Request) (int, error) {
files, err := ReadDir(filepath.Dir(c.File.Path)) files, err := ReadDir(filepath.Dir(c.File.Path))
if(err != nil) { if err != nil {
return http.StatusInternalServerError, err return http.StatusInternalServerError, err
} }
var subtitles = make([]map[string]string, 0) var subtitles = make([]map[string]string, 0)
for _, file := range files { for _, file := range files {
ext := filepath.Ext(file.Name()) ext := filepath.Ext(file.Name())
if(ext == ".vtt" || ext == ".srt") { if ext == ".vtt" || ext == ".srt" {
var sub map[string]string = make(map[string]string) var sub map[string]string = make(map[string]string)
sub["src"] = filepath.Dir(c.File.Path) + "/" + file.Name() sub["src"] = filepath.Dir(c.File.Path) + "/" + file.Name()
sub["kind"] = "subtitles" sub["kind"] = "subtitles"
@ -57,12 +57,12 @@ func subtitleHandler(c *fb.Context, w http.ResponseWriter, r *http.Request) (int
func CleanSubtitle(filename string) (string, error) { func CleanSubtitle(filename string) (string, error) {
b, err := ioutil.ReadFile(filename) b, err := ioutil.ReadFile(filename)
if(err != nil) { if err != nil {
return "", err return "", err
} }
str := string(b) // convert content to a 'string' str := string(b) // convert content to a 'string'
ext := filepath.Ext(filename) ext := filepath.Ext(filename)
if(ext == ".srt") { if ext == ".srt" {
re := regexp.MustCompile("([0-9]{2}:[0-9]{2}:[0-9]{2}),([0-9]{3})") re := regexp.MustCompile("([0-9]{2}:[0-9]{2}:[0-9]{2}),([0-9]{3})")
str = "WEBVTT\n\n" + re.ReplaceAllString(str, "$1.$2") str = "WEBVTT\n\n" + re.ReplaceAllString(str, "$1.$2")
} }