Fix bug while uploading file
Gitea/ncDocConverter/pipeline/head This commit looks good Details

main v1.1.1
Jonas Letzbor 2023-09-08 14:30:32 +02:00
parent 987bed7177
commit 1cc5378566
Signed by: RPJosh
GPG Key ID: 46D72F589702E55A
2 changed files with 18 additions and 19 deletions

View File

@ -175,7 +175,6 @@ func (job *convertJob) convertFile(sourceFile string, sourceid int, destinationF
logger.Error("Failed to access the convert api: %s", err) logger.Error("Failed to access the convert api: %s", err)
return return
} }
defer res.Body.Close()
if res.StatusCode != 200 { if res.StatusCode != 200 {
body, _ := io.ReadAll(res.Body) body, _ := io.ReadAll(res.Body)
@ -183,23 +182,9 @@ func (job *convertJob) convertFile(sourceFile string, sourceid int, destinationF
return return
} }
uploadClient := http.Client{Timeout: 10 * time.Second} if err := nextcloud.UploadFile(job.ncUser, destinationFile, res.Body); err != nil {
uploadReq, err := http.NewRequest(http.MethodPut, job.ncUser.NextcloudBaseUrl+"/remote.php/dav/files/"+job.ncUser.Username+"/"+destinationFile, res.Body) logger.Error("Failed to upload file %q to nextcloud: %s", destinationFile, err)
if err != nil {
logger.Error("%s", err)
}
uploadReq.SetBasicAuth(job.ncUser.Username, job.ncUser.Password)
uploadReq.Header.Set("Content-Type", "application/binary")
res, err = uploadClient.Do(uploadReq)
if err != nil {
logger.Error("%s", err)
} }
if res.StatusCode != 204 && res.StatusCode != 201 {
logger.Error("Failed to create file %s (#%d)", destinationFile, res.StatusCode)
}
// Status Code 201
res.Body.Close() res.Body.Close()
} }

View File

@ -179,6 +179,9 @@ func ParseSearchResult(result *searchResult, prefix string, sourceDir string) ma
// Delets a file with the given path. // Delets a file with the given path.
// The path has to start at the root level: Ebook/myFolder/file.txt // The path has to start at the root level: Ebook/myFolder/file.txt
func DeleteFile(ncUser *models.NextcloudUser, filePath string) error { func DeleteFile(ncUser *models.NextcloudUser, filePath string) error {
return deleteFile(ncUser, filePath, true)
}
func deleteFile(ncUser *models.NextcloudUser, filePath string, retry bool) error {
client := http.Client{Timeout: 5 * time.Second} client := http.Client{Timeout: 5 * time.Second}
req := getRequest(http.MethodDelete, "files/"+ncUser.Username+"/"+filePath, nil, ncUser) req := getRequest(http.MethodDelete, "files/"+ncUser.Username+"/"+filePath, nil, ncUser)
@ -192,6 +195,13 @@ func DeleteFile(ncUser *models.NextcloudUser, filePath string) error {
return fmt.Errorf("failed to delete file %s (%d)", filePath, res.StatusCode) return fmt.Errorf("failed to delete file %s (%d)", filePath, res.StatusCode)
} }
// If the server is locked try to delete it again
if res.StatusCode == 423 && retry {
logger.Debug("Trying to delete the file again (it was locked previously)")
time.Sleep(10 * time.Millisecond)
return deleteFile(ncUser, filePath, false)
}
return nil return nil
} }
@ -222,8 +232,12 @@ func CreateFoldersRecursively(ncUser *models.NextcloudUser, destinationFile stri
// Uploads a file to the nextcloud server. // Uploads a file to the nextcloud server.
// It will be saved to the destination as a relative path to the nextcloud root (ebook/file.txt). // It will be saved to the destination as a relative path to the nextcloud root (ebook/file.txt).
func UploadFile(ncUser *models.NextcloudUser, destination string, content io.ReadCloser) error { func UploadFile(ncUser *models.NextcloudUser, destination string, content io.ReadCloser) error {
client := http.Client{Timeout: 5 * time.Second} client := http.Client{Timeout: 10 * time.Second}
req := getRequest(http.MethodPut, "files/"+ncUser.Username+"/"+destination, content, ncUser) cnt, err := io.ReadAll(content)
if err != nil {
return fmt.Errorf("failed to read body: %s", err)
}
req := getRequest(http.MethodPut, "files/"+ncUser.Username+"/"+destination, bytes.NewBuffer(cnt), ncUser)
res, err := client.Do(req) res, err := client.Do(req)
if err != nil { if err != nil {