1
0
Fork 0
mirror of https://github.com/Eggbertx/gochan.git synced 2025-08-03 11:46:22 -07:00

Properly handle multiple gochan databases in MySQL/Postgres, use wrapper for "Get posts with this IP" JSON request

This commit is contained in:
Eggbertx 2024-09-20 23:13:44 -07:00
parent 52adfc0847
commit 605972bd86
2 changed files with 27 additions and 10 deletions

View file

@ -377,9 +377,9 @@ func doesTableExist(tableName string) (bool, error) {
switch config.GetSQLConfig().DBtype { switch config.GetSQLConfig().DBtype {
case "mysql": case "mysql":
fallthrough existQuery = `SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = ? AND TABLE_SCHEMA = DATABASE()`
case "postgres": case "postgres":
existQuery = `SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = ?` existQuery = `SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = ? AND TABLE_CATALOG = CURRENT_DATABASE()`
case "sqlite3": case "sqlite3":
existQuery = `SELECT COUNT(*) FROM sqlite_master WHERE type = 'table' AND name = ?` existQuery = `SELECT COUNT(*) FROM sqlite_master WHERE type = 'table' AND name = ?`
default: default:
@ -391,7 +391,7 @@ func doesTableExist(tableName string) (bool, error) {
if err != nil { if err != nil {
return false, err return false, err
} }
return count == 1, nil return count > 0, nil
} }
// getDatabaseVersion gets the version of the database, or an error if none or multiple exist // getDatabaseVersion gets the version of the database, or an error if none or multiple exist

View file

@ -603,8 +603,14 @@ func threadAttrsCallback(_ http.ResponseWriter, request *http.Request, _ *gcsql.
return attrBuffer.String(), nil return attrBuffer.String(), nil
} }
type postJSONWithIP struct {
// gcsql.Post.IP's struct tag hides the IP field, but we want to see it here
*gcsql.Post
IP string
}
type postInfoJSON struct { type postInfoJSON struct {
Post *gcsql.Post `json:"post"` Post *postJSONWithIP `json:"post"`
FQDN []string `json:"ipFQDN"` FQDN []string `json:"ipFQDN"`
OriginalFilename string `json:"originalFilename,omitempty"` OriginalFilename string `json:"originalFilename,omitempty"`
@ -612,22 +618,29 @@ type postInfoJSON struct {
Fingerprint string `json:"fingerprint,omitempty"` Fingerprint string `json:"fingerprint,omitempty"`
} }
func postInfoCallback(_ http.ResponseWriter, request *http.Request, _ *gcsql.Staff, _ bool, _ *zerolog.Event, _ *zerolog.Event) (output interface{}, err error) { func postInfoCallback(_ http.ResponseWriter, request *http.Request, _ *gcsql.Staff, _ bool, _ *zerolog.Event, errEv *zerolog.Event) (output interface{}, err error) {
postIDstr := request.FormValue("postid") postIDstr := request.FormValue("postid")
if postIDstr == "" { if postIDstr == "" {
return "", errors.New("invalid request (missing postid)") return "", errors.New("invalid request (missing postid)")
} }
var postID int var postID int
if postID, err = strconv.Atoi(postIDstr); err != nil { if postID, err = strconv.Atoi(postIDstr); err != nil {
errEv.Err(err).Caller().
Str("postID", postIDstr).Send()
return "", err return "", err
} }
post, err := gcsql.GetPostFromID(postID, true) post, err := gcsql.GetPostFromID(postID, true)
if err != nil { if err != nil {
errEv.Err(err).Caller().
Int("postID", postID).Send()
return "", err return "", err
} }
postInfo := postInfoJSON{ postInfo := postInfoJSON{
Post: &postJSONWithIP{
Post: post, Post: post,
IP: post.IP,
},
} }
names, err := net.LookupAddr(post.IP) names, err := net.LookupAddr(post.IP)
if err == nil { if err == nil {
@ -637,16 +650,20 @@ func postInfoCallback(_ http.ResponseWriter, request *http.Request, _ *gcsql.Sta
} }
upload, err := post.GetUpload() upload, err := post.GetUpload()
if err != nil { if err != nil {
errEv.Err(err).Caller().Msg("Unable to get upload")
return "", err return "", err
} }
if upload != nil { if upload != nil {
postInfo.OriginalFilename = upload.OriginalFilename postInfo.OriginalFilename = upload.OriginalFilename
postInfo.Checksum = upload.Checksum postInfo.Checksum = upload.Checksum
if postInfo.OriginalFilename != "deleted" {
postInfo.Fingerprint, err = uploads.GetPostImageFingerprint(postID) postInfo.Fingerprint, err = uploads.GetPostImageFingerprint(postID)
if err != nil { if err != nil {
errEv.Err(err).Caller().Msg("Unable to get image fingerprint")
return "", err return "", err
} }
} }
}
return postInfo, nil return postInfo, nil
} }