-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathshare.go
62 lines (53 loc) · 1.61 KB
/
share.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
package termux
import (
"bytes"
"path/filepath"
)
// ShareType represent the action of the share, defaults to edit
type ShareType = string
const (
// View share type
View ShareType = "view"
// Edit share type
Edit ShareType = "edit"
// Send share type
Send ShareType = "send"
)
func shareParam(title string, useDefault bool, actionType ShareType) map[string]interface{} {
if actionType == "" {
actionType = "edit"
}
return map[string]interface{}{
"title": title,
"default-receiver": useDefault,
"action-type": string(actionType),
}
}
// ShareFile shares a file with MIME type determined by the file extension, useDefault determines whether to use the default app if availables
func ShareFile(title string, path string, useDefault bool, actionType ShareType) error {
param := shareParam(title, useDefault, actionType)
buf := bytes.NewBuffer([]byte{})
realpath, err := filepath.Abs(path)
if err != nil {
return err
}
param["file"] = realpath
if err := exec(nil, buf, "Share", param, ""); err != nil {
return err
}
return checkErr(buf.Bytes())
}
// Share shares raw data bytes with the given content type, useDefault determines whether to use the default app if availables
func Share(title string, data []byte, contentType string, useDefault bool, actionType ShareType) error {
if contentType == "" {
contentType = "text/plain"
}
param := shareParam(title, useDefault, actionType)
param["content-type"] = contentType
in := bytes.NewBuffer(data)
buf := bytes.NewBuffer([]byte{})
if err := exec(in, buf, "Share", param, ""); err != nil {
return err
}
return checkErr(buf.Bytes())
}