Break share main file into subcommands files - upload, download, list

This commit is contained in:
Krishna Srinivas
2015-10-08 13:43:41 -07:00
parent 7ec74a33ce
commit aa53fb19f9
6 changed files with 493 additions and 337 deletions
+1 -1
View File
@@ -86,7 +86,7 @@ func (c *s3Client) ShareDownload(expires time.Duration) (string, *probe.Error) {
return presignedURL, nil
}
func (f *s3Client) ShareUpload(recursive bool, expires time.Duration, contentType string) (map[string]string, *probe.Error) {
func (c *s3Client) ShareUpload(recursive bool, expires time.Duration, contentType string) (map[string]string, *probe.Error) {
return nil, probe.NewError(client.APINotImplemented{API: "Share", APIType: "filesystem"})
}
+144
View File
@@ -0,0 +1,144 @@
/*
* 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 (
"errors"
"time"
"github.com/minio/cli"
"github.com/minio/mc/pkg/client"
"github.com/minio/minio/pkg/probe"
)
// Share documents via URL.
var shareDownload = cli.Command{
Name: "download",
Usage: "Share documents via URL.",
Action: mainShareDownload,
CustomHelpTemplate: `NAME:
mc share {{.Name}} - {{.Usage}}
USAGE:
mc share {{.Name}} download TARGET [DURATION]
DURATION = NN[h|m|s] [DEFAULT=168h]
EXAMPLES:
1. Generate URL for sharing, with a default expiry of 7 days.
$ mc share {{.Name}} https://s3.amazonaws.com/backup/2006-Mar-1/backup.tar.gz
2. Generate URL for sharing, with an expiry of 10 minutes.
$ mc share {{.Name}} https://s3.amazonaws.com/backup/2006-Mar-1/backup.tar.gz 10m
3. Generate list of URLs for sharing a folder recursively, with expiration of 5 days each.
$ mc share {{.Name}} https://s3.amazonaws.com/backup... 120h
4. Generate URL with space characters for sharing, with an expiry of 5 seconds.
$ mc share {{.Name}} s3/miniocloud/nothing-like-anything 5s
`,
}
func checkShareDownloadSyntax(ctx *cli.Context) {
args := ctx.Args()
if !args.Present() || args.First() == "help" {
cli.ShowCommandHelpAndExit(ctx, "download", 1) // last argument is exit code
}
if len(args) > 2 {
cli.ShowCommandHelpAndExit(ctx, "download", 1) // last argument is exit code
}
}
func mainShareDownload(ctx *cli.Context) {
shareDataDirSetup()
checkShareDownloadSyntax(ctx)
args := ctx.Args()
config := mustGetMcConfig()
url := args.Get(0)
// default expiration is 7days
expires := time.Duration(604800) * time.Second
if len(args) == 2 {
var err error
expires, err = time.ParseDuration(args.Get(1))
fatalIf(probe.NewError(err), "Unable to parse time argument.")
}
targetURL := getAliasURL(url, config.Aliases)
setSharePalette(ctx.GlobalString("colors"))
// if recursive strip off the "..."
err := doShareDownloadURL(stripRecursiveURL(targetURL), isURLRecursive(targetURL), expires)
fatalIf(err.Trace(targetURL), "Unable to generate URL for sharing.")
return
}
// doShareURL share files from target
func doShareDownloadURL(targetURL string, recursive bool, expires time.Duration) *probe.Error {
shareDate := time.Now().UTC()
sURLs, err := loadSharedURLsV3()
if err != nil {
return err.Trace()
}
var clnt client.Client
clnt, err = target2Client(targetURL)
if err != nil {
return err.Trace()
}
if expires.Seconds() < 1 {
return probe.NewError(errors.New("Too low expires, expiration cannot be less than 1 second."))
}
if expires.Seconds() > 604800 {
return probe.NewError(errors.New("Too high expires, expiration cannot be larger than 7 days."))
}
for contentCh := range clnt.List(recursive) {
if contentCh.Err != nil {
return contentCh.Err.Trace()
}
var newClnt client.Client
newClnt, err = url2Client(getNewTargetURL(clnt.URL(), contentCh.Content.Name))
if err != nil {
return err.Trace()
}
var sharedURL string
sharedURL, err = newClnt.ShareDownload(expires)
if err != nil {
return err.Trace()
}
shareMessage := ShareMessage{
Expiry: expires,
DownloadURL: sharedURL,
Key: newClnt.URL().String(),
}
shareMessageV3 := ShareMessageV3{
Expiry: expires,
DownloadURL: sharedURL,
Key: newClnt.URL().String(),
}
sURLs.URLs = append(sURLs.URLs, struct {
Date time.Time
Message ShareMessageV3
}{
Date: shareDate,
Message: shareMessageV3,
})
Prints("%s\n", shareMessage)
}
saveSharedURLsV3(sURLs)
return nil
}
+113
View File
@@ -0,0 +1,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/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
}
+35 -332
View File
@@ -19,13 +19,9 @@ package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"path/filepath"
"strings"
"time"
"github.com/fatih/color"
"github.com/minio/cli"
"github.com/minio/mc/pkg/client"
"github.com/minio/mc/pkg/console"
@@ -37,85 +33,77 @@ var shareCmd = cli.Command{
Name: "share",
Usage: "Share documents via URL.",
Action: mainShare,
Subcommands: []cli.Command{
shareDownload,
shareUpload,
shareList,
},
CustomHelpTemplate: `NAME:
mc {{.Name}} - {{.Usage}}
{{.Name}} - {{.Usage}}
USAGE:
mc {{.Name}} download TARGET [DURATION]
mc {{.Name}} upload TARGET [DURATION] [Content-Type]
{{.Name}} command [arguments...]
DURATION = NN[h|m|s] [DEFAULT=168h]
EXAMPLES:
1. Generate URL for sharing, with a default expiry of 7 days.
$ mc {{.Name}} https://s3.amazonaws.com/backup/2006-Mar-1/backup.tar.gz
2. Generate URL for sharing, with an expiry of 10 minutes.
$ mc {{.Name}} https://s3.amazonaws.com/backup/2006-Mar-1/backup.tar.gz 10m
3. Generate list of URLs for sharing a folder recursively, with expiration of 5 days each.
$ mc {{.Name}} https://s3.amazonaws.com/backup... 120h
4. Generate URL with space characters for sharing, with an expiry of 5 seconds.
$ mc {{.Name}} s3/miniocloud/nothing-like-anything 5s
5. List all the shared URLs which have not expired yet.
$ mc {{.Name}} list
COMMANDS:
{{range .Commands}}{{ .Name }}{{ "\t" }}{{.Usage}}
{{end}}
`,
}
// ShareMessage is container for share command on success and failure messages
// ShareMessageV1 ...
type ShareMessageV1 struct {
Expiry time.Duration `json:"expiry"`
URL string `json:"url"`
Key string `json:"keyName"`
}
// ShareMessageV2 ...
type ShareMessageV2 ShareMessageV1
// ShareMessageV3 ...
type ShareMessageV3 struct {
Expiry time.Duration `json:"expiry"`
DownloadUrl string `json:"downloadUrl,omitempty"`
DownloadURL string `json:"downloadUrl,omitempty"`
UploadInfo map[string]string `json:"uploadInfo,omitempty"`
Key string `json:"keyName"`
}
// ShareMessage ...
type ShareMessage ShareMessageV3
// String - regular colorized message
func (s ShareMessage) String() string {
if len(s.DownloadUrl) > 0 {
return console.Colorize("Share", fmt.Sprintf("Expiry: %s\n DownloadUrl: %s\n Key: %s", timeDurationToHumanizedTime(s.Expiry), s.DownloadUrl, s.Key))
} else {
var key string
URL := client.NewURL(s.Key)
postUrl := URL.Scheme + URL.SchemeSeparator + URL.Host + string(URL.Separator) + s.UploadInfo["bucket"] + " "
curlCommand := "curl " + postUrl
for k, v := range s.UploadInfo {
if k == "key" {
key = v
continue
}
curlCommand = curlCommand + fmt.Sprintf("-F %s=%s ", k, v)
}
curlCommand = curlCommand + fmt.Sprintf("-F key=%s ", key) + "-F file=@<LOCALFILEPATH> "
return console.Colorize("Share", fmt.Sprintf("Expiry: %s\n curl-command: %s\n Key: %s", timeDurationToHumanizedTime(s.Expiry), curlCommand, s.Key))
if len(s.DownloadURL) > 0 {
return console.Colorize("Share", fmt.Sprintf("Expiry: %s\n DownloadURL: %s\n Key: %s", timeDurationToHumanizedTime(s.Expiry), s.DownloadURL, s.Key))
}
var key string
URL := client.NewURL(s.Key)
postURL := URL.Scheme + URL.SchemeSeparator + URL.Host + string(URL.Separator) + s.UploadInfo["bucket"] + " "
curlCommand := "curl " + postURL
for k, v := range s.UploadInfo {
if k == "key" {
key = v
continue
}
curlCommand = curlCommand + fmt.Sprintf("-F %s=%s ", k, v)
}
curlCommand = curlCommand + fmt.Sprintf("-F key=%s ", key) + "-F file=@<LOCALFILEPATH> "
return console.Colorize("Share", fmt.Sprintf("Expiry: %s\n curl-command: %s\n Key: %s", timeDurationToHumanizedTime(s.Expiry), curlCommand, s.Key))
}
// JSON json message for share command
func (s ShareMessage) JSON() string {
var shareMessageBytes []byte
var err error
if len(s.DownloadUrl) > 0 {
if len(s.DownloadURL) > 0 {
shareMessageBytes, err = json.Marshal(struct {
Expiry humanizedTime `json:"expiry"`
DownloadUrl string `json:"downloadUrl"`
DownloadURL string `json:"downloadUrl"`
Key string `json:"keyName"`
}{
Expiry: timeDurationToHumanizedTime(s.Expiry),
DownloadUrl: s.DownloadUrl,
DownloadURL: s.DownloadURL,
Key: s.Key,
})
} else {
@@ -138,294 +126,9 @@ func (s ShareMessage) JSON() string {
return string(shareMessageBytes)
}
// migrateSharedURLs migrate to newest version sequentially
func migrateSharedURLs() {
migrateSharedURLsV1ToV2()
migrateSharedURLsV2ToV3()
}
func checkShareSyntax(ctx *cli.Context) {
if !ctx.Args().Present() || ctx.Args().First() == "help" {
cli.ShowCommandHelpAndExit(ctx, "share", 1) // last argument is exit code
}
if ctx.Args().Get(0) == "list" {
if len(ctx.Args()) >= 2 {
cli.ShowCommandHelpAndExit(ctx, "share", 1) // last argument is exit code
}
}
if strings.TrimSpace(ctx.Args().Get(0)) == "" {
fatalIf(errInvalidArgument().Trace(), "Unable to validate empty argument.")
}
}
func setSharePalette(style string) {
console.SetCustomPalette(map[string]*color.Color{
"Share": color.New(color.FgGreen, color.Bold),
"Expires": color.New(color.FgRed, color.Bold),
"URL": color.New(color.FgCyan, color.Bold),
})
if style == "light" {
console.SetCustomPalette(map[string]*color.Color{
"Share": color.New(color.FgWhite, color.Bold),
"Expires": color.New(color.FgWhite, color.Bold),
"URL": color.New(color.FgWhite, color.Bold),
})
return
}
/// Add more styles here
if style == "nocolor" {
// All coloring options exhausted, setting nocolor safely
console.SetNoColor()
}
}
// mainShare - is a handler for mc share command
func mainShare(ctx *cli.Context) {
checkShareSyntax(ctx)
if !isSharedURLsDataDirExists() {
shareDir, err := getSharedURLsDataDir()
fatalIf(err.Trace(), "Unable to get shared URL data directory")
fatalIf(createSharedURLsDataDir().Trace(), "Unable to create shared URL data directory ‘"+shareDir+"’.")
}
if !isSharedURLsDataFileExists() {
shareFile, err := getSharedURLsDataFile()
fatalIf(err.Trace(), "Unable to get shared URL data file")
fatalIf(createSharedURLsDataFile().Trace(), "Unable to create shared URL data file ‘"+shareFile+"’.")
}
setSharePalette(ctx.GlobalString("colors"))
args := ctx.Args()
config := mustGetMcConfig()
// if its only list, return quickly
if args.Get(0) == "list" {
err := doShareList()
fatalIf(err.Trace(), "Unable to list shared URLs.")
return
}
if args.Get(0) == "upload" {
var expires time.Duration
var err error
url := args.Get(1)
if len(args.Get(2)) == 0 {
expires = time.Duration(604800) * time.Second
} else {
expires, err = time.ParseDuration(args.Get(2))
if err != nil {
fatalIf(probe.NewError(err), "Unable to parse time argument.")
}
}
contentType := args.Get(3)
targetURL := getAliasURL(url, config.Aliases)
e := doShareUploadURL(stripRecursiveURL(targetURL), isURLRecursive(targetURL), expires, contentType)
fatalIf(e.Trace(targetURL), "Unable to generate URL for sharing.")
return
} else if args.Get(0) == "download" {
/// get first and last arguments
url := args.Get(1) // url to be shared
// default expiration is 7days
expires := time.Duration(604800) * time.Second
if len(args) == 3 {
var err error
expires, err = time.ParseDuration(args.Get(2))
fatalIf(probe.NewError(err), "Unable to parse time argument.")
}
targetURL := getAliasURL(url, config.Aliases)
// if recursive strip off the "..."
err := doShareDownloadURL(stripRecursiveURL(targetURL), isURLRecursive(targetURL), expires)
fatalIf(err.Trace(targetURL), "Unable to generate URL for sharing.")
return
if !ctx.Args().Present() || ctx.Args().First() == "help" {
cli.ShowAppHelp(ctx)
}
}
// doShareList list shared url's
func doShareList() *probe.Error {
sURLs, err := loadSharedURLsV3()
if err != nil {
return err.Trace()
}
for i, data := range sURLs.URLs {
if time.Since(data.Date) > data.Message.Expiry {
sURLs.URLs = append(sURLs.URLs[:i], sURLs.URLs[i+1:]...)
continue
}
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(sURLs); err != nil {
return err.Trace()
}
return nil
}
// doShareURL share files from target
func doShareUploadURL(targetURL string, recursive bool, expires time.Duration, contentType string) *probe.Error {
shareDate := time.Now().UTC()
sURLs, err := loadSharedURLsV3()
if err != nil {
return err.Trace()
}
clnt, err := url2Client(targetURL)
if err != nil {
return err.Trace()
}
m, err := clnt.ShareUpload(recursive, expires, contentType)
if err != nil {
return err.Trace()
}
Key := targetURL
if recursive {
Key = Key + recursiveSeparator
m["key"] = "<KEY>"
}
shareMessage := ShareMessage{
Expiry: expires,
UploadInfo: m,
Key: Key,
}
shareMessageV3 := ShareMessageV3{
Expiry: expires,
UploadInfo: m,
Key: Key,
}
sURLs.URLs = append(sURLs.URLs, struct {
Date time.Time
Message ShareMessageV3
}{
Date: shareDate,
Message: shareMessageV3,
})
Prints("%s\n", shareMessage)
saveSharedURLsV3(sURLs)
return nil
}
// doShareURL share files from target
func doShareDownloadURL(targetURL string, recursive bool, expires time.Duration) *probe.Error {
shareDate := time.Now().UTC()
sURLs, err := loadSharedURLsV3()
if err != nil {
return err.Trace()
}
var clnt client.Client
clnt, err = target2Client(targetURL)
if err != nil {
return err.Trace()
}
if expires.Seconds() < 1 {
return probe.NewError(errors.New("Too low expires, expiration cannot be less than 1 second."))
}
if expires.Seconds() > 604800 {
return probe.NewError(errors.New("Too high expires, expiration cannot be larger than 7 days."))
}
for contentCh := range clnt.List(recursive) {
if contentCh.Err != nil {
return contentCh.Err.Trace()
}
var newClnt client.Client
newClnt, err = url2Client(getNewTargetURL(clnt.URL(), contentCh.Content.Name))
if err != nil {
return err.Trace()
}
var sharedURL string
sharedURL, err = newClnt.ShareDownload(expires)
if err != nil {
return err.Trace()
}
shareMessage := ShareMessage{
Expiry: expires,
DownloadUrl: sharedURL,
Key: newClnt.URL().String(),
}
shareMessageV3 := ShareMessageV3{
Expiry: expires,
DownloadUrl: sharedURL,
Key: newClnt.URL().String(),
}
sURLs.URLs = append(sURLs.URLs, struct {
Date time.Time
Message ShareMessageV3
}{
Date: shareDate,
Message: shareMessageV3,
})
Prints("%s\n", shareMessage)
}
saveSharedURLsV3(sURLs)
return nil
}
func getNewTargetURL(targetParser *client.URL, name string) string {
match, _ := filepath.Match("*.s3*.amazonaws.com", targetParser.Host)
if match {
targetParser.Path = string(targetParser.Separator) + name
} else {
targetParser.Path = string(targetParser.Separator) + path2Bucket(targetParser) + string(targetParser.Separator) + name
}
return targetParser.String()
}
// this code is necessary since, share only operates on cloud storage URLs not filesystem
func path2Bucket(u *client.URL) (bucketName string) {
pathSplits := strings.SplitN(u.Path, "?", 2)
splits := strings.SplitN(pathSplits[0], string(u.Separator), 3)
switch len(splits) {
case 0, 1:
bucketName = ""
case 2:
bucketName = splits[1]
case 3:
bucketName = splits[1]
}
return bucketName
}
+128
View File
@@ -0,0 +1,128 @@
/*
* 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 (
"time"
"github.com/minio/cli"
"github.com/minio/minio/pkg/probe"
)
// Share documents via URL.
var shareUpload = cli.Command{
Name: "upload",
Usage: "Share link that can be used to upload files to private bucket",
Action: mainShareUpload,
CustomHelpTemplate: `NAME:
mc share {{.Name}} - {{.Usage}}
USAGE:
mc share {{.Name}} TARGET [DURATION] [Content-Type]
DURATION = NN[h|m|s] [DEFAULT=168h]
EXAMPLES:
1. Generate Curl upload command, with a default expiry of 7 days.
$ mc share {{.Name}} https://s3.amazonaws.com/backup/2006-Mar-1/backup.tar.gz
2. Generate Curl upload command to upload files to a folder, with expiry of 120 hours
$ mc share {{.Name}} https://s3.amazonaws.com/backup/2007-Mar-2/... 120h
3. Generate Curl upload command to upload with expiry of 2 hours with content-type image/png
$ mc share {{.Name}} https://s3.amazonaws.com/backup/2007-Mar-2/... 2h image/png
`,
}
func checkShareUploadSyntax(ctx *cli.Context) {
args := ctx.Args()
if !args.Present() || args.First() == "help" {
cli.ShowCommandHelpAndExit(ctx, "upload", 1) // last argument is exit code
}
if len(args) > 3 {
cli.ShowCommandHelpAndExit(ctx, "upload", 1) // last argument is exit code
}
}
func mainShareUpload(ctx *cli.Context) {
var expires time.Duration
var err error
shareDataDirSetup()
checkShareUploadSyntax(ctx)
args := ctx.Args()
config := mustGetMcConfig()
url := args.Get(0)
if len(args.Get(1)) == 0 {
expires = time.Duration(604800) * time.Second
} else {
expires, err = time.ParseDuration(args.Get(1))
if err != nil {
fatalIf(probe.NewError(err), "Unable to parse time argument.")
}
}
contentType := args.Get(2)
targetURL := getAliasURL(url, config.Aliases)
setSharePalette(ctx.GlobalString("colors"))
e := doShareUploadURL(stripRecursiveURL(targetURL), isURLRecursive(targetURL), expires, contentType)
fatalIf(e.Trace(targetURL), "Unable to generate URL for sharing.")
}
// doShareURL share files from target
func doShareUploadURL(targetURL string, recursive bool, expires time.Duration, contentType string) *probe.Error {
shareDate := time.Now().UTC()
sURLs, err := loadSharedURLsV3()
if err != nil {
return err.Trace()
}
clnt, err := url2Client(targetURL)
if err != nil {
return err.Trace()
}
m, err := clnt.ShareUpload(recursive, expires, contentType)
if err != nil {
return err.Trace()
}
Key := targetURL
if recursive {
Key = Key + recursiveSeparator
m["key"] = "<KEY>"
}
shareMessage := ShareMessage{
Expiry: expires,
UploadInfo: m,
Key: Key,
}
shareMessageV3 := ShareMessageV3{
Expiry: expires,
UploadInfo: m,
Key: Key,
}
sURLs.URLs = append(sURLs.URLs, struct {
Date time.Time
Message ShareMessageV3
}{
Date: shareDate,
Message: shareMessageV3,
})
Prints("%s\n", shareMessage)
saveSharedURLsV3(sURLs)
return nil
}
+72 -4
View File
@@ -20,14 +20,35 @@ import (
"encoding/json"
"os"
"path/filepath"
"strings"
"time"
"github.com/fatih/color"
"github.com/minio/mc/pkg/client"
"github.com/minio/mc/pkg/console"
"github.com/minio/minio/pkg/probe"
"github.com/minio/minio/pkg/quick"
)
func newSharedURLs() *sharedURLsV2 {
return newSharedURLsV2()
func shareDataDirSetup() {
if !isSharedURLsDataDirExists() {
shareDir, err := getSharedURLsDataDir()
fatalIf(err.Trace(), "Unable to get shared URL data directory")
fatalIf(createSharedURLsDataDir().Trace(), "Unable to create shared URL data directory ‘"+shareDir+"’.")
}
if !isSharedURLsDataFileExists() {
shareFile, err := getSharedURLsDataFile()
fatalIf(err.Trace(), "Unable to get shared URL data file")
fatalIf(createSharedURLsDataFile().Trace(), "Unable to create shared URL data file ‘"+shareFile+"’.")
}
}
// migrateSharedURLs migrate to newest version sequentially
func migrateSharedURLs() {
migrateSharedURLsV1ToV2()
migrateSharedURLsV2ToV3()
}
func migrateSharedURLsV1ToV2() {
@@ -102,7 +123,7 @@ func migrateSharedURLsV2ToV3() {
Date: value.Date,
Message: ShareMessageV3{
Expiry: value.Message.Expiry,
DownloadUrl: value.Message.URL,
DownloadURL: value.Message.URL,
Key: value.Message.Key,
},
}
@@ -165,8 +186,55 @@ func isSharedURLsDataFileExists() bool {
}
func createSharedURLsDataFile() *probe.Error {
if err := saveSharedURLsV2(newSharedURLs()); err != nil {
if err := saveSharedURLsV3(newSharedURLsV3()); err != nil {
return err.Trace()
}
return nil
}
func getNewTargetURL(targetParser *client.URL, name string) string {
match, _ := filepath.Match("*.s3*.amazonaws.com", targetParser.Host)
if match {
targetParser.Path = string(targetParser.Separator) + name
} else {
targetParser.Path = string(targetParser.Separator) + path2Bucket(targetParser) + string(targetParser.Separator) + name
}
return targetParser.String()
}
// this code is necessary since, share only operates on cloud storage URLs not filesystem
func path2Bucket(u *client.URL) (bucketName string) {
pathSplits := strings.SplitN(u.Path, "?", 2)
splits := strings.SplitN(pathSplits[0], string(u.Separator), 3)
switch len(splits) {
case 0, 1:
bucketName = ""
case 2:
bucketName = splits[1]
case 3:
bucketName = splits[1]
}
return bucketName
}
func setSharePalette(style string) {
console.SetCustomPalette(map[string]*color.Color{
"Share": color.New(color.FgGreen, color.Bold),
"Expires": color.New(color.FgRed, color.Bold),
"URL": color.New(color.FgCyan, color.Bold),
})
if style == "light" {
console.SetCustomPalette(map[string]*color.Color{
"Share": color.New(color.FgWhite, color.Bold),
"Expires": color.New(color.FgWhite, color.Bold),
"URL": color.New(color.FgWhite, color.Bold),
})
return
}
/// Add more styles here
if style == "nocolor" {
// All coloring options exhausted, setting nocolor safely
console.SetNoColor()
}
}