mirror of
https://github.com/1f349/dendrite.git
synced 2024-11-24 20:51:42 +00:00
Add ParseFileURI and use it when dealing with file URIs (#1088)
* Add ParseFileURI and use it when dealing with file URIs Fixes #1059 * Missing file * Linting
This commit is contained in:
parent
d4f9a4bb97
commit
e7d1ac84c3
@ -37,7 +37,11 @@ type Database struct {
|
||||
func NewDatabase(dataSourceName string) (*Database, error) {
|
||||
var result Database
|
||||
var err error
|
||||
if result.db, err = sqlutil.Open(internal.SQLiteDriverName(), dataSourceName, nil); err != nil {
|
||||
cs, err := sqlutil.ParseFileURI(dataSourceName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if result.db, err = sqlutil.Open(internal.SQLiteDriverName(), cs, nil); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err = result.prepare(); err != nil {
|
||||
|
@ -34,7 +34,7 @@ func NewDatabase(
|
||||
case "postgres":
|
||||
return nil, fmt.Errorf("Cannot use postgres implementation")
|
||||
case "file":
|
||||
return sqlite3.NewDatabase(uri.Path)
|
||||
return sqlite3.NewDatabase(dataSourceName)
|
||||
default:
|
||||
return nil, fmt.Errorf("Cannot use postgres implementation")
|
||||
}
|
||||
|
@ -48,7 +48,11 @@ type Database struct {
|
||||
func NewDatabase(dataSourceName string, serverName gomatrixserverlib.ServerName) (*Database, error) {
|
||||
var db *sql.DB
|
||||
var err error
|
||||
if db, err = sqlutil.Open(internal.SQLiteDriverName(), dataSourceName, nil); err != nil {
|
||||
cs, err := sqlutil.ParseFileURI(dataSourceName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if db, err = sqlutil.Open(internal.SQLiteDriverName(), cs, nil); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
partitions := internal.PartitionOffsetStatements{}
|
||||
|
@ -36,7 +36,7 @@ func NewDatabase(
|
||||
case "postgres":
|
||||
return nil, fmt.Errorf("Cannot use postgres implementation")
|
||||
case "file":
|
||||
return sqlite3.NewDatabase(uri.Path, serverName)
|
||||
return sqlite3.NewDatabase(dataSourceName, serverName)
|
||||
default:
|
||||
return nil, fmt.Errorf("Cannot use postgres implementation")
|
||||
}
|
||||
|
@ -41,7 +41,11 @@ type Database struct {
|
||||
func NewDatabase(dataSourceName string, serverName gomatrixserverlib.ServerName) (*Database, error) {
|
||||
var db *sql.DB
|
||||
var err error
|
||||
if db, err = sqlutil.Open(internal.SQLiteDriverName(), dataSourceName, nil); err != nil {
|
||||
cs, err := sqlutil.ParseFileURI(dataSourceName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if db, err = sqlutil.Open(internal.SQLiteDriverName(), cs, nil); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
d := devicesStatements{}
|
||||
|
@ -36,7 +36,7 @@ func NewDatabase(
|
||||
case "postgres":
|
||||
return nil, fmt.Errorf("Cannot use postgres implementation")
|
||||
case "file":
|
||||
return sqlite3.NewDatabase(uri.Path, serverName)
|
||||
return sqlite3.NewDatabase(dataSourceName, serverName)
|
||||
default:
|
||||
return nil, fmt.Errorf("Cannot use postgres implementation")
|
||||
}
|
||||
|
@ -38,7 +38,11 @@ type Database struct {
|
||||
func NewDatabase(dataSourceName string) (*Database, error) {
|
||||
var result Database
|
||||
var err error
|
||||
if result.db, err = sqlutil.Open(internal.SQLiteDriverName(), dataSourceName, nil); err != nil {
|
||||
cs, err := sqlutil.ParseFileURI(dataSourceName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if result.db, err = sqlutil.Open(internal.SQLiteDriverName(), cs, nil); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err = result.prepare(); err != nil {
|
||||
|
@ -33,7 +33,7 @@ func NewDatabase(
|
||||
}
|
||||
switch uri.Scheme {
|
||||
case "file":
|
||||
return sqlite3.NewDatabase(uri.Path)
|
||||
return sqlite3.NewDatabase(dataSourceName)
|
||||
case "postgres":
|
||||
return nil, fmt.Errorf("Cannot use postgres implementation")
|
||||
default:
|
||||
|
@ -277,12 +277,9 @@ func setupNaffka(cfg *config.Dendrite) (sarama.Consumer, sarama.SyncProducer) {
|
||||
uri, err := url.Parse(string(cfg.Database.Naffka))
|
||||
if err != nil || uri.Scheme == "file" {
|
||||
var cs string
|
||||
if uri.Opaque != "" { // file:filename.db
|
||||
cs = uri.Opaque
|
||||
} else if uri.Path != "" { // file:///path/to/filename.db
|
||||
cs = uri.Path
|
||||
} else {
|
||||
logrus.Panic("file uri has no filename")
|
||||
cs, err = sqlutil.ParseFileURI(string(cfg.Database.Naffka))
|
||||
if err != nil {
|
||||
logrus.WithError(err).Panic("Failed to parse naffka database file URI")
|
||||
}
|
||||
db, err = sqlutil.Open(internal.SQLiteDriverName(), cs, nil)
|
||||
if err != nil {
|
||||
|
24
internal/sqlutil/uri.go
Normal file
24
internal/sqlutil/uri.go
Normal file
@ -0,0 +1,24 @@
|
||||
package sqlutil
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
// ParseFileURI returns the filepath in the given file: URI. Specifically, this will handle
|
||||
// both relative (file:foo.db) and absolute (file:///path/to/foo) paths.
|
||||
func ParseFileURI(dataSourceName string) (string, error) {
|
||||
uri, err := url.Parse(dataSourceName)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
var cs string
|
||||
if uri.Opaque != "" { // file:filename.db
|
||||
cs = uri.Opaque
|
||||
} else if uri.Path != "" { // file:///path/to/filename.db
|
||||
cs = uri.Path
|
||||
} else {
|
||||
return "", fmt.Errorf("invalid file uri: %s", dataSourceName)
|
||||
}
|
||||
return cs, nil
|
||||
}
|
@ -37,7 +37,11 @@ type Database struct {
|
||||
func Open(dataSourceName string) (*Database, error) {
|
||||
var d Database
|
||||
var err error
|
||||
if d.db, err = sqlutil.Open(internal.SQLiteDriverName(), dataSourceName, nil); err != nil {
|
||||
cs, err := sqlutil.ParseFileURI(dataSourceName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if d.db, err = sqlutil.Open(internal.SQLiteDriverName(), cs, nil); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err = d.statements.prepare(d.db); err != nil {
|
||||
|
@ -35,7 +35,7 @@ func Open(
|
||||
case "postgres":
|
||||
return nil, fmt.Errorf("Cannot use postgres implementation")
|
||||
case "file":
|
||||
return sqlite3.Open(uri.Path)
|
||||
return sqlite3.Open(dataSourceName)
|
||||
default:
|
||||
return nil, fmt.Errorf("Cannot use postgres implementation")
|
||||
}
|
||||
|
@ -42,7 +42,11 @@ type attributeValue interface{}
|
||||
func NewPublicRoomsServerDatabase(dataSourceName string, localServerName gomatrixserverlib.ServerName) (*PublicRoomsServerDatabase, error) {
|
||||
var db *sql.DB
|
||||
var err error
|
||||
if db, err = sqlutil.Open(internal.SQLiteDriverName(), dataSourceName, nil); err != nil {
|
||||
cs, err := sqlutil.ParseFileURI(dataSourceName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if db, err = sqlutil.Open(internal.SQLiteDriverName(), cs, nil); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
storage := PublicRoomsServerDatabase{
|
||||
|
@ -32,7 +32,7 @@ func NewPublicRoomsServerDatabase(dataSourceName string, localServerName gomatri
|
||||
case "postgres":
|
||||
return nil, fmt.Errorf("Cannot use postgres implementation")
|
||||
case "file":
|
||||
return sqlite3.NewPublicRoomsServerDatabase(uri.Path, localServerName)
|
||||
return sqlite3.NewPublicRoomsServerDatabase(dataSourceName, localServerName)
|
||||
default:
|
||||
return nil, fmt.Errorf("Cannot use postgres implementation")
|
||||
}
|
||||
|
@ -18,8 +18,6 @@ package sqlite3
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"net/url"
|
||||
|
||||
"github.com/matrix-org/dendrite/internal/sqlutil"
|
||||
|
||||
@ -50,18 +48,10 @@ type Database struct {
|
||||
// nolint: gocyclo
|
||||
func Open(dataSourceName string) (*Database, error) {
|
||||
var d Database
|
||||
uri, err := url.Parse(dataSourceName)
|
||||
cs, err := sqlutil.ParseFileURI(dataSourceName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var cs string
|
||||
if uri.Opaque != "" { // file:filename.db
|
||||
cs = uri.Opaque
|
||||
} else if uri.Path != "" { // file:///path/to/filename.db
|
||||
cs = uri.Path
|
||||
} else {
|
||||
return nil, errors.New("no filename or path in connect string")
|
||||
}
|
||||
if d.db, err = sqlutil.Open(internal.SQLiteDriverName(), cs, nil); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -17,8 +17,6 @@ package sqlite3
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"golang.org/x/crypto/ed25519"
|
||||
@ -46,18 +44,10 @@ func NewDatabase(
|
||||
serverKey ed25519.PublicKey,
|
||||
serverKeyID gomatrixserverlib.KeyID,
|
||||
) (*Database, error) {
|
||||
uri, err := url.Parse(dataSourceName)
|
||||
cs, err := sqlutil.ParseFileURI(dataSourceName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var cs string
|
||||
if uri.Opaque != "" { // file:filename.db
|
||||
cs = uri.Opaque
|
||||
} else if uri.Path != "" { // file:///path/to/filename.db
|
||||
cs = uri.Path
|
||||
} else {
|
||||
return nil, errors.New("no filename or path in connect string")
|
||||
}
|
||||
db, err := sqlutil.Open(internal.SQLiteDriverName(), cs, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -17,8 +17,6 @@ package sqlite3
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"net/url"
|
||||
|
||||
"github.com/matrix-org/dendrite/internal/sqlutil"
|
||||
|
||||
@ -43,18 +41,10 @@ type SyncServerDatasource struct {
|
||||
// nolint: gocyclo
|
||||
func NewDatabase(dataSourceName string) (*SyncServerDatasource, error) {
|
||||
var d SyncServerDatasource
|
||||
uri, err := url.Parse(dataSourceName)
|
||||
cs, err := sqlutil.ParseFileURI(dataSourceName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var cs string
|
||||
if uri.Opaque != "" { // file:filename.db
|
||||
cs = uri.Opaque
|
||||
} else if uri.Path != "" { // file:///path/to/filename.db
|
||||
cs = uri.Path
|
||||
} else {
|
||||
return nil, errors.New("no filename or path in connect string")
|
||||
}
|
||||
if d.db, err = sqlutil.Open(internal.SQLiteDriverName(), cs, nil); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user