From 44899a0fd7375e6002e71189e0e62d6610131be5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Omar=20Vergara=20P=C3=A9rez?= Date: Mon, 15 Jan 2024 21:27:08 -0600 Subject: [PATCH] feat(main): add file previews for documents and images based on the mimetype of the result file --- main.go | 23 +++++++++++++++++++---- templates/partials/active_modal.tmpl | 4 ++++ templates/partials/card_file.tmpl | 3 ++- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index 2fd5bf9..66fc310 100644 --- a/main.go +++ b/main.go @@ -45,6 +45,7 @@ func init() { type ConvertedFile struct { Filename string + FileType string } func index(w http.ResponseWriter, _ *http.Request) { @@ -150,19 +151,32 @@ func handleUploadFile(w http.ResponseWriter, r *http.Request) { return } - files := []string{ + tmpls := []string{ "templates/partials/card_file.tmpl", "templates/partials/modal.tmpl", } - tmpl, err := template.ParseFS(templatesHTML, files...) + tmpl, err := template.ParseFS(templatesHTML, tmpls...) if err != nil { log.Printf("error occurred parsing template files: %v", err) renderError(w, "INTERNAL_ERROR", http.StatusInternalServerError) return } - err = tmpl.ExecuteTemplate(w, "content", ConvertedFile{Filename: convertedFileName}) + convertedFileMimeType := mimetype.Detect(convertedFile) + + convertedFileType, _, err := files.TypeAndSupType(convertedFileMimeType.String()) + if err != nil { + log.Printf("error occurred getting the file type of the result file: %v", err) + renderError(w, "INTERNAL_ERROR", http.StatusInternalServerError) + return + } + + err = tmpl.ExecuteTemplate( + w, + "content", + ConvertedFile{Filename: convertedFileName, FileType: convertedFileType}, + ) if err != nil { log.Printf("error occurred executing template files: %v", err) renderError(w, "INTERNAL_ERROR", http.StatusInternalServerError) @@ -223,6 +237,7 @@ func handleFileFormat(w http.ResponseWriter, r *http.Request) { func handleModal(w http.ResponseWriter, r *http.Request) { filename := r.URL.Query().Get("filename") + filetype := r.URL.Query().Get("filetype") files := []string{ "templates/partials/active_modal.tmpl", @@ -235,7 +250,7 @@ func handleModal(w http.ResponseWriter, r *http.Request) { return } - if err = tmpl.ExecuteTemplate(w, "content", ConvertedFile{Filename: filename}); err != nil { + if err = tmpl.ExecuteTemplate(w, "content", ConvertedFile{Filename: filename, FileType: filetype}); err != nil { log.Printf("error occurred executing template files: %v", err) renderError(w, "INTERNAL_ERROR", http.StatusInternalServerError) return diff --git a/templates/partials/active_modal.tmpl b/templates/partials/active_modal.tmpl index 0e16346..dfbf9ed 100644 --- a/templates/partials/active_modal.tmpl +++ b/templates/partials/active_modal.tmpl @@ -6,7 +6,11 @@