Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop attachment requirements for media download #646

Merged
merged 11 commits into from
Sep 29, 2023
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 {
clokep marked this conversation as resolved.
Show resolved Hide resolved
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)
Half-Shot marked this conversation as resolved.
Show resolved Hide resolved
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" {
if filename, ok := params["filename"]; ok {
return filename, true
} else {
t.Fatalf("Content Disposition did not have filename")
return "", true
clokep marked this conversation as resolved.
Show resolved Hide resolved
}
}

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)
Half-Shot marked this conversation as resolved.
Show resolved Hide resolved
}
return "", false
clokep marked this conversation as resolved.
Show resolved Hide resolved
}