/
share-list.go
113 lines (104 loc) · 2.89 KB
/
share-list.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
/*
* Minio Client (C) 2014, 2015 Minio, Inc.
*
* 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 main
import (
"encoding/json"
"time"
"github.com/minio/cli"
"github.com/minio/mc/pkg/console"
"github.com/minio/minio-xl/pkg/probe"
)
// Share documents via URL.
var shareList = cli.Command{
Name: "list",
Usage: "List the shared URLs",
Action: mainShareList,
CustomHelpTemplate: `NAME:
mc share {{.Name}} - {{.Usage}}
USAGE:
mc share {{.Name}}
EXAMPLES:
$ mc share {{.Name}}
`,
}
func mainShareList(ctx *cli.Context) {
shareDataDirSetup()
setSharePalette(ctx.GlobalString("colors"))
err := doShareList()
fatalIf(err.Trace(), "Unable to list shared URLs.")
}
// doShareList list shared url's
func doShareList() *probe.Error {
sURLs, err := loadSharedURLsV3()
if err != nil {
return err.Trace()
}
saveList := newSharedURLsV3()
for _, data := range sURLs.URLs {
if time.Since(data.Date) > data.Message.Expiry {
continue
}
saveList.URLs = append(saveList.URLs, data)
expiry := data.Message.Expiry - time.Since(data.Date)
if !globalJSONFlag {
var kind string
if len(data.Message.DownloadURL) > 0 {
kind = "Download"
} else {
kind = "Upload"
}
msg := console.Colorize("Share", "Name: ")
msg += console.Colorize("URL", data.Message.Key+" ("+kind+")\n")
msg += console.Colorize("Share", "Expiry: ")
msg += console.Colorize("Expires", timeDurationToHumanizedTime(expiry))
msg += "\n"
console.Println(msg)
continue
}
var shareMessageBytes []byte
var err error
s := data.Message
if len(s.DownloadURL) > 0 {
shareMessageBytes, err = json.Marshal(struct {
Expiry humanizedTime `json:"expiry"`
DownloadURL string `json:"downloadUrl"`
Key string `json:"keyName"`
}{
Expiry: timeDurationToHumanizedTime(s.Expiry),
DownloadURL: s.DownloadURL,
Key: s.Key,
})
} else {
shareMessageBytes, err = json.Marshal(struct {
Expiry humanizedTime `json:"expiry"`
UploadInfo map[string]string `json:"uploadInfo"`
Key string `json:"keyName"`
}{
Expiry: timeDurationToHumanizedTime(s.Expiry),
UploadInfo: s.UploadInfo,
Key: s.Key,
})
}
if err != nil {
return probe.NewError(err)
}
console.Println(string(shareMessageBytes))
}
if err := saveSharedURLsV3(saveList); err != nil {
return err.Trace()
}
return nil
}