@@ -136,50 +136,60 @@ func handleUploadFile(w http.ResponseWriter, r *http.Request) error {
136
136
err error
137
137
)
138
138
139
- // parse and validate file and post parameters.
139
+ // Parse and validate file and post parameters.
140
140
file , fileHeader , err := r .FormFile (uploadFileFormField )
141
141
if err != nil {
142
142
log .Printf ("error ocurred getting file from form: %v" , err )
143
143
return WithHTTPStatus (err , http .StatusBadRequest )
144
144
}
145
145
defer file .Close ()
146
146
147
+ // Get the content of the file in form of a slice of bytes.
147
148
fileBytes , err := io .ReadAll (file )
148
149
if err != nil {
149
150
log .Printf ("error ocurred reading file: %v" , err )
150
151
return WithHTTPStatus (err , http .StatusBadRequest )
151
152
}
152
153
154
+ // Get the sub-type of the input file from the form.
153
155
targetFileSubType := r .FormValue ("input_format" )
154
156
157
+ // Call Detect fuction to get the mimetype of the input file.
155
158
detectedFileType := mimetype .Detect (fileBytes )
156
159
160
+ // Parse the mimetype to get the type and the sub-type of the input file.
157
161
fileType , subType , err := files .TypeAndSupType (detectedFileType .String ())
158
162
if err != nil {
159
163
log .Printf ("error occurred getting type and subtype from mimetype: %v" , err )
160
164
return WithHTTPStatus (err , http .StatusBadRequest )
161
165
}
162
166
167
+ // Get the right factory based off the input file type.
163
168
fileFactory , err := files .BuildFactory (fileType , fileHeader .Filename )
164
169
if err != nil {
165
170
log .Printf ("error occurred while getting a file factory: %v" , err )
166
171
return WithHTTPStatus (err , http .StatusBadRequest )
167
172
}
168
173
174
+ // Returns an object that implements the File interface based on the sub-type of the input file.
169
175
f , err := fileFactory .NewFile (subType )
170
176
if err != nil {
171
177
log .Printf ("error occurred getting the file object: %v" , err )
172
178
return WithHTTPStatus (err , http .StatusBadRequest )
173
179
}
174
180
181
+ // Return the kind of the output file.
175
182
targetFileType := files .SupportedFileTypes ()[targetFileSubType ]
183
+
184
+ // Convert the file to the target format.
185
+ // convertedFile is an io.Reader.
176
186
convertedFile , err = f .ConvertTo (
177
187
cases .Title (language .English ).String (targetFileType ),
178
188
targetFileSubType ,
179
189
bytes .NewReader (fileBytes ),
180
190
)
181
191
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 )
183
193
return WithHTTPStatus (err , http .StatusInternalServerError )
184
194
}
185
195
@@ -193,18 +203,20 @@ func handleUploadFile(w http.ResponseWriter, r *http.Request) error {
193
203
194
204
newFile , err := os .Create (convertedFilePath )
195
205
if err != nil {
196
- log .Printf ("error occurred converting file: %v" , err )
206
+ log .Printf ("error occurred while creating the output file: %v" , err )
197
207
return WithHTTPStatus (err , http .StatusInternalServerError )
198
208
}
199
209
defer newFile .Close ()
200
210
201
211
buf := new (bytes.Buffer )
202
212
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 )
203
215
}
204
216
205
217
convertedFileBytes := buf .Bytes ()
206
218
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 )
208
220
return WithHTTPStatus (err , http .StatusInternalServerError )
209
221
}
210
222
0 commit comments