-
Notifications
You must be signed in to change notification settings - Fork 0
/
share.go
160 lines (142 loc) · 4.25 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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"errors"
"fmt"
"net/url"
"os"
"strings"
"time"
"github.com/atotto/clipboard"
"github.com/olebedev/when"
)
type share struct {
pathConfig
Clipboard bool `kong:"help='copy share url into systems clipboard',short=c"`
Expire string `kong:"help='expire date of this share, truncated to days'"`
Note string `kong:"help='note displayed together with the shared file'"`
With string `kong:"help='user to share with',xor=to"`
WithGroup string `kong:"help='group to share with',xor=to"`
Email string `kong:"help='send email with shared file to this email',xor=to"`
Edit bool `kong:"help='edit permission on the shared file'"`
Upload bool `kong:"help='allow uploads to folder using public link'"`
}
func fmtExpiry(expiry string) string {
return strings.TrimSuffix(expiry, " 00:00:00")
}
func (s *share) Help() string {
return `
Shares a file/folder identified by local path.
By default it's a public share and url with the shared file is printed to stdout.
`
}
func (s *share) Run() error {
v := url.Values{}
v.Set("shareType", "3") // public link
v.Set("path", s.remoteFile)
v.Set("note", s.Note)
if s.Email != "" {
v.Set("shareType", "4") // email
v.Set("shareWith", s.Email)
}
if s.With != "" {
v.Set("shareType", "0") // user
v.Set("shareWith", s.With)
}
if s.WithGroup != "" {
v.Set("shareType", "1") // user
v.Set("shareWith", s.WithGroup)
}
if s.Expire != "" {
res, err := when.EN.Parse(s.Expire, time.Now())
if err != nil {
return fmt.Errorf("parsing time: %w", err)
}
if res == nil {
return errors.New("failed to parse expire time")
}
v.Set("expireDate", res.Time.Format(time.RFC3339))
}
if s.Edit {
v.Set("permissions", "15")
}
if s.Upload {
v.Set("publicUpload", "true")
}
data, err := request[sharedFile](s.account, "POST", v)
if err != nil {
return err
}
if data.Expiration != "" {
fmt.Fprintln(os.Stderr, "share expires on:", fmtExpiry(data.Expiration))
}
if s.With == "" && s.WithGroup == "" {
fmt.Println(data.fmtShareLink(s.url))
}
if s.Clipboard {
return clipboard.WriteAll(data.URL)
}
return nil
}
type sharedFile struct {
Text string `xml:",chardata"`
ID string `xml:"id"`
ShareType int `xml:"share_type"`
UidOwner string `xml:"uid_owner"`
DisplaynameOwner string `xml:"displayname_owner"`
Permissions string `xml:"permissions"`
CanEdit string `xml:"can_edit"`
CanDelete string `xml:"can_delete"`
Stime string `xml:"stime"`
Parent string `xml:"parent"`
Expiration string `xml:"expiration"`
Token string `xml:"token"`
UidFileOwner string `xml:"uid_file_owner"`
Note string `xml:"note"`
Label string `xml:"label"`
DisplaynameFileOwner string `xml:"displayname_file_owner"`
Path string `xml:"path"`
ItemType string `xml:"item_type"`
Mimetype string `xml:"mimetype"`
HasPreview string `xml:"has_preview"`
StorageID string `xml:"storage_id"`
Storage string `xml:"storage"`
ItemSource string `xml:"item_source"`
FileSource string `xml:"file_source"`
FileParent string `xml:"file_parent"`
FileTarget string `xml:"file_target"`
ShareWith string `xml:"share_with"`
ShareWithDisplayname string `xml:"share_with_displayname"`
Password string `xml:"password"`
SendPasswordByTalk string `xml:"send_password_by_talk"`
URL string `xml:"url"`
MailSend string `xml:"mail_send"`
HideDownload string `xml:"hide_download"`
Attributes string `xml:"attributes"`
}
func (s *sharedFile) fmtShareLink(serverUrl string) string {
if s.URL != "" || s.Token == "" {
return s.URL
}
// TODO: this may not work on servers without rewrite rules
p, _ := url.JoinPath(serverUrl, "s", s.Token)
return p
}
func (s *sharedFile) fmtNote() string {
note := s.Note
suffix := ""
switch s.ShareType {
case 0:
suffix = "user"
case 1:
suffix = "group"
case 4:
suffix = "email"
}
if suffix != "" {
if note != "" {
note += ", "
}
note = note + suffix + ": " + s.ShareWith
}
return note
}