-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
cmd/homeserver_offline_importers/export_dendrite_for_import/main.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |