fix: preserve SRT subtitle line breaks (#6002)
This commit is contained in:
1 parent
6209f8fddd
commit
d9cf2f0100
2 files changed
+75
-1
No files matched your search
+13
-1
@@ -2,7 +2,9 @@ package fbhttp
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/asticode/go-astisub"
|
||||
@@ -10,6 +12,8 @@ import (
|
||||
"github.com/filebrowser/filebrowser/v2/files"
|
||||
)
|
||||
|
||||
var srtLineBreakTag = regexp.MustCompile(`(?i)<br(?:\s+[^>]*)?\s*/?>`)
|
||||
|
||||
var subtitleHandler = withUser(func(w http.ResponseWriter, r *http.Request, d *data) (int, error) {
|
||||
if !d.user.Perm.Download {
|
||||
return http.StatusAccepted, nil
|
||||
@@ -49,7 +53,11 @@ func subtitleFileHandler(w http.ResponseWriter, r *http.Request, file *files.Fil
|
||||
// load subtitle for conversion to vtt
|
||||
var sub *astisub.Subtitles
|
||||
if strings.HasSuffix(file.Name, ".srt") {
|
||||
sub, err = astisub.ReadFromSRT(fd)
|
||||
content, readErr := io.ReadAll(fd)
|
||||
if readErr != nil {
|
||||
return http.StatusInternalServerError, readErr
|
||||
}
|
||||
sub, err = astisub.ReadFromSRT(bytes.NewReader(normalizeSRTLineBreaks(content)))
|
||||
} else if strings.HasSuffix(file.Name, ".ass") || strings.HasSuffix(file.Name, ".ssa") {
|
||||
sub, err = astisub.ReadFromSSA(fd)
|
||||
}
|
||||
@@ -78,3 +86,7 @@ func subtitleFileHandler(w http.ResponseWriter, r *http.Request, file *files.Fil
|
||||
http.ServeContent(w, r, file.Name, file.ModTime, bytes.NewReader(buf.Bytes()))
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
func normalizeSRTLineBreaks(content []byte) []byte {
|
||||
return srtLineBreakTag.ReplaceAll(content, []byte("\n"))
|
||||
}
|
||||
Reference in new issue
Block a user