5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-02 16:02:07 +08:00

[assetserver] Add support for range requests if the fs.FS provides an io.Seeker (#2091)

This allows streaming audio and videos with range requests if the
platform webview supports it.
This commit is contained in:
stffabi 2022-11-14 21:19:25 +01:00 committed by GitHub
parent efd209b7c5
commit 177d90497a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 5 deletions

View File

@ -78,7 +78,7 @@ func (d *assetHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
}
d.logDebug("Loading file '%s'", filename)
if err := d.serveFSFile(rw, filename); err != nil {
if err := d.serveFSFile(rw, req, filename); err != nil {
if os.IsNotExist(err) {
if handler != nil {
d.logDebug("File '%s' not found, serving '%s' by AssetHandler", filename, req.URL)
@ -106,7 +106,7 @@ func (d *assetHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
}
// serveFile will try to load the file from the fs.FS and write it to the response
func (d *assetHandler) serveFSFile(rw http.ResponseWriter, filename string) error {
func (d *assetHandler) serveFSFile(rw http.ResponseWriter, req *http.Request, filename string) error {
if d.fs == nil {
return os.ErrNotExist
}
@ -122,8 +122,6 @@ func (d *assetHandler) serveFSFile(rw http.ResponseWriter, filename string) erro
return err
}
rw.Header().Set(HeaderContentLength, fmt.Sprintf("%d", statInfo.Size()))
var buf [512]byte
n, err := file.Read(buf[:])
if err != nil && err != io.EOF {
@ -131,11 +129,24 @@ func (d *assetHandler) serveFSFile(rw http.ResponseWriter, filename string) erro
}
// Detect MimeType by sniffing the first 512 bytes
// Do the custom MimeType sniffing even though http.ServeContent would do it in case
// of an io.ReadSeeker. We would like to have a consistent behaviour in both cases.
if contentType := GetMimetype(filename, buf[:n]); contentType != "" {
rw.Header().Set(HeaderContentType, contentType)
}
// Write the first bytes
if fileSeeker, _ := file.(io.ReadSeeker); fileSeeker != nil {
if _, err := fileSeeker.Seek(0, io.SeekStart); err != nil {
return fmt.Errorf("seeker can't seek")
}
http.ServeContent(rw, req, statInfo.Name(), statInfo.ModTime(), fileSeeker)
return nil
}
rw.Header().Set(HeaderContentLength, fmt.Sprintf("%d", statInfo.Size()))
// Write the first 512 bytes used for MimeType sniffing
_, err = io.Copy(rw, bytes.NewReader(buf[:n]))
if err != nil {
return err

View File

@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- Added `OpenInspectorOnStartup` to debug options to allow opening the WebInspector during startup of the application in debug mode. Added by @stffabi in [PR](https://github.com/wailsapp/wails/pull/2080)
- On macOS `wails doctor` now also shows the version of Xcode installed. Added by @stffabi in [PR](https://github.com/wailsapp/wails/pull/2089)
- The [AssetServer](https://wails.io/docs/reference/options#assetserver) now supports handling range-requests if the [Assets](https://wails.io/docs/reference/options/#assets-1) `fs.FS` provides an `io.ReadSeeker`. Added by @stffabi in [PR](https://github.com/wailsapp/wails/pull/2091)
### Fixed
- The `noreload` flag in wails dev wasn't applied. Fixed by @stffabi in this [PR](https://github.com/wailsapp/wails/pull/2081)