[fix] copy error
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Evan Reichard 2023-10-30 23:35:37 -04:00
parent 20560ed246
commit 0f271ac2fb

View File

@ -4,6 +4,7 @@ import (
"crypto/md5" "crypto/md5"
"database/sql" "database/sql"
"fmt" "fmt"
"io"
"mime/multipart" "mime/multipart"
"net/http" "net/http"
"os" "os"
@ -413,6 +414,7 @@ func (api *API) uploadNewDocument(c *gin.Context) {
errorPage(c, http.StatusInternalServerError, "Unable to create temp file.") errorPage(c, http.StatusInternalServerError, "Unable to create temp file.")
return return
} }
defer os.Remove(tempFile.Name())
defer tempFile.Close() defer tempFile.Close()
// Save Temp // Save Temp
@ -474,12 +476,19 @@ func (api *API) uploadNewDocument(c *gin.Context) {
// Derive & Sanitize File Name // Derive & Sanitize File Name
fileName = "." + filepath.Clean(fmt.Sprintf("/%s [%s]%s", fileName, partialMD5, fileExtension)) fileName = "." + filepath.Clean(fmt.Sprintf("/%s [%s]%s", fileName, partialMD5, fileExtension))
// Generate Storage Path // Generate Storage Path & Open File
safePath := filepath.Join(api.Config.DataPath, "documents", fileName) safePath := filepath.Join(api.Config.DataPath, "documents", fileName)
destFile, err := os.Create(safePath)
if err != nil {
log.Error("[uploadNewDocument] Dest File Error:", err)
errorPage(c, http.StatusInternalServerError, "Unable to save file.")
return
}
defer destFile.Close()
// Move File // Copy File
if err := os.Rename(tempFile.Name(), safePath); err != nil { if _, err = io.Copy(destFile, tempFile); err != nil {
log.Error("[uploadNewDocument] Move Temp File Error:", err) log.Error("[uploadNewDocument] Copy Temp File Error:", err)
errorPage(c, http.StatusInternalServerError, "Unable to save file.") errorPage(c, http.StatusInternalServerError, "Unable to save file.")
return return
} }
@ -770,12 +779,29 @@ func (api *API) saveNewDocument(c *gin.Context) {
// Derive & Sanitize File Name // Derive & Sanitize File Name
fileName = "." + filepath.Clean(fmt.Sprintf("/%s [%s]%s", fileName, partialMD5, fileExtension)) fileName = "." + filepath.Clean(fmt.Sprintf("/%s [%s]%s", fileName, partialMD5, fileExtension))
// Generate Storage Path // Open Source File
safePath := filepath.Join(api.Config.DataPath, "documents", fileName) sourceFile, err := os.Open(tempFilePath)
if err != nil {
log.Error("[saveNewDocument] Source File Error:", err)
errorPage(c, http.StatusInternalServerError, "Unable to save file.")
return
}
defer os.Remove(tempFilePath)
defer sourceFile.Close()
// Move File // Generate Storage Path & Open File
if err := os.Rename(tempFilePath, safePath); err != nil { safePath := filepath.Join(api.Config.DataPath, "documents", fileName)
log.Warn("[saveNewDocument] Move Temp File Error: ", err) destFile, err := os.Create(safePath)
if err != nil {
log.Error("[saveNewDocument] Dest File Error:", err)
errorPage(c, http.StatusInternalServerError, "Unable to save file.")
return
}
defer destFile.Close()
// Copy File
if _, err = io.Copy(destFile, sourceFile); err != nil {
log.Error("[saveNewDocument] Copy Temp File Error:", err)
errorPage(c, http.StatusInternalServerError, "Unable to save file.") errorPage(c, http.StatusInternalServerError, "Unable to save file.")
return return
} }