diff --git a/go.mod b/go.mod index 4add5ae1a..f706074e9 100644 --- a/go.mod +++ b/go.mod @@ -48,6 +48,7 @@ require ( golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 golang.org/x/image v0.18.0 golang.org/x/mobile v0.0.0-20240520174638-fa72addaaa1b + golang.org/x/net v0.29.0 golang.org/x/sync v0.8.0 golang.org/x/term v0.24.0 gopkg.in/h2non/bimg.v1 v1.1.9 @@ -140,7 +141,6 @@ require ( go.opentelemetry.io/otel/trace v1.28.0 // indirect go.uber.org/mock v0.4.0 // indirect golang.org/x/mod v0.17.0 // indirect - golang.org/x/net v0.29.0 // indirect golang.org/x/sys v0.25.0 // indirect golang.org/x/text v0.18.0 // indirect golang.org/x/time v0.6.0 // indirect diff --git a/mediaapi/routing/url_preview.go b/mediaapi/routing/url_preview.go index 85affba78..75150f440 100644 --- a/mediaapi/routing/url_preview.go +++ b/mediaapi/routing/url_preview.go @@ -612,9 +612,6 @@ func getMetaFieldsFromHTML(resp *http.Response) map[string]string { "og:image", "og:image:url", "og:image:secure_url", - "og:image:width", - "og:image:height", - "og:image:type", "og:type", "og:url", } diff --git a/mediaapi/routing/url_preview_test.go b/mediaapi/routing/url_preview_test.go new file mode 100644 index 000000000..f9def31ff --- /dev/null +++ b/mediaapi/routing/url_preview_test.go @@ -0,0 +1,153 @@ +package routing + +import ( + "context" + "fmt" + "io" + "net/http" + "os" + "path/filepath" + "reflect" + "strings" + "testing" + + "github.com/matrix-org/dendrite/internal/sqlutil" + "github.com/matrix-org/dendrite/mediaapi/fileutils" + "github.com/matrix-org/dendrite/mediaapi/storage" + "github.com/matrix-org/dendrite/mediaapi/types" + "github.com/matrix-org/dendrite/setup/config" + userapi "github.com/matrix-org/dendrite/userapi/api" + log "github.com/sirupsen/logrus" +) + +var tests = []map[string]interface{}{ + { + "test": ` + + Title + + + + + + + + + + `, + "expected": map[string]string{ + "og:title": "test_title", + "og:description": "test_description", + "og:image": "test.png", + "og:image:url": "test2.png", + "og:image:secure_url": "test3.png", + "og:type": "image/jpeg", + "og:url": "/image.jpg", + }, + }, +} + +func Test_getMetaFieldsFromHTML(t *testing.T) { + for _, test := range tests { + r := &http.Response{Body: io.NopCloser(strings.NewReader(test["test"].(string)))} + result := getMetaFieldsFromHTML(r) + fmt.Println(result) + for k, v := range test["expected"].(map[string]string) { + if val, ok := result[k]; ok { + if val != v { + t.Errorf("Values don't match: expected %s, got %s", v, val) + } + } else { + t.Errorf("Not found %s in the test HTML", k) + } + } + } +} + +func Test_LoadStorePreview(t *testing.T) { + type fields struct { + MediaMetadata *types.MediaMetadata + Logger *log.Entry + } + type args struct { + ctx context.Context + reqReader io.Reader + cfg *config.MediaAPI + db storage.Database + activeThumbnailGeneration *types.ActiveThumbnailGeneration + } + + wd, err := os.Getwd() + if err != nil { + t.Errorf("failed to get current working directory: %v", err) + } + + maxSize := config.FileSizeBytes(8) + logger := log.New().WithField("mediaapi", "test") + testdataPath := filepath.Join(wd, "./testdata") + + g := &config.Global{} + g.Defaults(config.DefaultOpts{Generate: true}) + cfg := &config.MediaAPI{ + Matrix: g, + MaxFileSizeBytes: maxSize, + BasePath: config.Path(testdataPath), + AbsBasePath: config.Path(testdataPath), + DynamicThumbnails: false, + } + + // create testdata folder and remove when done + _ = os.Mkdir(testdataPath, os.ModePerm) + defer fileutils.RemoveDir(types.Path(testdataPath), nil) + cm := sqlutil.NewConnectionManager(nil, config.DatabaseOptions{}) + db, err := storage.NewMediaAPIDatasource(cm, &config.DatabaseOptions{ + ConnectionString: "file::memory:?cache=shared", + MaxOpenConnections: 100, + MaxIdleConnections: 2, + ConnMaxLifetimeSeconds: -1, + }) + if err != nil { + t.Errorf("error opening mediaapi database: %v", err) + } + + testPreview := &types.UrlPreview{ + Title: "test_title", + Description: "test_description", + ImageUrl: "test_url.png", + ImageType: "image/png", + ImageSize: types.FileSizeBytes(100), + ImageHeight: 100, + ImageWidth: 100, + Type: "video", + Url: "video.avi", + } + + hash := getHashFromString("testhash") + device := userapi.Device{ + ID: "1", + UserID: "user", + } + err = storeUrlPreviewResponse(context.Background(), cfg, db, device, hash, testPreview, logger) + if err != nil { + t.Errorf("Can't store urel preview response: %v", err) + } + + filePath, err := fileutils.GetPathFromBase64Hash(hash, cfg.AbsBasePath) + if err != nil { + t.Errorf("Can't get stored file path: %v", err) + } + _, err = os.Stat(filePath) + if err != nil { + t.Errorf("Can't get stored file info: %v", err) + + } + + loadedPreview, err := loadUrlPreviewResponse(context.Background(), cfg, db, hash) + if err != nil { + t.Errorf("Can't load the preview: %v", err) + } + + if !reflect.DeepEqual(loadedPreview, testPreview) { + t.Errorf("Stored and loaded previews not equal: stored=%v, loaded=%v", testPreview, loadedPreview) + } +} diff --git a/setup/config/config_test.go b/setup/config/config_test.go index eeefb425f..14f4fffc4 100644 --- a/setup/config/config_test.go +++ b/setup/config/config_test.go @@ -322,3 +322,18 @@ func Test_SigningIdentityFor(t *testing.T) { }) } } + +func Test_MediaAPIConfigVerify(t *testing.T) { + config := &MediaAPI{ + Matrix: &Global{DatabaseOptions: DatabaseOptions{}}, + Database: DatabaseOptions{}, + MaxFileSizeBytes: FileSizeBytes(9223372036854775807), + } + + configErrs := &ConfigErrors{} + + config.Verify(configErrs) + if config.MaxFileSizeBytes != DefaultMaxFileSizeBytes { + t.Errorf("config.MediaAPI.MaxFileSizeBytes got = %v, want %v", config.MaxFileSizeBytes, DefaultMaxFileSizeBytes) + } +}