|
|
@@ -993,6 +993,40 @@ func insertAvatar(db *sql.DB, user int, img []byte) error { |
|
|
|
return nil |
|
|
|
} |
|
|
|
|
|
|
|
func fetchLetterhead(db *sql.DB, user int) ( []byte, error ) { |
|
|
|
var img []byte |
|
|
|
var query string |
|
|
|
var err error |
|
|
|
|
|
|
|
query = `SELECT |
|
|
|
letterhead |
|
|
|
FROM user WHERE user.id = ? |
|
|
|
` |
|
|
|
row := db.QueryRow(query, user) |
|
|
|
err = row.Scan(&img) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
return img, err |
|
|
|
} |
|
|
|
|
|
|
|
return img, nil |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func insertLetterhead(db *sql.DB, user int, img []byte) error { |
|
|
|
query := `UPDATE user |
|
|
|
SET letterhead = ? |
|
|
|
WHERE id = ? |
|
|
|
` |
|
|
|
_, err := db.Exec(query, img, user) |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
|
|
|
|
return nil |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func setAvatar(w http.ResponseWriter, db *sql.DB, r *http.Request) { |
|
|
|
var validTypes []string = []string{"image/png", "image/jpeg"} |
|
|
|
var isValidType bool |
|
|
@@ -1020,6 +1054,33 @@ func getAvatar(w http.ResponseWriter, db *sql.DB, r *http.Request) { |
|
|
|
w.Write(img) |
|
|
|
} |
|
|
|
|
|
|
|
func setLetterhead(w http.ResponseWriter, db *sql.DB, r *http.Request) { |
|
|
|
var validTypes []string = []string{"image/png", "image/jpeg"} |
|
|
|
var isValidType bool |
|
|
|
|
|
|
|
claims, err := getClaims(r) |
|
|
|
if err != nil { http.Error(w, "Invalid token.", 422); return } |
|
|
|
img, err := io.ReadAll(r.Body) |
|
|
|
if err != nil { http.Error(w, "Invalid file.", 422); return } |
|
|
|
for _, v := range validTypes { |
|
|
|
if v == http.DetectContentType(img) { isValidType = true } |
|
|
|
} |
|
|
|
if !isValidType { http.Error(w, "Invalid file type.", 422); return } |
|
|
|
|
|
|
|
err = insertAvatar(db, claims.Id, img) |
|
|
|
if err != nil { http.Error(w, "Could not insert.", 500); return } |
|
|
|
} |
|
|
|
|
|
|
|
func getLetterhead(w http.ResponseWriter, db *sql.DB, r *http.Request) { |
|
|
|
claims, err := getClaims(r) |
|
|
|
if err != nil { http.Error(w, "Invalid token.", 422); return } |
|
|
|
img, err := fetchLetterhead(db, claims.Id) |
|
|
|
if err != nil { http.Error(w, "Could not retrieve.", 500); return } |
|
|
|
|
|
|
|
w.Header().Set("Content-Type", http.DetectContentType(img)) |
|
|
|
w.Write(img) |
|
|
|
} |
|
|
|
|
|
|
|
func queryBorrower(db *sql.DB, id int) ( Borrower, error ) { |
|
|
|
var borrower Borrower |
|
|
|
var query string |
|
|
@@ -1671,6 +1732,14 @@ func api(w http.ResponseWriter, r *http.Request) { |
|
|
|
r.Method == http.MethodPost && |
|
|
|
guard(r, 1): |
|
|
|
setAvatar(w, db, r) |
|
|
|
case match(p, "/api/user/letterhead", &args) && |
|
|
|
r.Method == http.MethodGet && |
|
|
|
guard(r, 1): |
|
|
|
getLetterhead(w, db, r) |
|
|
|
case match(p, "/api/user/letterhead", &args) && |
|
|
|
r.Method == http.MethodPost && |
|
|
|
guard(r, 1): |
|
|
|
setLetterhead(w, db, r) |
|
|
|
case match(p, "/api/fees", &args) && |
|
|
|
r.Method == http.MethodGet && |
|
|
|
guard(r, 1): |
|
|
|