-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathuploads.go
65 lines (57 loc) · 2.1 KB
/
uploads.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package pulp_client
import (
"context"
"net/http"
"os"
zest "github.com/content-services/zest/release/v2024"
)
// CreateUpload Creates an upload
func (r *pulpDaoImpl) CreateUpload(ctx context.Context, size int64) (*zest.UploadResponse, int, error) {
ctx, client := getZestClient(ctx)
_, err := r.LookupOrCreateDomain(ctx, r.domainName)
if err != nil {
return nil, 0, err
}
statusCode := http.StatusInternalServerError
upload := zest.Upload{}
upload.Size = size
readResp, httpResp, err := client.UploadsAPI.UploadsCreate(ctx, r.domainName).Upload(upload).Execute()
if httpResp != nil {
statusCode = httpResp.StatusCode
defer httpResp.Body.Close()
}
if err != nil {
return nil, statusCode, errorWithResponseBody("creating upload", httpResp, err)
}
return readResp, statusCode, nil
}
// UploadChunk Uploads a chunk for an upload
func (r *pulpDaoImpl) UploadChunk(ctx context.Context, uploadHref string, contentRange string, file *os.File, sha256 string) (*zest.UploadResponse, int, error) {
ctx, client := getZestClient(ctx)
statusCode := http.StatusInternalServerError
readResp, httpResp, err := client.UploadsAPI.UploadsUpdate(ctx, uploadHref).ContentRange(contentRange).File(file).Sha256(sha256).Execute()
if httpResp != nil {
statusCode = httpResp.StatusCode
defer httpResp.Body.Close()
}
if err != nil {
return &zest.UploadResponse{}, statusCode, errorWithResponseBody("uploading file chunk", httpResp, err)
}
return readResp, statusCode, nil
}
// FinishUpload Finishes an upload
func (r *pulpDaoImpl) FinishUpload(ctx context.Context, uploadHref string, sha256 string) (*zest.AsyncOperationResponse, int, error) {
ctx, client := getZestClient(ctx)
uploadCommit := zest.UploadCommit{}
uploadCommit.Sha256 = sha256
statusCode := http.StatusInternalServerError
readResp, httpResp, err := client.UploadsAPI.UploadsCommit(ctx, uploadHref).UploadCommit(uploadCommit).Execute()
if httpResp != nil {
statusCode = httpResp.StatusCode
defer httpResp.Body.Close()
}
if err != nil {
return nil, statusCode, errorWithResponseBody("finishing upload", httpResp, err)
}
return readResp, statusCode, nil
}