Skip to content

Commit b9d1072

Browse files
committed
docs(main): add more doc comments to the main file
1 parent ed4860d commit b9d1072

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

main.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,50 +136,60 @@ func handleUploadFile(w http.ResponseWriter, r *http.Request) error {
136136
err error
137137
)
138138

139-
// parse and validate file and post parameters.
139+
// Parse and validate file and post parameters.
140140
file, fileHeader, err := r.FormFile(uploadFileFormField)
141141
if err != nil {
142142
log.Printf("error ocurred getting file from form: %v", err)
143143
return WithHTTPStatus(err, http.StatusBadRequest)
144144
}
145145
defer file.Close()
146146

147+
// Get the content of the file in form of a slice of bytes.
147148
fileBytes, err := io.ReadAll(file)
148149
if err != nil {
149150
log.Printf("error ocurred reading file: %v", err)
150151
return WithHTTPStatus(err, http.StatusBadRequest)
151152
}
152153

154+
// Get the sub-type of the input file from the form.
153155
targetFileSubType := r.FormValue("input_format")
154156

157+
// Call Detect fuction to get the mimetype of the input file.
155158
detectedFileType := mimetype.Detect(fileBytes)
156159

160+
// Parse the mimetype to get the type and the sub-type of the input file.
157161
fileType, subType, err := files.TypeAndSupType(detectedFileType.String())
158162
if err != nil {
159163
log.Printf("error occurred getting type and subtype from mimetype: %v", err)
160164
return WithHTTPStatus(err, http.StatusBadRequest)
161165
}
162166

167+
// Get the right factory based off the input file type.
163168
fileFactory, err := files.BuildFactory(fileType, fileHeader.Filename)
164169
if err != nil {
165170
log.Printf("error occurred while getting a file factory: %v", err)
166171
return WithHTTPStatus(err, http.StatusBadRequest)
167172
}
168173

174+
// Returns an object that implements the File interface based on the sub-type of the input file.
169175
f, err := fileFactory.NewFile(subType)
170176
if err != nil {
171177
log.Printf("error occurred getting the file object: %v", err)
172178
return WithHTTPStatus(err, http.StatusBadRequest)
173179
}
174180

181+
// Return the kind of the output file.
175182
targetFileType := files.SupportedFileTypes()[targetFileSubType]
183+
184+
// Convert the file to the target format.
185+
// convertedFile is an io.Reader.
176186
convertedFile, err = f.ConvertTo(
177187
cases.Title(language.English).String(targetFileType),
178188
targetFileSubType,
179189
bytes.NewReader(fileBytes),
180190
)
181191
if err != nil {
182-
log.Printf("error ocurred while converting image %v", err)
192+
log.Printf("error ocurred while processing the input file: %v", err)
183193
return WithHTTPStatus(err, http.StatusInternalServerError)
184194
}
185195

@@ -193,18 +203,20 @@ func handleUploadFile(w http.ResponseWriter, r *http.Request) error {
193203

194204
newFile, err := os.Create(convertedFilePath)
195205
if err != nil {
196-
log.Printf("error occurred converting file: %v", err)
206+
log.Printf("error occurred while creating the output file: %v", err)
197207
return WithHTTPStatus(err, http.StatusInternalServerError)
198208
}
199209
defer newFile.Close()
200210

201211
buf := new(bytes.Buffer)
202212
if _, err := buf.ReadFrom(convertedFile); err != nil {
213+
log.Printf("error occurred while readinf from the converted file: %v", err)
214+
return WithHTTPStatus(err, http.StatusInternalServerError)
203215
}
204216

205217
convertedFileBytes := buf.Bytes()
206218
if _, err := newFile.Write(convertedFileBytes); err != nil {
207-
log.Printf("error occurred writing file: %v", err)
219+
log.Printf("error occurred writing converted output to a file in disk: %v", err)
208220
return WithHTTPStatus(err, http.StatusInternalServerError)
209221
}
210222

0 commit comments

Comments
 (0)