2022-08-28 19:21:57 +08:00
|
|
|
|
// Package link provides the link command.
|
2018-03-29 15:10:19 +08:00
|
|
|
|
package link
|
|
|
|
|
|
|
|
|
|
import (
|
2019-06-17 16:34:30 +08:00
|
|
|
|
"context"
|
2018-03-29 15:10:19 +08:00
|
|
|
|
"fmt"
|
|
|
|
|
|
2019-07-29 01:47:38 +08:00
|
|
|
|
"github.com/rclone/rclone/cmd"
|
2020-06-01 05:18:01 +08:00
|
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
|
"github.com/rclone/rclone/fs/config/flags"
|
2019-07-29 01:47:38 +08:00
|
|
|
|
"github.com/rclone/rclone/fs/operations"
|
2018-03-29 15:10:19 +08:00
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
)
|
|
|
|
|
|
2020-06-01 05:18:01 +08:00
|
|
|
|
var (
|
2021-04-06 23:21:32 +08:00
|
|
|
|
expire = fs.DurationOff
|
2020-06-01 05:18:01 +08:00
|
|
|
|
unlink = false
|
|
|
|
|
)
|
|
|
|
|
|
2018-03-29 15:10:19 +08:00
|
|
|
|
func init() {
|
2019-10-11 23:58:11 +08:00
|
|
|
|
cmd.Root.AddCommand(commandDefinition)
|
2020-06-01 05:18:01 +08:00
|
|
|
|
cmdFlags := commandDefinition.Flags()
|
2023-07-11 01:34:10 +08:00
|
|
|
|
flags.FVarP(cmdFlags, &expire, "expire", "", "The amount of time that the link will be valid", "")
|
|
|
|
|
flags.BoolVarP(cmdFlags, &unlink, "unlink", "", unlink, "Remove existing public link to file/folder", "")
|
2018-03-29 15:10:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-11 23:58:11 +08:00
|
|
|
|
var commandDefinition = &cobra.Command{
|
2018-03-29 15:10:19 +08:00
|
|
|
|
Use: "link remote:path",
|
|
|
|
|
Short: `Generate public link to file/folder.`,
|
2020-06-01 05:18:01 +08:00
|
|
|
|
Long: `rclone link will create, retrieve or remove a public link to the given
|
|
|
|
|
file or folder.
|
2018-03-29 15:10:19 +08:00
|
|
|
|
|
|
|
|
|
rclone link remote:path/to/file
|
|
|
|
|
rclone link remote:path/to/folder/
|
2020-06-01 05:18:01 +08:00
|
|
|
|
rclone link --unlink remote:path/to/folder/
|
|
|
|
|
rclone link --expire 1d remote:path/to/file
|
|
|
|
|
|
|
|
|
|
If you supply the --expire flag, it will set the expiration time
|
|
|
|
|
otherwise it will use the default (100 years). **Note** not all
|
|
|
|
|
backends support the --expire flag - if the backend doesn't support it
|
|
|
|
|
then the link returned won't expire.
|
|
|
|
|
|
|
|
|
|
Use the --unlink flag to remove existing public links to the file or
|
|
|
|
|
folder. **Note** not all backends support "--unlink" flag - those that
|
|
|
|
|
don't will just ignore it.
|
2018-03-29 15:10:19 +08:00
|
|
|
|
|
2020-06-01 05:18:01 +08:00
|
|
|
|
If successful, the last line of the output will contain the
|
|
|
|
|
link. Exact capabilities depend on the remote, but the link will
|
|
|
|
|
always by default be created with the least constraints – e.g. no
|
|
|
|
|
expiry, no password protection, accessible without account.
|
2018-03-29 15:10:19 +08:00
|
|
|
|
`,
|
2022-11-27 06:40:49 +08:00
|
|
|
|
Annotations: map[string]string{
|
|
|
|
|
"versionIntroduced": "v1.41",
|
|
|
|
|
},
|
2018-03-29 15:10:19 +08:00
|
|
|
|
Run: func(command *cobra.Command, args []string) {
|
|
|
|
|
cmd.CheckArgs(1, 1, command, args)
|
|
|
|
|
fsrc, remote := cmd.NewFsFile(args[0])
|
|
|
|
|
cmd.Run(false, false, command, func() error {
|
2020-06-01 05:18:01 +08:00
|
|
|
|
link, err := operations.PublicLink(context.Background(), fsrc, remote, expire, unlink)
|
2018-03-29 15:10:19 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2018-09-05 03:02:35 +08:00
|
|
|
|
if link != "" {
|
|
|
|
|
fmt.Println(link)
|
|
|
|
|
}
|
2018-03-29 15:10:19 +08:00
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
}
|