mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-07 06:53:09 -06:00
mediaapi/writers/utils: Move and reuse createTempFileWriter
This commit is contained in:
parent
abcbb57aa1
commit
074e899000
|
|
@ -15,7 +15,6 @@
|
|||
package writers
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
|
@ -278,27 +277,6 @@ func createRemoteRequest(mediaMetadata *types.MediaMetadata, logger *log.Entry)
|
|||
return resp, nil
|
||||
}
|
||||
|
||||
// FIXME: move to utils and use in upload as well
|
||||
func createTempFileWriter(basePath types.Path, logger *log.Entry) (*bufio.Writer, *os.File, types.Path, *util.JSONResponse) {
|
||||
tmpDir, err := createTempDir(basePath)
|
||||
if err != nil {
|
||||
logger.Infof("Failed to create temp dir %q\n", err)
|
||||
return nil, nil, "", &util.JSONResponse{
|
||||
Code: 400,
|
||||
JSON: jsonerror.Unknown(fmt.Sprintf("Failed to upload: %q", err)),
|
||||
}
|
||||
}
|
||||
tmpFile, writer, err := createFileWriter(tmpDir, "content")
|
||||
if err != nil {
|
||||
logger.Infof("Failed to create file writer %q\n", err)
|
||||
return nil, nil, "", &util.JSONResponse{
|
||||
Code: 400,
|
||||
JSON: jsonerror.Unknown(fmt.Sprintf("Failed to upload: %q", err)),
|
||||
}
|
||||
}
|
||||
return writer, tmpFile, tmpDir, nil
|
||||
}
|
||||
|
||||
// copyToActiveAndPassive works like io.Copy except it copies from the reader to both of the writers
|
||||
// If there is an error with the reader or the active writer, that is considered an error
|
||||
// If there is an error with the passive writer, that is non-critical and copying continues
|
||||
|
|
|
|||
|
|
@ -146,15 +146,10 @@ func Upload(req *http.Request, cfg config.MediaAPI, db *storage.Database) util.J
|
|||
"Content-Disposition": r.MediaMetadata.ContentDisposition,
|
||||
}).Info("Uploading file")
|
||||
|
||||
tmpDir, err := createTempDir(cfg.BasePath)
|
||||
if err != nil {
|
||||
logger.Infof("Failed to create temp dir %q\n", err)
|
||||
return util.JSONResponse{
|
||||
Code: 400,
|
||||
JSON: jsonerror.Unknown(fmt.Sprintf("Failed to upload: %q", err)),
|
||||
}
|
||||
writer, file, tmpDir, errorResponse := createTempFileWriter(cfg.BasePath, logger)
|
||||
if errorResponse != nil {
|
||||
return *errorResponse
|
||||
}
|
||||
file, writer, err := createFileWriter(tmpDir, "content")
|
||||
defer file.Close()
|
||||
|
||||
// The limited reader restricts how many bytes are read from the body to the specified maximum bytes
|
||||
|
|
|
|||
|
|
@ -16,12 +16,15 @@ package writers
|
|||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
|
||||
log "github.com/Sirupsen/logrus"
|
||||
"github.com/matrix-org/dendrite/clientapi/jsonerror"
|
||||
"github.com/matrix-org/dendrite/mediaapi/types"
|
||||
"github.com/matrix-org/util"
|
||||
)
|
||||
|
||||
// createTempDir creates a tmp/<random string> directory within baseDirectory and returns its path
|
||||
|
|
@ -53,6 +56,26 @@ func createFileWriter(directory types.Path, filename types.Filename) (*os.File,
|
|||
return file, bufio.NewWriter(file), nil
|
||||
}
|
||||
|
||||
func createTempFileWriter(basePath types.Path, logger *log.Entry) (*bufio.Writer, *os.File, types.Path, *util.JSONResponse) {
|
||||
tmpDir, err := createTempDir(basePath)
|
||||
if err != nil {
|
||||
logger.Infof("Failed to create temp dir %q\n", err)
|
||||
return nil, nil, "", &util.JSONResponse{
|
||||
Code: 400,
|
||||
JSON: jsonerror.Unknown(fmt.Sprintf("Failed to upload: %q", err)),
|
||||
}
|
||||
}
|
||||
tmpFile, writer, err := createFileWriter(tmpDir, "content")
|
||||
if err != nil {
|
||||
logger.Infof("Failed to create file writer %q\n", err)
|
||||
return nil, nil, "", &util.JSONResponse{
|
||||
Code: 400,
|
||||
JSON: jsonerror.Unknown(fmt.Sprintf("Failed to upload: %q", err)),
|
||||
}
|
||||
}
|
||||
return writer, tmpFile, tmpDir, nil
|
||||
}
|
||||
|
||||
func getPathFromMediaMetadata(m *types.MediaMetadata, basePath types.Path) string {
|
||||
return path.Join(
|
||||
string(basePath),
|
||||
|
|
|
|||
Loading…
Reference in a new issue