cryptdecode: added option to return encrypted file names. Fixes #1923

This commit is contained in:
ishuah 2018-01-08 11:20:01 +03:00 committed by Ishuah Kariuki
parent 6cac98d2ce
commit 0a7731cf0d
2 changed files with 58 additions and 7 deletions

View File

@ -10,8 +10,15 @@ import (
"github.com/spf13/cobra"
)
// Options set by command line flags
var (
Reverse = false
)
func init() {
cmd.Root.AddCommand(commandDefinition)
flags := commandDefinition.Flags()
fs.BoolVarP(flags, &Reverse, "reverse", "", Reverse, "Reverse cryptdecode, encrypts filenames")
}
var commandDefinition = &cobra.Command{
@ -21,25 +28,44 @@ var commandDefinition = &cobra.Command{
rclone cryptdecode returns unencrypted file names when provided with
a list of encrypted file names. List limit is 10 items.
If you supply the --reverse flag, it will return encrypted file names.
use it like this
rclone cryptdecode encryptedremote: encryptedfilename1 encryptedfilename2
rclone cryptdecode --reverse encryptedremote: filename1 filename2
`,
Run: func(command *cobra.Command, args []string) {
cmd.CheckArgs(2, 11, command, args)
fsrc := cmd.NewFsSrc(args)
cmd.Run(false, false, command, func() error {
return cryptDecode(fsrc, args[1:])
})
if Reverse {
cmd.Run(false, false, command, func() error {
return cryptEncode(fsrc, args[1:])
})
} else {
cmd.Run(false, false, command, func() error {
return cryptDecode(fsrc, args[1:])
})
}
},
}
// Check if fsrc is a crypt
func assertCryptFs(fsrc fs.Fs) (*crypt.Fs, error) {
fcrypt, ok := fsrc.(*crypt.Fs)
if !ok {
return nil, errors.Errorf("%s:%s is not a crypt remote", fsrc.Name(), fsrc.Root())
}
return fcrypt, nil
}
// cryptDecode returns the unencrypted file name
func cryptDecode(fsrc fs.Fs, args []string) error {
// Check if fsrc is a crypt
fcrypt, ok := fsrc.(*crypt.Fs)
if !ok {
return errors.Errorf("%s:%s is not a crypt remote", fsrc.Name(), fsrc.Root())
fcrypt, err := assertCryptFs(fsrc)
if err != nil {
return err
}
output := ""
@ -57,3 +83,23 @@ func cryptDecode(fsrc fs.Fs, args []string) error {
return nil
}
// cryptEncode returns the encrypted file name
func cryptEncode(fsrc fs.Fs, args []string) error {
fcrypt, err := assertCryptFs(fsrc)
if err != nil {
return err
}
output := ""
for _, fileName := range args {
encryptedFileName := fcrypt.EncryptFileName(fileName)
output += fmt.Sprintln(fileName, "\t", encryptedFileName)
}
fmt.Printf(output)
return nil
}

View File

@ -445,6 +445,11 @@ func (f *Fs) UnWrap() fs.Fs {
return f.Fs
}
// EncryptFileName returns an encrypted file name
func (f *Fs) EncryptFileName(fileName string) string {
return f.cipher.EncryptFileName(fileName)
}
// DecryptFileName returns a decrypted file name
func (f *Fs) DecryptFileName(encryptedFileName string) (string, error) {
return f.cipher.DecryptFileName(encryptedFileName)