@@ -4,9 +4,12 @@ import (
44 "bytes"
55 "fmt"
66 "io"
7+ "log/slog"
78 "os"
9+ "path/filepath"
810 "strings"
911
12+ "github.com/gabriel-vasile/mimetype"
1013 "github.com/opentdf/otdfctl/pkg/cli"
1114 "github.com/opentdf/otdfctl/pkg/man"
1215 "github.com/spf13/cobra"
@@ -23,10 +26,14 @@ func dev_tdfEncryptCmd(cmd *cobra.Command, args []string) {
2326
2427 flagHelper := cli .NewFlagHelper (cmd )
2528 var filePath string
29+ var fileExt string
2630 if len (args ) > 0 {
2731 filePath = args [0 ]
32+ fileExt = strings .ToLower (strings .TrimPrefix (filepath .Ext (filePath ), "." ))
2833 }
34+
2935 out := flagHelper .GetOptionalString ("out" )
36+ fileMimeType := flagHelper .GetOptionalString ("mime-type" )
3037 values := flagHelper .GetStringSlice ("attr" , attrValues , cli.FlagHelperStringSliceOptions {Min : 0 })
3138 tdfType := flagHelper .GetOptionalString ("tdf-type" )
3239 if tdfType == "" {
@@ -43,25 +50,46 @@ func dev_tdfEncryptCmd(cmd *cobra.Command, args []string) {
4350 inputCount ++
4451 }
4552
53+ cliExit := func (s string ) {
54+ cli .ExitWithError ("Must provide " + s + " of the following to encrypt: [file argument, stdin input]" , nil )
55+ }
4656 if inputCount == 0 {
47- cli . ExitWithError ( "Must provide ONE of the following to encrypt: [file argument, stdin input]" , nil )
57+ cliExit ( " ONE" )
4858 } else if inputCount > 1 {
49- cli . ExitWithError ( "Must provide ONLY ONE of the following to encrypt: [file argument, stdin input]" , nil )
59+ cliExit ( " ONLY ONE" )
5060 }
5161
5262 // prefer filepath argument over stdin input
53- var bytesSlice [] byte
63+ bytesSlice := piped
5464 if filePath != "" {
5565 bytesSlice = readBytesFromFile (filePath )
56- } else {
57- bytesSlice = piped
5866 }
5967
68+ // auto-detect mime type if not provided
69+ if fileMimeType == "" {
70+ slog .Debug ("Detecting mime type of file" )
71+ // get the mime type of the file
72+ mimetype .SetLimit (1024 * 1024 ) // limit to 1MB
73+ m := mimetype .Detect (bytesSlice )
74+ // default to application/octet-stream if no mime type is detected
75+ fileMimeType = m .String ()
76+
77+ if fileMimeType == "application/octet-stream" {
78+ if fileExt != "" {
79+ fileMimeType = mimetype .Lookup (fileExt ).String ()
80+ }
81+ }
82+ }
83+ slog .Debug ("Encrypting file" ,
84+ slog .Int ("file-len" , len (bytesSlice )),
85+ slog .String ("mime-type" , fileMimeType ),
86+ )
87+
6088 // Do the encryption
6189 var encrypted * bytes.Buffer
6290 var err error
6391 if tdfType == TDF3 {
64- encrypted , err = h .EncryptBytes (bytesSlice , values )
92+ encrypted , err = h .EncryptBytes (bytesSlice , values , fileMimeType )
6593 } else if tdfType == NANO {
6694 encrypted , err = h .EncryptNanoBytes (bytesSlice , values )
6795 } else {
@@ -111,6 +139,11 @@ func init() {
111139 []string {},
112140 encryptCmd .GetDocFlag ("attr" ).Description ,
113141 )
142+ encryptCmd .Flags ().String (
143+ encryptCmd .GetDocFlag ("mime-type" ).Name ,
144+ encryptCmd .GetDocFlag ("mime-type" ).Default ,
145+ encryptCmd .GetDocFlag ("mime-type" ).Description ,
146+ )
114147 encryptCmd .Flags ().StringP (
115148 encryptCmd .GetDocFlag ("tdf-type" ).Name ,
116149 encryptCmd .GetDocFlag ("tdf-type" ).Shorthand ,
0 commit comments