mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-08 07:23:10 -06:00
mediaapi/writers/utils: Move and reuse createTempFileWriter
This commit is contained in:
parent
abcbb57aa1
commit
074e899000
|
|
@ -15,7 +15,6 @@
|
||||||
package writers
|
package writers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
@ -278,27 +277,6 @@ func createRemoteRequest(mediaMetadata *types.MediaMetadata, logger *log.Entry)
|
||||||
return resp, nil
|
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
|
// 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 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
|
// 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,
|
"Content-Disposition": r.MediaMetadata.ContentDisposition,
|
||||||
}).Info("Uploading file")
|
}).Info("Uploading file")
|
||||||
|
|
||||||
tmpDir, err := createTempDir(cfg.BasePath)
|
writer, file, tmpDir, errorResponse := createTempFileWriter(cfg.BasePath, logger)
|
||||||
if err != nil {
|
if errorResponse != nil {
|
||||||
logger.Infof("Failed to create temp dir %q\n", err)
|
return *errorResponse
|
||||||
return util.JSONResponse{
|
|
||||||
Code: 400,
|
|
||||||
JSON: jsonerror.Unknown(fmt.Sprintf("Failed to upload: %q", err)),
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
file, writer, err := createFileWriter(tmpDir, "content")
|
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
// The limited reader restricts how many bytes are read from the body to the specified maximum bytes
|
// The limited reader restricts how many bytes are read from the body to the specified maximum bytes
|
||||||
|
|
|
||||||
|
|
@ -16,12 +16,15 @@ package writers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
|
||||||
log "github.com/Sirupsen/logrus"
|
log "github.com/Sirupsen/logrus"
|
||||||
|
"github.com/matrix-org/dendrite/clientapi/jsonerror"
|
||||||
"github.com/matrix-org/dendrite/mediaapi/types"
|
"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
|
// 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
|
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 {
|
func getPathFromMediaMetadata(m *types.MediaMetadata, basePath types.Path) string {
|
||||||
return path.Join(
|
return path.Join(
|
||||||
string(basePath),
|
string(basePath),
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue