-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathshare-createlink.go
More file actions
147 lines (130 loc) · 3.79 KB
/
share-createlink.go
File metadata and controls
147 lines (130 loc) · 3.79 KB
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
// Copyright © 2017 Dropbox, Inc.
// Author: Daniel Porteous
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd
import (
"fmt"
"io/ioutil"
"os"
"os/user"
"path"
"path/filepath"
"strings"
"github.com/dropbox/dropbox-sdk-go-unofficial/dropbox/sharing"
"github.com/spf13/cobra"
)
/**
** Try to get the share link for a file if it already exists.
** If it doesn't make a new share link for it.
*/
func getShareLink(cmd *cobra.Command, args []string) (err error) {
if len(args) != 1 {
printShareLinkUsage()
return
}
dbx := sharing.New(config)
path, err := filepath.Abs(args[0])
if err != nil {
return
}
// Confirm that the file exists.
exists, err := exists(path)
if !exists || err != nil {
print("The file / folder specified does not exist.\n")
return
}
// Try to get a link if it already exists.
if getExistingLink(dbx, path) {
return
}
// print("File / folder does not yet have a sharelink, creating one...\n")
// The file had no share link, let's get it.
getNewLink(dbx, path)
return
}
func printShareLinkUsage() {
fmt.Printf("Usage: %s share getlink [file / folder path]\n", os.Args[0])
}
/*
** Try to get an existing share link for a file / folder.
** It returns true if the file / folder had a link. Otherwise it returns false.
*/
func getExistingLink(dbx sharing.Client, path string) bool {
// Remove the Dropbox folder from the start.
path = strings.Replace(path, getDropboxFolder(), "", 1)
arg := sharing.ListSharedLinksArg{Path: path}
// This method can be called with a path and just get that share link.
res, err := dbx.ListSharedLinks(&arg)
if err != nil || len(res.Links) == 0 {
} else {
printLinks(res.Links)
return true
}
return false
}
/*
** Create and print a link for file / folder that doesn't yet have one.
**
** CreateSharedLinkWithSettings doesn't allow pending uploads,
** so we use the partially deprecated CreateSharedLink.
*/
func getNewLink(dbx sharing.Client, path string) bool {
arg := sharing.NewCreateSharedLinkArg(strings.Replace(path, getDropboxFolder(), "", 1))
// Get the sharelink even if the file isn't fully uploaded yet.
arg.PendingUpload = new(sharing.PendingUploadMode)
// Determine whether the target is a file or folder.
fi, err := os.Stat(path)
if err != nil {
fmt.Println(err)
return false
}
switch mode := fi.Mode(); {
case mode.IsDir():
arg.PendingUpload.Tag = sharing.PendingUploadModeFolder
case mode.IsRegular():
arg.PendingUpload.Tag = sharing.PendingUploadModeFile
}
res, err := dbx.CreateSharedLink(arg)
if err != nil {
fmt.Printf("%+v\n", err)
return false
}
fmt.Printf("%s\t%s\n", res.Path[1:], res.Url)
return true
}
/* Return the path of the Dropbox folder. */
func getDropboxFolder() string {
// I should be using a JSON parser here but it's a pain in Go.
usr, _ := user.Current()
homedir := usr.HomeDir
infoFilePath := path.Join(homedir, ".dropbox/info.json")
raw, err := ioutil.ReadFile(infoFilePath)
if err != nil {
print("Couldn't find Dropbox folder")
return ""
}
// This is obviously dirty.
return strings.Split(strings.Split(string(raw), "\"path\": \"")[1], "\"")[0]
}
/* Check whether a file / folder exists. */
func exists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return true, err
}