-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathquick_upload_example_test.go
136 lines (113 loc) · 3.21 KB
/
quick_upload_example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Package quick provides a high-performance HTTP framework with built-in utilities
// for handling file uploads, multipart form data, and request processing.
//
// This file contains Example functions for the GoDoc documentation, demonstrating
// how to use various methods in the Quick framework, particularly related to
// handling uploaded files, saving them, setting file size limits, and parsing multipart forms.
package quick
import (
"fmt"
)
// form multipart/formdata
// This function is named ExampleUploadedFile_FileName()
// it with the Examples type.
func ExampleUploadedFile_FileName() {
// Creating an UploadedFile object simulating an uploaded file
uploadedFile := &UploadedFile{
Info: FileInfo{
Filename: "quick.txt",
},
}
// Retrieving the filename
fmt.Println(uploadedFile.FileName())
// Out put: quick.txt
}
// This function is named ExampleUploadedFile_Size()
// it with the Examples type.
func ExampleUploadedFile_Size() {
// Creating an UploadedFile object simulating an uploaded file of 2048 bytes
uploadedFile := &UploadedFile{
Info: FileInfo{
Size: 2048,
},
}
// Retrieving the file size
fmt.Println(uploadedFile.Size())
// Out put: 2048
}
// This function is named ExampleUploadedFile_ContentType()
// it with the Examples type.
func ExampleUploadedFile_ContentType() {
// Creating an UploadedFile object simulating an uploaded PNG file
uploadedFile := &UploadedFile{
Info: FileInfo{
ContentType: "image/png",
},
}
// Retrieving the content type
fmt.Println(uploadedFile.ContentType())
// Out put: image/png
}
// This function is named ExampleUploadedFile_Bytes()
// it with the Examples type.
func ExampleUploadedFile_Bytes() {
// Creating an UploadedFile object with content as bytes
uploadedFile := &UploadedFile{
Info: FileInfo{
Bytes: []byte("Hello, Quick!"),
},
}
// Converting bytes to a string to display the content
fmt.Println(string(uploadedFile.Bytes()))
// Out put: Hello, Quick!
}
// This function is named ExampleUploadedFile_Save()
// it with the Examples type.
func ExampleUploadedFile_Save() {
// Creating an UploadedFile object simulating an uploaded file
uploadedFile := &UploadedFile{
Info: FileInfo{
Filename: "quick.txt",
Bytes: []byte("Hello, Quick!"),
},
}
// Saving the file to the "uploads" directory
err := uploadedFile.Save("uploads")
// Checking for errors
if err != nil {
fmt.Println("Error saving file:", err)
} else {
fmt.Println("File saved successfully!")
}
// Out put: File saved successfully!
}
// This function is named ExampleSaveAll()
// it with the Examples type.
func ExampleSaveAll() {
// Creating a list of files to simulate multiple uploads
files := []*UploadedFile{
{
Info: FileInfo{
Filename: "file1.txt",
Bytes: []byte("File 1 content"),
},
},
{
Info: FileInfo{
Filename: "file2.txt",
Bytes: []byte("File 2 content"),
},
},
}
// Creating a temporary directory for testing
tempDir := "test_uploads"
// Saving all files to the temporary directory
err := SaveAll(files, tempDir)
// Handling errors
if err != nil {
fmt.Println("Error saving files:", err)
} else {
fmt.Println("All files saved successfully!")
}
// Out put:All files saved successfully!
}