Skip to content

Commit

Permalink
Add Dendrite exporter
Browse files Browse the repository at this point in the history
  • Loading branch information
turt2live committed Nov 20, 2023
1 parent 4b25dc7 commit 0a4e3fc
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package main

import (
"io"
"os"
"path"

"github.com/turt2live/matrix-media-repo/archival/v2archive"
"github.com/turt2live/matrix-media-repo/cmd/homeserver_offline_importers/_common"
"github.com/turt2live/matrix-media-repo/common/rcontext"
"github.com/turt2live/matrix-media-repo/homeserver_interop/dendrite"
"github.com/turt2live/matrix-media-repo/util"
)

func main() {
cfg := _common.InitExportPsqlFlatFile("Dendrite", "media_api.base_path")
ctx := rcontext.InitialNoConfig()

ctx.Log.Debug("Connecting to homeserver database...")
hsDb, err := dendrite.OpenDatabase(cfg.ConnectionString, cfg.ServerName)
if err != nil {
panic(err)
}

_common.PsqlFlatFileArchive[dendrite.LocalMedia](ctx, cfg, hsDb, func(r *dendrite.LocalMedia) (v2archive.MediaInfo, io.ReadCloser, error) {
// For Base64Hash ABCCDD :
// $importPath/A/B/CCDD/file

mxc := util.MxcUri(cfg.ServerName, r.MediaId)

ctx.Log.Info("Copying " + mxc)

filePath := path.Join(cfg.ImportPath, r.Base64Hash[0:1], r.Base64Hash[1:2], r.Base64Hash[2:], "file")

f, err := os.Open(filePath)
if os.IsNotExist(err) && cfg.SkipMissing {
ctx.Log.Warn("File does not appear to exist, skipping: " + filePath)
return v2archive.MediaInfo{
FileName: filePath,
}, nil, err
}
if err != nil {
return v2archive.MediaInfo{}, nil, err
}

return v2archive.MediaInfo{
Origin: cfg.ServerName,
MediaId: r.MediaId,
FileName: r.UploadName,
ContentType: r.ContentType,
CreationTs: r.CreationTs,
S3Url: "",
UserId: r.UserId,
}, f, nil
})
}
96 changes: 96 additions & 0 deletions homeserver_interop/dendrite/db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package dendrite

import (
"database/sql"
"errors"

_ "github.com/lib/pq" // postgres driver
"github.com/turt2live/matrix-media-repo/homeserver_interop"
)

const selectLocalMedia = "SELECT media_id, media_origin, content_type, file_size_bytes, creation_ts, upload_name, base64hash, user_id FROM mediaapi_media_repository WHERE media_origin = $1;"

type LocalMedia struct {
homeserver_interop.ImportDbMedia
MediaId string
MediaOrigin string
ContentType string
FileSizeBytes int64
CreationTs int64
UploadName string
Base64Hash string
UserId string
}

type DenDatabase struct {
homeserver_interop.ImportDb[LocalMedia]
db *sql.DB
statements statements
origin string
}

type statements struct {
selectLocalMedia *sql.Stmt
}

func OpenDatabase(connectionString string, origin string) (*DenDatabase, error) {
d := DenDatabase{origin: origin}
var err error

if d.db, err = sql.Open("postgres", connectionString); err != nil {
return nil, err
}

if d.statements.selectLocalMedia, err = d.db.Prepare(selectLocalMedia); err != nil {
return nil, err
}

return &d, nil
}

func (d *DenDatabase) GetAllMedia() ([]*LocalMedia, error) {
rows, err := d.statements.selectLocalMedia.Query(d.origin)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return []*LocalMedia{}, nil // no records
}
return nil, err
}

var results []*LocalMedia
for rows.Next() {
var mediaId sql.NullString
var mediaOrigin sql.NullString
var contentType sql.NullString
var sizeBytes sql.NullInt64
var createdTs sql.NullInt64
var uploadName sql.NullString
var b64hash sql.NullString
var userId sql.NullString
err = rows.Scan(
&mediaId,
&mediaOrigin,
&contentType,
&sizeBytes,
&createdTs,
&uploadName,
&b64hash,
&userId,
)
if err != nil {
return nil, err
}
results = append(results, &LocalMedia{
MediaId: mediaId.String,
MediaOrigin: mediaOrigin.String,
ContentType: contentType.String,
FileSizeBytes: sizeBytes.Int64,
CreationTs: createdTs.Int64,
UploadName: uploadName.String,
Base64Hash: b64hash.String,
UserId: userId.String,
})
}

return results, nil
}

0 comments on commit 0a4e3fc

Please sign in to comment.