mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-08 07:23:10 -06:00
mediaapi/writers/download: Comment the flow of respondFromRemoteFile
This commit is contained in:
parent
da28e4a3e2
commit
5e66ad6d39
|
|
@ -122,6 +122,9 @@ func Download(w http.ResponseWriter, req *http.Request, origin types.ServerName,
|
||||||
return
|
return
|
||||||
} else if err == sql.ErrNoRows && r.MediaMetadata.Origin != cfg.ServerName {
|
} else if err == sql.ErrNoRows && r.MediaMetadata.Origin != cfg.ServerName {
|
||||||
// If we do not have a record and the origin is remote, we need to fetch it and respond with that file
|
// If we do not have a record and the origin is remote, we need to fetch it and respond with that file
|
||||||
|
// The following code using activeRemoteRequests is avoiding duplication of fetches from the remote server in the case
|
||||||
|
// of multiple simultaneous incoming requests for the same remote file - it will be downloaded once, cached and served
|
||||||
|
// to all clients.
|
||||||
|
|
||||||
mxcURL := "mxc://" + string(r.MediaMetadata.Origin) + "/" + string(r.MediaMetadata.MediaID)
|
mxcURL := "mxc://" + string(r.MediaMetadata.Origin) + "/" + string(r.MediaMetadata.MediaID)
|
||||||
|
|
||||||
|
|
@ -256,6 +259,7 @@ func respondFromRemoteFile(w http.ResponseWriter, logger *log.Entry, mediaMetada
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
// create request for remote file
|
||||||
urls := getMatrixUrls(mediaMetadata.Origin)
|
urls := getMatrixUrls(mediaMetadata.Origin)
|
||||||
|
|
||||||
logger.Printf("Connecting to remote %q\n", urls[0])
|
logger.Printf("Connecting to remote %q\n", urls[0])
|
||||||
|
|
@ -299,6 +303,7 @@ func respondFromRemoteFile(w http.ResponseWriter, logger *log.Entry, mediaMetada
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// get metadata from request and set metadata on response
|
||||||
contentLength, err := strconv.ParseInt(resp.Header.Get("Content-Length"), 10, 64)
|
contentLength, err := strconv.ParseInt(resp.Header.Get("Content-Length"), 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Warn("Failed to parse content length")
|
logger.Warn("Failed to parse content length")
|
||||||
|
|
@ -324,6 +329,7 @@ func respondFromRemoteFile(w http.ResponseWriter, logger *log.Entry, mediaMetada
|
||||||
" object-src 'self';"
|
" object-src 'self';"
|
||||||
w.Header().Set("Content-Security-Policy", contentSecurityPolicy)
|
w.Header().Set("Content-Security-Policy", contentSecurityPolicy)
|
||||||
|
|
||||||
|
// create the temporary file writer
|
||||||
tmpDir, err := createTempDir(cfg.BasePath)
|
tmpDir, err := createTempDir(cfg.BasePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Infof("Failed to create temp dir %q\n", err)
|
logger.Infof("Failed to create temp dir %q\n", err)
|
||||||
|
|
@ -344,6 +350,8 @@ func respondFromRemoteFile(w http.ResponseWriter, logger *log.Entry, mediaMetada
|
||||||
}
|
}
|
||||||
defer tmpFile.Close()
|
defer tmpFile.Close()
|
||||||
|
|
||||||
|
// read the remote request's response body
|
||||||
|
// simultaneously write it to the incoming request's response body and the temporary file
|
||||||
logger.WithFields(log.Fields{
|
logger.WithFields(log.Fields{
|
||||||
"MediaID": mediaMetadata.MediaID,
|
"MediaID": mediaMetadata.MediaID,
|
||||||
"Origin": mediaMetadata.Origin,
|
"Origin": mediaMetadata.Origin,
|
||||||
|
|
@ -429,9 +437,14 @@ func respondFromRemoteFile(w http.ResponseWriter, logger *log.Entry, mediaMetada
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The file has been fetched. It is moved to its final destination and its metadata is inserted into the database.
|
||||||
|
|
||||||
// Note: After this point we have responded to the client's request and are just dealing with local caching.
|
// Note: After this point we have responded to the client's request and are just dealing with local caching.
|
||||||
// As we have responded with 200 OK, any errors are ineffectual to the client request and so we just log and return.
|
// As we have responded with 200 OK, any errors are ineffectual to the client request and so we just log and return.
|
||||||
|
|
||||||
|
// It's possible the bytesWritten to the temporary file is different to the reported Content-Length from the remote
|
||||||
|
// request's response. bytesWritten is therefore used as it is what would be sent to clients when reading from the local
|
||||||
|
// file.
|
||||||
mediaMetadata.ContentLength = types.ContentLength(bytesWritten)
|
mediaMetadata.ContentLength = types.ContentLength(bytesWritten)
|
||||||
mediaMetadata.UserID = types.MatrixUserID("@:" + string(mediaMetadata.Origin))
|
mediaMetadata.UserID = types.MatrixUserID("@:" + string(mediaMetadata.Origin))
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue