Skip to content

Commit

Permalink
feat(main): add file previews for documents and images based on the m…
Browse files Browse the repository at this point in the history
…imetype of the result file
  • Loading branch information
danvergara committed Jan 16, 2024
1 parent 6cdf961 commit 44899a0
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
23 changes: 19 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func init() {

type ConvertedFile struct {
Filename string
FileType string
}

func index(w http.ResponseWriter, _ *http.Request) {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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",
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions templates/partials/active_modal.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
<h5 class="modal-title">{{ .Filename }}</h5>
</div>
<div class="modal-body">
{{if eq .FileType "image"}}
<img src="/files/{{ .Filename }}" class="img-fluid" alt="Responsive image">
{{else if eq .FileType "application"}}
<embed src="/files/{{ .Filename }}" width="500" height="375">
{{end}}
</div>
<div class="modal-footer">
<a href="/files/{{ .Filename }}" download>
Expand Down
3 changes: 2 additions & 1 deletion templates/partials/card_file.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@
<div class="card text-center">
<div class="card-body">
<input type="hidden" name="filename" value="{{ .Filename }}" id="filename"></input>
<input type="hidden" name="filetype" value="{{ .FileType }}" id="filetype"></input>
<button hx-get="/modal"
hx-target="#modals-here"
hx-trigger="click"
hx-include="[name='filename']"
hx-include="[name='filename'],[name='filetype']"
hx-params="*"
data-bs-toggle="modal"
data-bs-target="#modals-here"
Expand Down

0 comments on commit 44899a0

Please sign in to comment.