mirror of
https://github.com/1f349/dendrite.git
synced 2024-11-09 22:42:58 +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) {
|
func NewDatabase(dataSourceName string) (*Database, error) {
|
||||||
var result Database
|
var result Database
|
||||||
var err error
|
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
|
return nil, err
|
||||||
}
|
}
|
||||||
if err = result.prepare(); err != nil {
|
if err = result.prepare(); err != nil {
|
||||||
|
@ -34,7 +34,7 @@ func NewDatabase(
|
|||||||
case "postgres":
|
case "postgres":
|
||||||
return nil, fmt.Errorf("Cannot use postgres implementation")
|
return nil, fmt.Errorf("Cannot use postgres implementation")
|
||||||
case "file":
|
case "file":
|
||||||
return sqlite3.NewDatabase(uri.Path)
|
return sqlite3.NewDatabase(dataSourceName)
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("Cannot use postgres implementation")
|
return nil, fmt.Errorf("Cannot use postgres implementation")
|
||||||
}
|
}
|
||||||
|
@ -48,7 +48,11 @@ type Database struct {
|
|||||||
func NewDatabase(dataSourceName string, serverName gomatrixserverlib.ServerName) (*Database, error) {
|
func NewDatabase(dataSourceName string, serverName gomatrixserverlib.ServerName) (*Database, error) {
|
||||||
var db *sql.DB
|
var db *sql.DB
|
||||||
var err error
|
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
|
return nil, err
|
||||||
}
|
}
|
||||||
partitions := internal.PartitionOffsetStatements{}
|
partitions := internal.PartitionOffsetStatements{}
|
||||||
|
@ -36,7 +36,7 @@ func NewDatabase(
|
|||||||
case "postgres":
|
case "postgres":
|
||||||
return nil, fmt.Errorf("Cannot use postgres implementation")
|
return nil, fmt.Errorf("Cannot use postgres implementation")
|
||||||
case "file":
|
case "file":
|
||||||
return sqlite3.NewDatabase(uri.Path, serverName)
|
return sqlite3.NewDatabase(dataSourceName, serverName)
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("Cannot use postgres implementation")
|
return nil, fmt.Errorf("Cannot use postgres implementation")
|
||||||
}
|
}
|
||||||
|
@ -41,7 +41,11 @@ type Database struct {
|
|||||||
func NewDatabase(dataSourceName string, serverName gomatrixserverlib.ServerName) (*Database, error) {
|
func NewDatabase(dataSourceName string, serverName gomatrixserverlib.ServerName) (*Database, error) {
|
||||||
var db *sql.DB
|
var db *sql.DB
|
||||||
var err error
|
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
|
return nil, err
|
||||||
}
|
}
|
||||||
d := devicesStatements{}
|
d := devicesStatements{}
|
||||||
|
@ -36,7 +36,7 @@ func NewDatabase(
|
|||||||
case "postgres":
|
case "postgres":
|
||||||
return nil, fmt.Errorf("Cannot use postgres implementation")
|
return nil, fmt.Errorf("Cannot use postgres implementation")
|
||||||
case "file":
|
case "file":
|
||||||
return sqlite3.NewDatabase(uri.Path, serverName)
|
return sqlite3.NewDatabase(dataSourceName, serverName)
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("Cannot use postgres implementation")
|
return nil, fmt.Errorf("Cannot use postgres implementation")
|
||||||
}
|
}
|
||||||
|
@ -38,7 +38,11 @@ type Database struct {
|
|||||||
func NewDatabase(dataSourceName string) (*Database, error) {
|
func NewDatabase(dataSourceName string) (*Database, error) {
|
||||||
var result Database
|
var result Database
|
||||||
var err error
|
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
|
return nil, err
|
||||||
}
|
}
|
||||||
if err = result.prepare(); err != nil {
|
if err = result.prepare(); err != nil {
|
||||||
|
@ -33,7 +33,7 @@ func NewDatabase(
|
|||||||
}
|
}
|
||||||
switch uri.Scheme {
|
switch uri.Scheme {
|
||||||
case "file":
|
case "file":
|
||||||
return sqlite3.NewDatabase(uri.Path)
|
return sqlite3.NewDatabase(dataSourceName)
|
||||||
case "postgres":
|
case "postgres":
|
||||||
return nil, fmt.Errorf("Cannot use postgres implementation")
|
return nil, fmt.Errorf("Cannot use postgres implementation")
|
||||||
default:
|
default:
|
||||||
|
@ -277,12 +277,9 @@ func setupNaffka(cfg *config.Dendrite) (sarama.Consumer, sarama.SyncProducer) {
|
|||||||
uri, err := url.Parse(string(cfg.Database.Naffka))
|
uri, err := url.Parse(string(cfg.Database.Naffka))
|
||||||
if err != nil || uri.Scheme == "file" {
|
if err != nil || uri.Scheme == "file" {
|
||||||
var cs string
|
var cs string
|
||||||
if uri.Opaque != "" { // file:filename.db
|
cs, err = sqlutil.ParseFileURI(string(cfg.Database.Naffka))
|
||||||
cs = uri.Opaque
|
if err != nil {
|
||||||
} else if uri.Path != "" { // file:///path/to/filename.db
|
logrus.WithError(err).Panic("Failed to parse naffka database file URI")
|
||||||
cs = uri.Path
|
|
||||||
} else {
|
|
||||||
logrus.Panic("file uri has no filename")
|
|
||||||
}
|
}
|
||||||
db, err = sqlutil.Open(internal.SQLiteDriverName(), cs, nil)
|
db, err = sqlutil.Open(internal.SQLiteDriverName(), cs, nil)
|
||||||
if err != 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) {
|
func Open(dataSourceName string) (*Database, error) {
|
||||||
var d Database
|
var d Database
|
||||||
var err error
|
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
|
return nil, err
|
||||||
}
|
}
|
||||||
if err = d.statements.prepare(d.db); err != nil {
|
if err = d.statements.prepare(d.db); err != nil {
|
||||||
|
@ -35,7 +35,7 @@ func Open(
|
|||||||
case "postgres":
|
case "postgres":
|
||||||
return nil, fmt.Errorf("Cannot use postgres implementation")
|
return nil, fmt.Errorf("Cannot use postgres implementation")
|
||||||
case "file":
|
case "file":
|
||||||
return sqlite3.Open(uri.Path)
|
return sqlite3.Open(dataSourceName)
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("Cannot use postgres implementation")
|
return nil, fmt.Errorf("Cannot use postgres implementation")
|
||||||
}
|
}
|
||||||
|
@ -42,7 +42,11 @@ type attributeValue interface{}
|
|||||||
func NewPublicRoomsServerDatabase(dataSourceName string, localServerName gomatrixserverlib.ServerName) (*PublicRoomsServerDatabase, error) {
|
func NewPublicRoomsServerDatabase(dataSourceName string, localServerName gomatrixserverlib.ServerName) (*PublicRoomsServerDatabase, error) {
|
||||||
var db *sql.DB
|
var db *sql.DB
|
||||||
var err error
|
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
|
return nil, err
|
||||||
}
|
}
|
||||||
storage := PublicRoomsServerDatabase{
|
storage := PublicRoomsServerDatabase{
|
||||||
|
@ -32,7 +32,7 @@ func NewPublicRoomsServerDatabase(dataSourceName string, localServerName gomatri
|
|||||||
case "postgres":
|
case "postgres":
|
||||||
return nil, fmt.Errorf("Cannot use postgres implementation")
|
return nil, fmt.Errorf("Cannot use postgres implementation")
|
||||||
case "file":
|
case "file":
|
||||||
return sqlite3.NewPublicRoomsServerDatabase(uri.Path, localServerName)
|
return sqlite3.NewPublicRoomsServerDatabase(dataSourceName, localServerName)
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("Cannot use postgres implementation")
|
return nil, fmt.Errorf("Cannot use postgres implementation")
|
||||||
}
|
}
|
||||||
|
@ -18,8 +18,6 @@ package sqlite3
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"errors"
|
|
||||||
"net/url"
|
|
||||||
|
|
||||||
"github.com/matrix-org/dendrite/internal/sqlutil"
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
||||||
|
|
||||||
@ -50,18 +48,10 @@ type Database struct {
|
|||||||
// nolint: gocyclo
|
// nolint: gocyclo
|
||||||
func Open(dataSourceName string) (*Database, error) {
|
func Open(dataSourceName string) (*Database, error) {
|
||||||
var d Database
|
var d Database
|
||||||
uri, err := url.Parse(dataSourceName)
|
cs, err := sqlutil.ParseFileURI(dataSourceName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
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 {
|
if d.db, err = sqlutil.Open(internal.SQLiteDriverName(), cs, nil); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -17,8 +17,6 @@ package sqlite3
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
|
||||||
"net/url"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"golang.org/x/crypto/ed25519"
|
"golang.org/x/crypto/ed25519"
|
||||||
@ -46,18 +44,10 @@ func NewDatabase(
|
|||||||
serverKey ed25519.PublicKey,
|
serverKey ed25519.PublicKey,
|
||||||
serverKeyID gomatrixserverlib.KeyID,
|
serverKeyID gomatrixserverlib.KeyID,
|
||||||
) (*Database, error) {
|
) (*Database, error) {
|
||||||
uri, err := url.Parse(dataSourceName)
|
cs, err := sqlutil.ParseFileURI(dataSourceName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
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)
|
db, err := sqlutil.Open(internal.SQLiteDriverName(), cs, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -17,8 +17,6 @@ package sqlite3
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"errors"
|
|
||||||
"net/url"
|
|
||||||
|
|
||||||
"github.com/matrix-org/dendrite/internal/sqlutil"
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
||||||
|
|
||||||
@ -43,18 +41,10 @@ type SyncServerDatasource struct {
|
|||||||
// nolint: gocyclo
|
// nolint: gocyclo
|
||||||
func NewDatabase(dataSourceName string) (*SyncServerDatasource, error) {
|
func NewDatabase(dataSourceName string) (*SyncServerDatasource, error) {
|
||||||
var d SyncServerDatasource
|
var d SyncServerDatasource
|
||||||
uri, err := url.Parse(dataSourceName)
|
cs, err := sqlutil.ParseFileURI(dataSourceName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
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 {
|
if d.db, err = sqlutil.Open(internal.SQLiteDriverName(), cs, nil); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user