@@ -155,22 +155,63 @@ func encryptFile(filename string) error {
155155 if err != nil {
156156 return fmt .Errorf ("error encrypting file: %w" , err )
157157 }
158-
159158 fmt .Printf ("File encrypted successfully: %s.gpg\n " , filename )
159+
160+ // Use askUserYN to ask if the user wants to upload to IPFS
161+ if askUserYN ("Do you want to upload the encrypted file to IPFS?" ) {
162+ // Call ipfsUpload function with the encrypted file
163+ if err := ipfsUpload (filename + ".gpg" ); err != nil {
164+ return fmt .Errorf ("error uploading file to IPFS: %w" , err )
165+ }
166+ }
167+
168+ return nil
169+ }
170+
171+ func saveCID (cid string ) error {
172+ // Write CID to log_CID.log
173+ f , err := os .OpenFile ("log_CID.log" , os .O_APPEND | os .O_CREATE | os .O_WRONLY , 0644 )
174+ if err != nil {
175+ return err
176+ }
177+ defer f .Close ()
178+
179+ if _ , err := f .WriteString (cid + "\n " ); err != nil {
180+ return err
181+ }
182+
160183 return nil
161184}
162185
186+
187+ func ipfsUpload (filePath string ) error {
188+ // Call AddFileToIPFS function
189+ cid , err := ipfs_link .AddFileToIPFS (filePath )
190+ if err != nil {
191+ return err
192+ }
193+
194+ // Call saveCID function with the returned CID
195+ if err := saveCID (cid ); err != nil {
196+ return fmt .Errorf ("error saving CID: %w" , err )
197+ }
198+
199+ fmt .Println ("File uploaded to IPFS successfully, CID:" , cid )
200+ return nil
201+ }
202+
203+
163204func decryptFile (filename string ) error {
164205 // Ask user for CID
165206 cid , err := generalAskUser ("Enter the CID for the file to decrypt: " )
166207 if err != nil {
167208 return fmt .Errorf ("error reading CID: %w" , err )
168209 }
169210
170- outputPath := "./" // Set the output path, adjust if necessary
211+ ipfsFilePath := "retrieved_" + filename // Save the file retrieved from IPFS with a prefixed name
171212
172- // Retrieve the file from IPFS using ipfs_link library
173- err = ipfs_link .GetFileFromIPFS (cid , outputPath )
213+ // Retrieve the file from IPFS
214+ err = ipfs_link .GetFileFromIPFS (cid , ipfsFilePath )
174215 if err != nil {
175216 return fmt .Errorf ("error retrieving file from IPFS: %w" , err )
176217 }
@@ -186,15 +227,14 @@ func decryptFile(filename string) error {
186227 if err != nil {
187228 return fmt .Errorf ("error reading passphrase: %w" , err )
188229 }
189-
190230 // Generate the symmetric key
191231 seedKDF := publicKey + passphrase
192232 kdfKey := sha256 .Sum256 ([]byte (seedKDF ))
193233 decryptedKey := fmt .Sprintf ("%x" , kdfKey )
194234
195- // Decrypt the file using GPG and the derived key
196- decryptedFilePath := outputPath // Adjust as needed
197- cmd := exec .Command ("gpg" , "--decrypt" , "--batch" , "--passphrase" , decryptedKey , "--output" , decryptedFilePath , outputPath )
235+ // Decrypt the file using GPG
236+ decryptedFilePath := "decrypted_" + filename // This is the path where the decrypted file will be saved
237+ cmd := exec .Command ("gpg" , "--decrypt" , "--batch" , "--passphrase" , decryptedKey , "--output" , decryptedFilePath , ipfsFilePath )
198238 cmd .Stdout = os .Stdout
199239 cmd .Stderr = os .Stderr
200240 err = cmd .Run ()
@@ -206,6 +246,3 @@ func decryptFile(filename string) error {
206246 return nil
207247}
208248
209-
210-
211-
0 commit comments