Skip to content

Commit

Permalink
Drop attachment requirements for media download
Browse files Browse the repository at this point in the history
  • Loading branch information
Half-Shot authored Aug 8, 2023
1 parent b986a30 commit af5155e
Showing 1 changed file with 24 additions and 20 deletions.
44 changes: 24 additions & 20 deletions tests/media_filename_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ func TestMediaFilenames(t *testing.T) {

mxcUri := alice.UploadContent(t, data.MatrixPng, filename, "image/png")

name := downloadForFilename(t, alice, mxcUri, "")
name, isAttachment := downloadForFilename(t, alice, mxcUri, "")

if name != filename {
// filename is not required, but if it's an attachment then check it matches
if isAttachment && name != filename {
t.Fatalf("Incorrect filename '%s', expected '%s'", name, filename)
}
})
Expand All @@ -58,8 +59,9 @@ func TestMediaFilenames(t *testing.T) {
mxcUri := alice.UploadContent(t, data.MatrixPng, "test.png", "image/png")

const altName = "file.png"
filename := downloadForFilename(t, alice, mxcUri, altName)
if filename != altName {
filename, isAttachment := downloadForFilename(t, alice, mxcUri, altName)

if isAttachment && filename != altName {
t.Fatalf("filename did not match, expected '%s', got '%s'", altName, filename)
}
})
Expand All @@ -83,8 +85,9 @@ func TestMediaFilenames(t *testing.T) {

const diffUnicodeFilename = "\u2615" // coffee emoji

filename := downloadForFilename(t, alice, mxcUri, diffUnicodeFilename)
if filename != diffUnicodeFilename {
filename, isAttachment := downloadForFilename(t, alice, mxcUri, diffUnicodeFilename)

if isAttachment && filename != diffUnicodeFilename {
t.Fatalf("filename did not match, expected '%s', got '%s'", diffUnicodeFilename, filename)
}
})
Expand All @@ -95,9 +98,9 @@ func TestMediaFilenames(t *testing.T) {

mxcUri := alice.UploadContent(t, data.MatrixPng, unicodeFileName, "image/png")

filename := downloadForFilename(t, alice, mxcUri, "")
filename, isAttachment := downloadForFilename(t, alice, mxcUri, "")

if filename != unicodeFileName {
if isAttachment && filename != unicodeFileName {
t.Fatalf("filename did not match, expected '%s', got '%s'", unicodeFileName, filename)
}
})
Expand All @@ -108,9 +111,9 @@ func TestMediaFilenames(t *testing.T) {

mxcUri := alice.UploadContent(t, data.MatrixPng, unicodeFileName, "image/png")

filename := downloadForFilename(t, bob, mxcUri, "")
filename, isAttachment := downloadForFilename(t, bob, mxcUri, "")

if filename != unicodeFileName {
if isAttachment && filename != unicodeFileName {
t.Fatalf("filename did not match, expected '%s', got '%s'", unicodeFileName, filename)
}
})
Expand All @@ -119,7 +122,7 @@ func TestMediaFilenames(t *testing.T) {
}

// Returns content disposition information like (mediatype, filename)
func downloadForFilename(t *testing.T, c *client.CSAPI, mxcUri string, diffName string) string {
func downloadForFilename(t *testing.T, c *client.CSAPI, mxcUri string, diffName string) (filename string, isAttachment bool) {
t.Helper()

origin, mediaId := client.SplitMxc(mxcUri)
Expand All @@ -139,15 +142,16 @@ func downloadForFilename(t *testing.T, c *client.CSAPI, mxcUri string, diffName
t.Fatalf("Got err when parsing content disposition: %s", err)
}

if mediaType != "attachment" {
t.Fatalf("Found unexpected mediatype %s, expected attachment", mediaType)
if mediaType = "attachment" || {

Check failure on line 145 in tests/media_filename_test.go

View workflow job for this annotation

GitHub Actions / Complement (Synapse)

expected operand, found '{'

Check failure on line 145 in tests/media_filename_test.go

View workflow job for this annotation

GitHub Actions / Complement (Dendrite)

expected operand, found '{'
if filename, ok := params["filename"]; ok {
return filename, true
} else {
t.Fatalf("Content Disposition did not have filename")
return "", true
}
}

if filename, ok := params["filename"]; ok {
return filename
} else {
t.Fatalf("Content Disposition did not have filename")

return ""
if mediaType != "inline" {
t.Fatalf("Found unexpected mediatype %s, expected attachment", mediaType)
}
return "", false
}

0 comments on commit af5155e

Please sign in to comment.