@@ -2,8 +2,10 @@ package main
2
2
3
3
import (
4
4
"fmt"
5
+ "image"
6
+ "image/color"
7
+ "image/png"
5
8
"log"
6
- "math/rand"
7
9
"net/http"
8
10
"os"
9
11
"os/signal"
@@ -29,12 +31,7 @@ func main() {
29
31
Value : 8088 ,
30
32
Aliases : []string {"p" },
31
33
},
32
- & cli.StringFlag {
33
- Name : FILE_URL ,
34
- Usage : "Specifies the url to serve the file at." ,
35
- Value : "/test.txt" ,
36
- Aliases : []string {"f" },
37
- },
34
+
38
35
& cli.UintFlag {
39
36
Name : FILE_LENGTH ,
40
37
Usage : "Specifies the length of the file to serve." ,
@@ -44,17 +41,22 @@ func main() {
44
41
},
45
42
Action : func (c * cli.Context ) error {
46
43
const (
47
- fileName = "test.txt "
44
+ fileName = "test.png "
48
45
)
49
46
50
47
fileContent := generateFileData (c .Int (FILE_LENGTH ))
51
48
filePath , cleanup := setupFile (fileName , fileContent )
52
49
defer cleanup ()
53
50
54
- http .HandleFunc (c . String ( FILE_URL ) , func (w http.ResponseWriter , r * http.Request ) {
51
+ http .HandleFunc ("/" , func (w http.ResponseWriter , r * http.Request ) {
55
52
// Set the Content-Disposition header to suggest a filename
56
53
w .Header ().Set ("Content-Disposition" , fmt .Sprintf ("attachment; filename=\" %s\" " , fileName ))
57
54
55
+ // Add CORS headers to allow all origins (*).
56
+ w .Header ().Set ("Access-Control-Allow-Origin" , "*" )
57
+ w .Header ().Set ("Access-Control-Allow-Headers" , "*" )
58
+ w .Header ().Set ("Access-Control-Expose-Headers" , "*" )
59
+
58
60
http .ServeFile (w , r , filePath )
59
61
})
60
62
@@ -81,7 +83,7 @@ func waitForKillSignal() {
81
83
}
82
84
83
85
// setupFile creates a file with the given name and content, and returns a cleanup function
84
- func setupFile (fileName string , fileContent string ) (string , func ()) {
86
+ func setupFile (fileName string , fileContent * image. RGBA ) (string , func ()) {
85
87
dataFolder , err := os .MkdirTemp ("" , "sample-file-server-*" )
86
88
if err != nil {
87
89
panic (err )
@@ -94,11 +96,12 @@ func setupFile(fileName string, fileContent string) (string, func()) {
94
96
}
95
97
defer file .Close ()
96
98
97
- _ , err = file . WriteString ( fileContent )
98
- if err != nil {
99
+ if err := png . Encode ( file , fileContent ); err != nil {
100
+ fmt . Println ( "Failed to encode image:" , err )
99
101
os .Remove (filePath )
100
102
panic (err )
101
103
}
104
+
102
105
return filePath , func () {
103
106
err := os .Remove (fileName )
104
107
if err != nil {
@@ -108,22 +111,26 @@ func setupFile(fileName string, fileContent string) (string, func()) {
108
111
}
109
112
110
113
// generateFileData generates a string of the given length composed of random words
111
- func generateFileData (length int ) (fileData string ) {
112
- if length < 10 {
113
- panic ("file length must be at least 10" )
114
- }
115
- wordSelection := []string {
116
- "Alpha" , "Bravo" , "Charlie" , "Delta" , "Echo" , "Foxtrot" , "Golf" , "Hotel" ,
117
- "India" , "Juliet" , "Kilo" , "Lima" , "Mike" , "November" , "Oscar" , "Papa" ,
118
- "Quebec" , "Romeo" , "Sierra" , "Tango" , "Uniform" , "Victor" , "Whiskey" ,
119
- "X-ray" , "Yankee" , "Zulu" ,
120
- }
121
- fileData = "START"
122
- // Continue adding words until we reach the desired length or beyond
123
- for len (fileData ) < length {
124
- randomIndex := rand .Intn (len (wordSelection ))
125
- fileData = fileData + " " + wordSelection [randomIndex ]
114
+ func generateFileData (length int ) (img * image.RGBA ) {
115
+ // Define image dimensions
116
+ width , height := length , length
117
+
118
+ // Create an empty RGBA image
119
+ img = image .NewRGBA (image .Rect (0 , 0 , width , height ))
120
+
121
+ // Fill the image with a gradient
122
+ for y := 0 ; y < height ; y ++ {
123
+ for x := 0 ; x < width ; x ++ {
124
+ // Gradient: Horizontal red, Vertical blue
125
+ c := color.RGBA {
126
+ R : uint8 (x * 255 / width ),
127
+ B : uint8 (y * 255 / height ),
128
+ G : 0 ,
129
+ A : 255 , // Fully opaque
130
+ }
131
+ img .Set (x , y , c )
132
+ }
126
133
}
127
134
128
- return fileData [: length - 3 ] + "END"
135
+ return img
129
136
}
0 commit comments