mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-07 23:13:11 -06:00
mediaapi/writers/fileutils: Return errors to log using request context
This commit is contained in:
parent
5dd90fbff3
commit
cdd4222e45
|
|
@ -388,6 +388,7 @@ func (r *downloadRequest) commitFileAndMetadata(tmpDir types.Path, absBasePath t
|
||||||
types.Path(finalPath),
|
types.Path(finalPath),
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
r.Logger.Warnf("Failed to move file to final destination: %q\n", err)
|
||||||
tmpDirErr := os.RemoveAll(string(tmpDir))
|
tmpDirErr := os.RemoveAll(string(tmpDir))
|
||||||
if tmpDirErr != nil {
|
if tmpDirErr != nil {
|
||||||
r.Logger.Warnf("Failed to remove tmpDir (%v): %q\n", tmpDir, tmpDirErr)
|
r.Logger.Warnf("Failed to remove tmpDir (%v): %q\n", tmpDir, tmpDirErr)
|
||||||
|
|
|
||||||
|
|
@ -33,13 +33,11 @@ import (
|
||||||
func createTempDir(baseDirectory types.Path) (types.Path, error) {
|
func createTempDir(baseDirectory types.Path) (types.Path, error) {
|
||||||
baseTmpDir := path.Join(string(baseDirectory), "tmp")
|
baseTmpDir := path.Join(string(baseDirectory), "tmp")
|
||||||
if err := os.MkdirAll(baseTmpDir, 0770); err != nil {
|
if err := os.MkdirAll(baseTmpDir, 0770); err != nil {
|
||||||
log.Printf("Failed to create base temp dir: %v\n", err)
|
return "", fmt.Errorf("Failed to create base temp dir: %v", err)
|
||||||
return "", err
|
|
||||||
}
|
}
|
||||||
tmpDir, err := ioutil.TempDir(baseTmpDir, "")
|
tmpDir, err := ioutil.TempDir(baseTmpDir, "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Failed to create temp dir: %v\n", err)
|
return "", fmt.Errorf("Failed to create temp dir: %v", err)
|
||||||
return "", err
|
|
||||||
}
|
}
|
||||||
return types.Path(tmpDir), nil
|
return types.Path(tmpDir), nil
|
||||||
}
|
}
|
||||||
|
|
@ -50,8 +48,7 @@ func createFileWriter(directory types.Path, filename types.Filename) (*bufio.Wri
|
||||||
filePath := path.Join(string(directory), string(filename))
|
filePath := path.Join(string(directory), string(filename))
|
||||||
file, err := os.Create(filePath)
|
file, err := os.Create(filePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Failed to create file: %v\n", err)
|
return nil, nil, fmt.Errorf("Failed to create file: %v", err)
|
||||||
return nil, nil, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return bufio.NewWriter(file), file, nil
|
return bufio.NewWriter(file), file, nil
|
||||||
|
|
@ -60,7 +57,7 @@ func createFileWriter(directory types.Path, filename types.Filename) (*bufio.Wri
|
||||||
func createTempFileWriter(absBasePath 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(absBasePath)
|
tmpDir, err := createTempDir(absBasePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Infof("Failed to create temp dir %q\n", err)
|
logger.Warnf("Failed to create temp dir: %q\n", err)
|
||||||
return nil, nil, "", &util.JSONResponse{
|
return nil, nil, "", &util.JSONResponse{
|
||||||
Code: 400,
|
Code: 400,
|
||||||
JSON: jsonerror.Unknown(fmt.Sprintf("Failed to upload")),
|
JSON: jsonerror.Unknown(fmt.Sprintf("Failed to upload")),
|
||||||
|
|
@ -68,7 +65,7 @@ func createTempFileWriter(absBasePath types.Path, logger *log.Entry) (*bufio.Wri
|
||||||
}
|
}
|
||||||
writer, tmpFile, err := createFileWriter(tmpDir, "content")
|
writer, tmpFile, err := createFileWriter(tmpDir, "content")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Infof("Failed to create file writer %q\n", err)
|
logger.Warnf("Failed to create file writer: %q\n", err)
|
||||||
return nil, nil, "", &util.JSONResponse{
|
return nil, nil, "", &util.JSONResponse{
|
||||||
Code: 400,
|
Code: 400,
|
||||||
JSON: jsonerror.Unknown(fmt.Sprintf("Failed to upload")),
|
JSON: jsonerror.Unknown(fmt.Sprintf("Failed to upload")),
|
||||||
|
|
@ -126,13 +123,11 @@ func moveFile(src types.Path, dst types.Path) error {
|
||||||
|
|
||||||
err := os.MkdirAll(dstDir, 0770)
|
err := os.MkdirAll(dstDir, 0770)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Failed to make directory: %q", err)
|
return fmt.Errorf("Failed to make directory: %q", err)
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
err = os.Rename(string(src), string(dst))
|
err = os.Rename(string(src), string(dst))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Failed to move directory: %q", err)
|
return fmt.Errorf("Failed to move directory: %q", err)
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue