forked from tiny-craft/tiny-rdm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add custom decoder/encoder for value content
- Loading branch information
1 parent
7faca87
commit 450e451
Showing
18 changed files
with
783 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package convutil | ||
|
||
import ( | ||
"encoding/base64" | ||
"os/exec" | ||
"strings" | ||
sliceutil "tinyrdm/backend/utils/slice" | ||
) | ||
|
||
type CmdConvert struct { | ||
Name string | ||
Auto bool | ||
DecodePath string | ||
DecodeArgs []string | ||
EncodePath string | ||
EncodeArgs []string | ||
} | ||
|
||
const replaceholder = "{VALUE}" | ||
|
||
func (c CmdConvert) Encode(str string) (string, bool) { | ||
base64Content := base64.StdEncoding.EncodeToString([]byte(str)) | ||
var containHolder bool | ||
args := sliceutil.Map(c.EncodeArgs, func(i int) string { | ||
arg := strings.TrimSpace(c.EncodeArgs[i]) | ||
if strings.Contains(arg, replaceholder) { | ||
arg = strings.ReplaceAll(arg, replaceholder, base64Content) | ||
containHolder = true | ||
} | ||
return arg | ||
}) | ||
if len(args) <= 0 || !containHolder { | ||
args = append(args, base64Content) | ||
} | ||
cmd := exec.Command(c.EncodePath, args...) | ||
output, err := cmd.Output() | ||
if err != nil || len(output) <= 0 || string(output) == "[RDM-ERROR]" { | ||
return str, false | ||
} | ||
|
||
outputContent := make([]byte, base64.StdEncoding.DecodedLen(len(output))) | ||
n, err := base64.StdEncoding.Decode(outputContent, output) | ||
if err != nil { | ||
return str, false | ||
} | ||
return string(outputContent[:n]), true | ||
} | ||
|
||
func (c CmdConvert) Decode(str string) (string, bool) { | ||
base64Content := base64.StdEncoding.EncodeToString([]byte(str)) | ||
var containHolder bool | ||
args := sliceutil.Map(c.DecodeArgs, func(i int) string { | ||
arg := strings.TrimSpace(c.DecodeArgs[i]) | ||
if strings.Contains(arg, replaceholder) { | ||
arg = strings.ReplaceAll(arg, replaceholder, base64Content) | ||
containHolder = true | ||
} | ||
return arg | ||
}) | ||
if len(args) <= 0 || !containHolder { | ||
args = append(args, base64Content) | ||
} | ||
cmd := exec.Command(c.DecodePath, args...) | ||
output, err := cmd.Output() | ||
if err != nil || len(output) <= 0 || string(output) == "[RDM-ERROR]" { | ||
return str, false | ||
} | ||
|
||
outputContent := make([]byte, base64.StdEncoding.DecodedLen(len(output))) | ||
n, err := base64.StdEncoding.Decode(outputContent, output) | ||
if err != nil { | ||
return str, false | ||
} | ||
return string(outputContent[:n]), true | ||
} |
Oops, something went wrong.