-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathimport.go
103 lines (95 loc) · 2.86 KB
/
import.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package elevengo
import (
"context"
"crypto/sha1"
"fmt"
"io"
"github.com/deadblue/elevengo/internal/crypto/hash"
"github.com/deadblue/elevengo/internal/util"
"github.com/deadblue/elevengo/lowlevel/api"
"github.com/deadblue/elevengo/lowlevel/errors"
)
type ErrImportNeedCheck struct {
// The sign key your should set to ImportTicket
SignKey string
// The sign range in format of "<start>-<end>" in bytes.
// You can directly use it in ImportCreateTicket.
SignRange string
}
func (e *ErrImportNeedCheck) Error() string {
return "import requires sign check"
}
// ImportTicket container reqiured fields to import(aka. quickly upload) a file
// to your 115 cloud storage.
type ImportTicket struct {
// File base name
FileName string
// File size in bytes
FileSize int64
// File SHA-1 hash, in upper-case HEX format
FileSha1 string
// Sign key from 115 server.
SignKey string
// SHA-1 hash value of a segment of the file, in upper-case HEX format
SignValue string
}
// Import imports(aka. rapid-upload) a file to your 115 cloud storage.
// Please check example code for the detailed usage.
func (a *Agent) Import(dirId string, ticket *ImportTicket) (err error) {
spec := (&api.UploadInitSpec{}).Init(
dirId, ticket.FileSha1, ticket.FileName, ticket.FileSize,
ticket.SignKey, ticket.SignValue, &a.common,
)
if err = a.llc.CallApi(spec, context.Background()); err != nil {
return
}
if spec.Result.SignCheck != "" {
err = &ErrImportNeedCheck{
SignKey: spec.Result.SignKey,
SignRange: spec.Result.SignCheck,
}
} else if !spec.Result.Exists {
err = errors.ErrNotExist
}
return
}
// ImportCreateTicket is a helper function to create an ImportTicket of a file,
// that you can share to others to import this file to their cloud storage.
// You should also send pickcode together with ticket.
func (a *Agent) ImportCreateTicket(fileId string, ticket *ImportTicket) (pickcode string, err error) {
file := &File{}
if err = a.FileGet(fileId, file); err == nil {
pickcode = file.PickCode
if ticket != nil {
ticket.FileName = file.Name
ticket.FileSize = file.Size
ticket.FileSha1 = file.Sha1
}
}
return
}
// ImportCalculateSignValue calculates sign value of a file on cloud storage.
// Please check example code for the detailed usage.
func (a *Agent) ImportCalculateSignValue(pickcode string, signRange string) (value string, err error) {
// Parse range text at first
var start, end int64
if _, err = fmt.Sscanf(signRange, "%d-%d", &start, &end); err != nil {
return
}
// Get download URL
ticket := &DownloadTicket{}
if err = a.DownloadCreateTicket(pickcode, ticket); err != nil {
return
}
// Get range content
var body io.ReadCloser
if body, err = a.FetchRange(ticket.Url, Range{start, end}); err != nil {
return
}
defer util.QuietlyClose(body)
h := sha1.New()
if _, err = io.Copy(h, body); err == nil {
value = hash.ToHexUpper(h)
}
return
}