Fix bug while uploading file
Gitea/ncDocConverter/pipeline/head This commit looks good
Details
Gitea/ncDocConverter/pipeline/head This commit looks good
Details
parent
987bed7177
commit
1cc5378566
|
@ -175,7 +175,6 @@ func (job *convertJob) convertFile(sourceFile string, sourceid int, destinationF
|
|||
logger.Error("Failed to access the convert api: %s", err)
|
||||
return
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
if res.StatusCode != 200 {
|
||||
body, _ := io.ReadAll(res.Body)
|
||||
|
@ -183,23 +182,9 @@ func (job *convertJob) convertFile(sourceFile string, sourceid int, destinationF
|
|||
return
|
||||
}
|
||||
|
||||
uploadClient := http.Client{Timeout: 10 * time.Second}
|
||||
uploadReq, err := http.NewRequest(http.MethodPut, job.ncUser.NextcloudBaseUrl+"/remote.php/dav/files/"+job.ncUser.Username+"/"+destinationFile, res.Body)
|
||||
|
||||
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 err := nextcloud.UploadFile(job.ncUser, destinationFile, res.Body); err != nil {
|
||||
logger.Error("Failed to upload file %q to nextcloud: %s", destinationFile, 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()
|
||||
}
|
||||
|
|
|
@ -179,6 +179,9 @@ func ParseSearchResult(result *searchResult, prefix string, sourceDir string) ma
|
|||
// Delets a file with the given path.
|
||||
// The path has to start at the root level: Ebook/myFolder/file.txt
|
||||
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}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
|
@ -222,8 +232,12 @@ func CreateFoldersRecursively(ncUser *models.NextcloudUser, destinationFile stri
|
|||
// 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).
|
||||
func UploadFile(ncUser *models.NextcloudUser, destination string, content io.ReadCloser) error {
|
||||
client := http.Client{Timeout: 5 * time.Second}
|
||||
req := getRequest(http.MethodPut, "files/"+ncUser.Username+"/"+destination, content, ncUser)
|
||||
client := http.Client{Timeout: 10 * time.Second}
|
||||
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)
|
||||
if err != nil {
|
||||
|
|
Loading…
Reference in New Issue