mirror of
https://github.com/wailsapp/wails.git
synced 2025-05-02 15:30:37 +08:00
[v2] Improve translation of URI to file to be loaded
This commit is contained in:
parent
187bf3085c
commit
8107a8bd1e
20
v2/internal/frontend/desktop/common/uri_translate.go
Normal file
20
v2/internal/frontend/desktop/common/uri_translate.go
Normal file
@ -0,0 +1,20 @@
|
||||
package common
|
||||
|
||||
import "net/url"
|
||||
|
||||
func TranslateUriToFile(uri string, expectedScheme string, expectedHost string) (file string, match bool, err error) {
|
||||
url, err := url.Parse(uri)
|
||||
if err != nil {
|
||||
return "", false, err
|
||||
}
|
||||
|
||||
if url.Scheme != expectedScheme || url.Host != expectedHost {
|
||||
return "", false, nil
|
||||
}
|
||||
|
||||
filePath := url.Path
|
||||
if filePath == "" {
|
||||
filePath = "/"
|
||||
}
|
||||
return filePath, true, nil
|
||||
}
|
@ -19,12 +19,12 @@ import (
|
||||
"html/template"
|
||||
"log"
|
||||
"strconv"
|
||||
"strings"
|
||||
"unsafe"
|
||||
|
||||
"github.com/wailsapp/wails/v2/internal/binding"
|
||||
"github.com/wailsapp/wails/v2/internal/frontend"
|
||||
"github.com/wailsapp/wails/v2/internal/frontend/assetserver"
|
||||
"github.com/wailsapp/wails/v2/internal/frontend/desktop/common"
|
||||
"github.com/wailsapp/wails/v2/internal/logger"
|
||||
"github.com/wailsapp/wails/v2/pkg/options"
|
||||
)
|
||||
@ -270,12 +270,18 @@ func (f *Frontend) ExecJS(js string) {
|
||||
}
|
||||
|
||||
func (f *Frontend) processRequest(r *request) {
|
||||
url := C.GoString(r.url)
|
||||
url = strings.TrimPrefix(url, "wails://wails")
|
||||
if !strings.HasPrefix(url, "/") {
|
||||
uri := C.GoString(r.url)
|
||||
|
||||
// Translate URI to file
|
||||
file, match, err := common.TranslateUriToFile(uri, "wails", "wails")
|
||||
if err != nil {
|
||||
// TODO Handle errors
|
||||
return
|
||||
} else if !match {
|
||||
return
|
||||
}
|
||||
_contents, _mimetype, err := f.assets.Load(url)
|
||||
|
||||
_contents, _mimetype, err := f.assets.Load(file)
|
||||
if err != nil {
|
||||
f.logger.Error(err.Error())
|
||||
//TODO: Handle errors
|
||||
|
@ -27,7 +27,6 @@ import (
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"text/template"
|
||||
"unsafe"
|
||||
@ -35,6 +34,7 @@ import (
|
||||
"github.com/wailsapp/wails/v2/internal/binding"
|
||||
"github.com/wailsapp/wails/v2/internal/frontend"
|
||||
"github.com/wailsapp/wails/v2/internal/frontend/assetserver"
|
||||
"github.com/wailsapp/wails/v2/internal/frontend/desktop/common"
|
||||
"github.com/wailsapp/wails/v2/internal/logger"
|
||||
"github.com/wailsapp/wails/v2/pkg/options"
|
||||
)
|
||||
@ -328,14 +328,16 @@ func (f *Frontend) processRequest(request unsafe.Pointer) {
|
||||
uri := C.webkit_uri_scheme_request_get_uri(req)
|
||||
goURI := C.GoString(uri)
|
||||
|
||||
// Translate URI
|
||||
goURI = strings.TrimPrefix(goURI, "wails://")
|
||||
if !strings.HasPrefix(goURI, "/") {
|
||||
file, match, err := common.TranslateUriToFile(uri, "wails", "")
|
||||
if err != nil {
|
||||
// TODO Handle errors
|
||||
return
|
||||
} else if !match {
|
||||
return
|
||||
}
|
||||
|
||||
// Load file from asset store
|
||||
content, mimeType, err := f.assets.Load(goURI)
|
||||
content, mimeType, err := f.assets.Load(file)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
@ -19,6 +19,7 @@ import (
|
||||
"github.com/wailsapp/wails/v2/internal/binding"
|
||||
"github.com/wailsapp/wails/v2/internal/frontend"
|
||||
"github.com/wailsapp/wails/v2/internal/frontend/assetserver"
|
||||
"github.com/wailsapp/wails/v2/internal/frontend/desktop/common"
|
||||
"github.com/wailsapp/wails/v2/internal/logger"
|
||||
"github.com/wailsapp/wails/v2/pkg/options"
|
||||
)
|
||||
@ -335,14 +336,16 @@ func (f *Frontend) processRequest(req *edge.ICoreWebView2WebResourceRequest, arg
|
||||
//Get the request
|
||||
uri, _ := req.GetUri()
|
||||
|
||||
// Translate URI
|
||||
uri = strings.TrimPrefix(uri, "file://wails")
|
||||
if !strings.HasPrefix(uri, "/") {
|
||||
file, match, err := common.TranslateUriToFile(uri, "file", "wails")
|
||||
if err != nil {
|
||||
// TODO Handle errors
|
||||
return
|
||||
} else if !match {
|
||||
return
|
||||
}
|
||||
|
||||
// Load file from asset store
|
||||
content, mimeType, err := f.assets.Load(uri)
|
||||
content, mimeType, err := f.assets.Load(file)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user