mediaapi: Also rename all basePath variables to absBasePath for clarity

This commit is contained in:
Robert Swain 2017-05-18 17:37:32 +02:00
parent 995e1f2c99
commit 10a2b2f8e6
5 changed files with 20 additions and 20 deletions

View file

@ -64,7 +64,7 @@ func main() {
cfg := &config.MediaAPI{ cfg := &config.MediaAPI{
ServerName: gomatrixserverlib.ServerName(serverName), ServerName: gomatrixserverlib.ServerName(serverName),
BasePath: types.Path(absBasePath), AbsBasePath: types.Path(absBasePath),
MaxFileSizeBytes: types.ContentLength(maxFileSizeBytes), MaxFileSizeBytes: types.ContentLength(maxFileSizeBytes),
DataSource: dataSource, DataSource: dataSource,
} }

View file

@ -23,8 +23,8 @@ import (
type MediaAPI struct { type MediaAPI struct {
// The name of the server. This is usually the domain name, e.g 'matrix.org', 'localhost'. // The name of the server. This is usually the domain name, e.g 'matrix.org', 'localhost'.
ServerName gomatrixserverlib.ServerName `yaml:"server_name"` ServerName gomatrixserverlib.ServerName `yaml:"server_name"`
// The base path to where media files will be stored. // The absolute base path to where media files will be stored.
BasePath types.Path `yaml:"base_path"` AbsBasePath types.Path `yaml:"abs_base_path"`
// The maximum file size in bytes that is allowed to be stored on this server. // The maximum file size in bytes that is allowed to be stored on this server.
// Note that remote files larger than this can still be proxied to a client, they will just not be cached. // Note that remote files larger than this can still be proxied to a client, they will just not be cached.
MaxFileSizeBytes types.ContentLength `yaml:"max_file_size_bytes"` MaxFileSizeBytes types.ContentLength `yaml:"max_file_size_bytes"`

View file

@ -120,7 +120,7 @@ func Download(w http.ResponseWriter, req *http.Request, origin gomatrixserverlib
if err == nil { if err == nil {
// If we have a record, we can respond from the local file // If we have a record, we can respond from the local file
r.MediaMetadata = mediaMetadata r.MediaMetadata = mediaMetadata
r.respondFromLocalFile(w, cfg.BasePath) r.respondFromLocalFile(w, cfg.AbsBasePath)
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
@ -136,7 +136,7 @@ func Download(w http.ResponseWriter, req *http.Request, origin gomatrixserverlib
if err == nil { if err == nil {
// If we have a record, we can respond from the local file // If we have a record, we can respond from the local file
r.MediaMetadata = mediaMetadata r.MediaMetadata = mediaMetadata
r.respondFromLocalFile(w, cfg.BasePath) r.respondFromLocalFile(w, cfg.AbsBasePath)
activeRemoteRequests.Unlock() activeRemoteRequests.Unlock()
return return
} }
@ -166,7 +166,7 @@ func Download(w http.ResponseWriter, req *http.Request, origin gomatrixserverlib
} }
} }
r.respondFromRemoteFile(w, cfg.BasePath, cfg.MaxFileSizeBytes, db, activeRemoteRequests) r.respondFromRemoteFile(w, cfg.AbsBasePath, cfg.MaxFileSizeBytes, db, activeRemoteRequests)
} else { } else {
// If we do not have a record and the origin is local, or if we have another error from the database, the file is not found // If we do not have a record and the origin is local, or if we have another error from the database, the file is not found
r.Logger.Warnln("Failed to look up file in database:", err) r.Logger.Warnln("Failed to look up file in database:", err)
@ -177,7 +177,7 @@ func Download(w http.ResponseWriter, req *http.Request, origin gomatrixserverlib
} }
} }
func (r *downloadRequest) respondFromLocalFile(w http.ResponseWriter, basePath types.Path) { func (r *downloadRequest) respondFromLocalFile(w http.ResponseWriter, absBasePath types.Path) {
r.Logger.WithFields(log.Fields{ r.Logger.WithFields(log.Fields{
"MediaID": r.MediaMetadata.MediaID, "MediaID": r.MediaMetadata.MediaID,
"Origin": r.MediaMetadata.Origin, "Origin": r.MediaMetadata.Origin,
@ -187,7 +187,7 @@ func (r *downloadRequest) respondFromLocalFile(w http.ResponseWriter, basePath t
"Content-Disposition": r.MediaMetadata.ContentDisposition, "Content-Disposition": r.MediaMetadata.ContentDisposition,
}).Infof("Downloading file") }).Infof("Downloading file")
filePath := getPathFromMediaMetadata(r.MediaMetadata, basePath) filePath := getPathFromMediaMetadata(r.MediaMetadata, absBasePath)
file, err := os.Open(filePath) file, err := os.Open(filePath)
if err != nil { if err != nil {
// FIXME: Remove erroneous file from database? // FIXME: Remove erroneous file from database?
@ -351,7 +351,7 @@ func completeRemoteRequest(activeRemoteRequests *types.ActiveRemoteRequests, mxc
activeRemoteRequests.Unlock() activeRemoteRequests.Unlock()
} }
func (r *downloadRequest) commitFileAndMetadata(tmpDir types.Path, basePath types.Path, activeRemoteRequests *types.ActiveRemoteRequests, db *storage.Database, mxcURL string) bool { func (r *downloadRequest) commitFileAndMetadata(tmpDir types.Path, absBasePath types.Path, activeRemoteRequests *types.ActiveRemoteRequests, db *storage.Database, mxcURL string) bool {
updateActiveRemoteRequests := true updateActiveRemoteRequests := true
r.Logger.WithFields(log.Fields{ r.Logger.WithFields(log.Fields{
@ -366,7 +366,7 @@ func (r *downloadRequest) commitFileAndMetadata(tmpDir types.Path, basePath type
// The database is the source of truth so we need to have moved the file first // The database is the source of truth so we need to have moved the file first
err := moveFile( err := moveFile(
types.Path(path.Join(string(tmpDir), "content")), types.Path(path.Join(string(tmpDir), "content")),
types.Path(getPathFromMediaMetadata(r.MediaMetadata, basePath)), types.Path(getPathFromMediaMetadata(r.MediaMetadata, absBasePath)),
) )
if err != nil { if err != nil {
tmpDirErr := os.RemoveAll(string(tmpDir)) tmpDirErr := os.RemoveAll(string(tmpDir))
@ -387,7 +387,7 @@ func (r *downloadRequest) commitFileAndMetadata(tmpDir types.Path, basePath type
// if written to disk, add to db // if written to disk, add to db
err = db.StoreMediaMetadata(r.MediaMetadata) err = db.StoreMediaMetadata(r.MediaMetadata)
if err != nil { if err != nil {
finalDir := path.Dir(getPathFromMediaMetadata(r.MediaMetadata, basePath)) finalDir := path.Dir(getPathFromMediaMetadata(r.MediaMetadata, absBasePath))
finalDirErr := os.RemoveAll(finalDir) finalDirErr := os.RemoveAll(finalDir)
if finalDirErr != nil { if finalDirErr != nil {
r.Logger.Warnf("Failed to remove finalDir (%v): %q\n", finalDir, finalDirErr) r.Logger.Warnf("Failed to remove finalDir (%v): %q\n", finalDir, finalDirErr)
@ -403,7 +403,7 @@ func (r *downloadRequest) commitFileAndMetadata(tmpDir types.Path, basePath type
return updateActiveRemoteRequests return updateActiveRemoteRequests
} }
func (r *downloadRequest) respondFromRemoteFile(w http.ResponseWriter, basePath types.Path, maxFileSizeBytes types.ContentLength, db *storage.Database, activeRemoteRequests *types.ActiveRemoteRequests) { func (r *downloadRequest) respondFromRemoteFile(w http.ResponseWriter, absBasePath types.Path, maxFileSizeBytes types.ContentLength, db *storage.Database, activeRemoteRequests *types.ActiveRemoteRequests) {
r.Logger.WithFields(log.Fields{ r.Logger.WithFields(log.Fields{
"MediaID": r.MediaMetadata.MediaID, "MediaID": r.MediaMetadata.MediaID,
"Origin": r.MediaMetadata.Origin, "Origin": r.MediaMetadata.Origin,
@ -459,7 +459,7 @@ func (r *downloadRequest) respondFromRemoteFile(w http.ResponseWriter, basePath
w.Header().Set("Content-Security-Policy", contentSecurityPolicy) w.Header().Set("Content-Security-Policy", contentSecurityPolicy)
// create the temporary file writer // create the temporary file writer
tmpFileWriter, tmpFile, tmpDir, errorResponse := createTempFileWriter(basePath, r.Logger) tmpFileWriter, tmpFile, tmpDir, errorResponse := createTempFileWriter(absBasePath, r.Logger)
if errorResponse != nil { if errorResponse != nil {
r.jsonErrorResponse(w, *errorResponse) r.jsonErrorResponse(w, *errorResponse)
return return
@ -516,7 +516,7 @@ func (r *downloadRequest) respondFromRemoteFile(w http.ResponseWriter, basePath
r.MediaMetadata.ContentLength = types.ContentLength(bytesWritten) r.MediaMetadata.ContentLength = types.ContentLength(bytesWritten)
r.MediaMetadata.UserID = types.MatrixUserID("@:" + string(r.MediaMetadata.Origin)) r.MediaMetadata.UserID = types.MatrixUserID("@:" + string(r.MediaMetadata.Origin))
updateActiveRemoteRequests = r.commitFileAndMetadata(tmpDir, basePath, activeRemoteRequests, db, mxcURL) updateActiveRemoteRequests = r.commitFileAndMetadata(tmpDir, absBasePath, activeRemoteRequests, db, mxcURL)
// TODO: generate thumbnails // TODO: generate thumbnails

View file

@ -55,8 +55,8 @@ func createFileWriter(directory types.Path, filename types.Filename) (*bufio.Wri
return bufio.NewWriter(file), file, nil return bufio.NewWriter(file), file, nil
} }
func createTempFileWriter(basePath types.Path, logger *log.Entry) (*bufio.Writer, *os.File, types.Path, *util.JSONResponse) { func createTempFileWriter(absBasePath types.Path, logger *log.Entry) (*bufio.Writer, *os.File, types.Path, *util.JSONResponse) {
tmpDir, err := createTempDir(basePath) tmpDir, err := createTempDir(absBasePath)
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)
return nil, nil, "", &util.JSONResponse{ return nil, nil, "", &util.JSONResponse{
@ -75,9 +75,9 @@ func createTempFileWriter(basePath types.Path, logger *log.Entry) (*bufio.Writer
return writer, tmpFile, tmpDir, nil return writer, tmpFile, tmpDir, nil
} }
func getPathFromMediaMetadata(m *types.MediaMetadata, basePath types.Path) string { func getPathFromMediaMetadata(m *types.MediaMetadata, absBasePath types.Path) string {
return path.Join( return path.Join(
string(basePath), string(absBasePath),
string(m.Origin), string(m.Origin),
string(m.MediaID[:3]), string(m.MediaID[:3]),
string(m.MediaID[3:]), string(m.MediaID[3:]),

View file

@ -153,7 +153,7 @@ func Upload(req *http.Request, cfg *config.MediaAPI, db *storage.Database) util.
"Content-Disposition": r.MediaMetadata.ContentDisposition, "Content-Disposition": r.MediaMetadata.ContentDisposition,
}).Info("Uploading file") }).Info("Uploading file")
writer, file, tmpDir, errorResponse := createTempFileWriter(cfg.BasePath, logger) writer, file, tmpDir, errorResponse := createTempFileWriter(cfg.AbsBasePath, logger)
if errorResponse != nil { if errorResponse != nil {
return *errorResponse return *errorResponse
} }
@ -210,7 +210,7 @@ func Upload(req *http.Request, cfg *config.MediaAPI, db *storage.Database) util.
// TODO: generate thumbnails // TODO: generate thumbnails
finalPath := getPathFromMediaMetadata(r.MediaMetadata, cfg.BasePath) finalPath := getPathFromMediaMetadata(r.MediaMetadata, cfg.AbsBasePath)
err = moveFile( err = moveFile(
types.Path(path.Join(string(tmpDir), "content")), types.Path(path.Join(string(tmpDir), "content")),