Skip to content

Commit

Permalink
feat: init file trasnfer logic
Browse files Browse the repository at this point in the history
  • Loading branch information
abdealijaroli committed Sep 1, 2024
1 parent 3a53e28 commit 7af40ed
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 7 deletions.
8 changes: 4 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

var shortenURL string
var transferFile string
var fileToTransfer string

var rootCmd = &cobra.Command{
Use: "jaro",
Expand All @@ -31,8 +31,8 @@ var rootCmd = &cobra.Command{
}
fmt.Printf("Your sweetened link: %s\n", shortened)
return
} else if transferFile != "" {
fmt.Printf("Transferring file: %s\n", transferFile)
} else if fileToTransfer != "" {
fmt.Printf("Transferring file: %s\n", fileToTransfer)
return
} else {
fmt.Println("Please provide a valid option. Run 'jaro --help' for more information.")
Expand All @@ -43,7 +43,7 @@ var rootCmd = &cobra.Command{

func Execute() {
rootCmd.Flags().StringVarP(&shortenURL, "shorten", "s", "", "Link to shorten")
rootCmd.Flags().StringVarP(&transferFile, "transfer", "t", "", "File to transfer")
rootCmd.Flags().StringVarP(&fileToTransfer, "transfer", "t", "", "File to transfer")
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
Expand Down
65 changes: 62 additions & 3 deletions cmd/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,69 @@ package cmd

import (
"fmt"
"github.com/skip2/go-qrcode"
"github.com/spf13/cobra"
"io"
"net/http"
"os"
)

func transferFile(filePath string) {
file, err := os.Open(filePath)
if err != nil {
fmt.Printf("Error opening file: %v\n", err)
return
}
defer file.Close()

shortCode := generateShortCode(filePath)
shortURL := fmt.Sprintf("https://jaroli.me/%s", shortCode)

qr, err := qrcode.New(shortURL, qrcode.Medium)
if err != nil {
fmt.Printf("Error generating QR code: %v\n", err)
return
}

fmt.Printf("Your shareable file link is: %s\n", shortURL)
fmt.Println("QR Code for your link:")
fmt.Println(qr.ToSmallString(false))

go startFileServer(shortCode, file)
}

func startFileServer(shortCode string, file *os.File) {
http.HandleFunc("/"+shortCode, func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "<h1>File Transfer</h1>")
fmt.Fprintf(w, "<p>Sender: %s</p>", "SenderName")
fmt.Fprintf(w, "<p>File: %s</p>", file.Name())
fmt.Fprintf(w, "<form method='POST' action='/accept/%s'><button type='submit'>Accept</button></form>", shortCode)
})

http.HandleFunc("/accept/"+shortCode, func(w http.ResponseWriter, r *http.Request) {
transferToRecipient(w, file)
})

http.ListenAndServe(":8080", nil)
}

func transferToRecipient(w http.ResponseWriter, file *os.File) {
w.Header().Set("Content-Disposition", "attachment; filename="+file.Name())
w.Header().Set("Content-Type", "application/octet-stream")

_, err := io.Copy(w, file)
if err != nil {
http.Error(w, "Error transferring file", http.StatusInternalServerError)
return
}

fmt.Fprintln(w, "File delivered successfully.")
}

func generateShortCode(filePath string) string {
return ""
}

var transferCmd = &cobra.Command{
Use: "transfer",
Short: "Transfer a file",
Expand All @@ -13,10 +73,9 @@ var transferCmd = &cobra.Command{
fmt.Println("Please provide a file to transfer. Run 'jaro --help' for more information.")
return
}
file := args[0]
filePath := args[0]

// file transfer logic here
fmt.Printf("Transferring file: %s\n", file)
transferFile(filePath)
},
}

Expand Down

0 comments on commit 7af40ed

Please sign in to comment.