@@ -3,14 +3,19 @@ package utils
33import (
44 "context"
55 "fmt"
6+ "log"
67 "os"
78 "path/filepath"
89 "strings"
910 "time"
11+
12+ "github.com/h0tak88r/AutoAR/internal/modules/r2storage"
1013)
1114
1215// WriteLines writes lines to a file (one per line)
13- // Creates parent directories if they don't exist
16+ // Creates parent directories if they don't exist.
17+ // After writing, if R2 storage is enabled and the file is non-empty,
18+ // it automatically uploads the file and prints the public URL.
1419func WriteLines (path string , lines []string ) error {
1520 if err := os .MkdirAll (filepath .Dir (path ), 0755 ); err != nil {
1621 return err
@@ -19,9 +24,36 @@ func WriteLines(path string, lines []string) error {
1924 if len (lines ) > 0 {
2025 data += "\n "
2126 }
22- return os .WriteFile (path , []byte (data ), 0644 )
27+ if err := os .WriteFile (path , []byte (data ), 0644 ); err != nil {
28+ return err
29+ }
30+
31+ // Auto-upload to R2 if enabled and file is non-empty
32+ if len (lines ) > 0 && r2storage .IsEnabled () {
33+ go uploadResultAsync (path )
34+ }
35+
36+ return nil
2337}
2438
39+ // uploadResultAsync uploads a result file to R2 in the background.
40+ // The R2 key strips the leading "/" and uses the full local path as key.
41+ func uploadResultAsync (path string ) {
42+ // Derive R2 key from the path:
43+ // Strip absolute prefix up to "new-results" so the key is e.g.
44+ // new-results/example.com/subs/subdomains.txt
45+ r2Key := path
46+ if idx := strings .Index (path , "new-results/" ); idx >= 0 {
47+ r2Key = path [idx :]
48+ } else if strings .HasPrefix (r2Key , "/" ) {
49+ r2Key = r2Key [1 :]
50+ }
51+ r2storage .UploadResultFileAndLog (path , r2Key )
52+ log .Printf ("[R2] ✅ Auto-uploaded result file: %s" , path )
53+ }
54+
55+
56+
2557// ReadLines reads lines from a file
2658func ReadLines (path string ) ([]string , error ) {
2759 data , err := os .ReadFile (path )
0 commit comments