Refactor common code to download invoice and expenses attachments

This commit is contained in:
jordi fita mas 2024-08-12 00:07:30 +02:00
parent 4deb698265
commit 58cef8c00b
3 changed files with 29 additions and 34 deletions

25
pkg/attachment.go Normal file
View File

@ -0,0 +1,25 @@
package pkg
import (
"github.com/julienschmidt/httprouter"
"net/http"
"strconv"
)
func serveAttachment(w http.ResponseWriter, r *http.Request, params httprouter.Params, sql string) {
slug := params[0].Value
if !ValidUuid(slug) {
http.NotFound(w, r)
return
}
conn := getConn(r)
var contentType string
var content []byte
if notFoundErrorOrPanic(conn.QueryRow(r.Context(), sql, slug).Scan(&contentType, &content)) {
http.NotFound(w, r)
return
}
w.Header().Set("Content-Type", contentType)
w.Header().Set("Content-Length", strconv.FormatInt(int64(len(content)), 10))
_, _ = w.Write(content)
}

View File

@ -635,28 +635,13 @@ func HandleUpdateExpenseTags(w http.ResponseWriter, r *http.Request, params http
} }
func ServeExpenseAttachment(w http.ResponseWriter, r *http.Request, params httprouter.Params) { func ServeExpenseAttachment(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
slug := params[0].Value serveAttachment(w, r, params, `
if !ValidUuid(slug) {
http.NotFound(w, r)
return
}
conn := getConn(r)
var contentType string
var content []byte
if notFoundErrorOrPanic(conn.QueryRow(r.Context(), `
select mime_type select mime_type
, content , content
from expense from expense
join expense_attachment using (expense_id) join expense_attachment using (expense_id)
where slug = $1 where slug = $1
`, slug).Scan(&contentType, &content)) { `)
http.NotFound(w, r)
return
}
w.Header().Set("Content-Type", contentType)
w.Header().Set("Content-Length", strconv.FormatInt(int64(len(content)), 10))
w.WriteHeader(http.StatusOK)
w.Write(content)
} }
func HandleEditExpenseAction(w http.ResponseWriter, r *http.Request, params httprouter.Params) { func HandleEditExpenseAction(w http.ResponseWriter, r *http.Request, params httprouter.Params) {

View File

@ -1514,26 +1514,11 @@ func HandleUpdateInvoiceTags(w http.ResponseWriter, r *http.Request, params http
} }
func ServeInvoiceAttachment(w http.ResponseWriter, r *http.Request, params httprouter.Params) { func ServeInvoiceAttachment(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
slug := params[0].Value serveAttachment(w, r, params, `
if !ValidUuid(slug) {
http.NotFound(w, r)
return
}
conn := getConn(r)
var contentType string
var content []byte
if notFoundErrorOrPanic(conn.QueryRow(r.Context(), `
select mime_type select mime_type
, content , content
from invoice from invoice
join invoice_attachment using (invoice_id) join invoice_attachment using (invoice_id)
where slug = $1 where slug = $1
`, slug).Scan(&contentType, &content)) { `)
http.NotFound(w, r)
return
}
w.Header().Set("Content-Type", contentType)
w.Header().Set("Content-Length", strconv.FormatInt(int64(len(content)), 10))
w.WriteHeader(http.StatusOK)
w.Write(content)
} }