mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-08 07:23:10 -06:00
mediaapi/writers/fileutils: Store files based on hash, not media ID
This avoids having to sanitize the origin and media ID for files from remote servers. It also allows us to deduplicate files across all files uploaded to this homeserver or downloaded from remote homeservers.
This commit is contained in:
parent
979ce964ab
commit
01c565ddfb
|
|
@ -196,28 +196,29 @@ func readAndHashAndWriteWithLimit(reqReader io.Reader, maxFileSizeBytes types.Co
|
||||||
func getPathFromMediaMetadata(m *types.MediaMetadata, absBasePath types.Path) (string, error) {
|
func getPathFromMediaMetadata(m *types.MediaMetadata, absBasePath types.Path) (string, error) {
|
||||||
var subPath, fileName string
|
var subPath, fileName string
|
||||||
|
|
||||||
mediaIDLen := len(m.MediaID)
|
hashLen := len(m.Base64Hash)
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
case mediaIDLen < 1:
|
case hashLen < 1:
|
||||||
return "", fmt.Errorf("Invalid filePath (MediaID too short): %q", m.MediaID)
|
return "", fmt.Errorf("Invalid filePath (Base64Hash too short): %q", m.Base64Hash)
|
||||||
case mediaIDLen < 2:
|
case hashLen > 255:
|
||||||
|
return "", fmt.Errorf("Invalid filePath (Base64Hash too long - max 255 characters): %q", m.Base64Hash)
|
||||||
|
case hashLen < 2:
|
||||||
subPath = ""
|
subPath = ""
|
||||||
fileName = string(m.MediaID)
|
fileName = string(m.Base64Hash)
|
||||||
case mediaIDLen < 3:
|
case hashLen < 3:
|
||||||
subPath = string(m.MediaID[0:1])
|
subPath = string(m.Base64Hash[0:1])
|
||||||
fileName = string(m.MediaID[1:])
|
fileName = string(m.Base64Hash[1:])
|
||||||
default:
|
default:
|
||||||
subPath = path.Join(
|
subPath = path.Join(
|
||||||
string(m.MediaID[0:1]),
|
string(m.Base64Hash[0:1]),
|
||||||
string(m.MediaID[1:2]),
|
string(m.Base64Hash[1:2]),
|
||||||
)
|
)
|
||||||
fileName = string(m.MediaID[2:])
|
fileName = string(m.Base64Hash[2:])
|
||||||
}
|
}
|
||||||
|
|
||||||
filePath, err := filepath.Abs(path.Join(
|
filePath, err := filepath.Abs(path.Join(
|
||||||
string(absBasePath),
|
string(absBasePath),
|
||||||
string(m.Origin),
|
|
||||||
subPath,
|
subPath,
|
||||||
fileName,
|
fileName,
|
||||||
))
|
))
|
||||||
|
|
@ -225,11 +226,6 @@ func getPathFromMediaMetadata(m *types.MediaMetadata, absBasePath types.Path) (s
|
||||||
return "", fmt.Errorf("Unable to construct filePath: %q", err)
|
return "", fmt.Errorf("Unable to construct filePath: %q", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME:
|
|
||||||
// - validate origin
|
|
||||||
// - sanitize mediaID (e.g. '/' characters and such)
|
|
||||||
// - validate length of origin and mediaID according to common filesystem limitations
|
|
||||||
|
|
||||||
// check if the absolute absBasePath is a prefix of the absolute filePath
|
// check if the absolute absBasePath is a prefix of the absolute filePath
|
||||||
// if so, no directory escape has occurred and the filePath is valid
|
// if so, no directory escape has occurred and the filePath is valid
|
||||||
// Note: absBasePath is already absolute
|
// Note: absBasePath is already absolute
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue