mirror of
https://github.com/photoprism/photoprism.git
synced 2026-01-23 02:24:24 +00:00
Media: Improve ReadUrl() implementation and tests #5330
Signed-off-by: Michael Mayer <michael@photoprism.app>
This commit is contained in:
parent
149f5e5731
commit
ed96f381b9
2 changed files with 20 additions and 1 deletions
|
|
@ -100,7 +100,14 @@ func ReadUrl(fileUrl string, schemes []string) (data []byte, err error) {
|
|||
return DecodeBase64String(binaryData)
|
||||
}
|
||||
case scheme.File:
|
||||
if data, err = os.ReadFile(fileUrl); err != nil { //nolint:gosec // fileUrl validated earlier
|
||||
path := u.Path
|
||||
if path == "" {
|
||||
path = u.Opaque
|
||||
}
|
||||
if path == "" {
|
||||
return data, fmt.Errorf("invalid %s url (empty path)", u.Scheme)
|
||||
}
|
||||
if data, err = os.ReadFile(path); err != nil { //nolint:gosec // file path validated earlier
|
||||
return data, fmt.Errorf("invalid %s url (%s)", u.Scheme, err)
|
||||
}
|
||||
default:
|
||||
|
|
|
|||
|
|
@ -5,6 +5,8 @@ import (
|
|||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
|
|
@ -75,6 +77,16 @@ func TestReadUrl(t *testing.T) {
|
|||
_, err := ReadUrl("file:///this/does/not/exist", []string{"file"})
|
||||
assert.Error(t, err)
|
||||
})
|
||||
t.Run("FileSchemeValidPng", func(t *testing.T) {
|
||||
tmp := t.TempDir()
|
||||
fn := filepath.Join(tmp, "pic.png")
|
||||
payload := append([]byte{0x89, 'P', 'N', 'G', '\r', '\n', 0x1A, '\n'}, bytes.Repeat([]byte{0}, 16)...)
|
||||
assert.NoError(t, os.WriteFile(fn, payload, 0o600))
|
||||
|
||||
data, err := ReadUrl("file://"+fn, []string{"file"})
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, payload, data)
|
||||
})
|
||||
}
|
||||
|
||||
func TestDataUrl_LargeBinary(t *testing.T) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue