1
0
Fork 0
mirror of https://github.com/Eggbertx/gochan.git synced 2025-08-02 10:56:25 -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 {
case "mysql":
fallthrough
existQuery = `SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = ? AND TABLE_SCHEMA = DATABASE()`
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":
existQuery = `SELECT COUNT(*) FROM sqlite_master WHERE type = 'table' AND name = ?`
default:
@ -391,7 +391,7 @@ func doesTableExist(tableName string) (bool, error) {
if err != nil {
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

View file

@ -603,31 +603,44 @@ func threadAttrsCallback(_ http.ResponseWriter, request *http.Request, _ *gcsql.
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 {
Post *gcsql.Post `json:"post"`
FQDN []string `json:"ipFQDN"`
Post *postJSONWithIP `json:"post"`
FQDN []string `json:"ipFQDN"`
OriginalFilename string `json:"originalFilename,omitempty"`
Checksum string `json:"checksum,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")
if postIDstr == "" {
return "", errors.New("invalid request (missing postid)")
}
var postID int
if postID, err = strconv.Atoi(postIDstr); err != nil {
errEv.Err(err).Caller().
Str("postID", postIDstr).Send()
return "", err
}
post, err := gcsql.GetPostFromID(postID, true)
if err != nil {
errEv.Err(err).Caller().
Int("postID", postID).Send()
return "", err
}
postInfo := postInfoJSON{
Post: post,
Post: &postJSONWithIP{
Post: post,
IP: post.IP,
},
}
names, err := net.LookupAddr(post.IP)
if err == nil {
@ -637,14 +650,18 @@ func postInfoCallback(_ http.ResponseWriter, request *http.Request, _ *gcsql.Sta
}
upload, err := post.GetUpload()
if err != nil {
errEv.Err(err).Caller().Msg("Unable to get upload")
return "", err
}
if upload != nil {
postInfo.OriginalFilename = upload.OriginalFilename
postInfo.Checksum = upload.Checksum
postInfo.Fingerprint, err = uploads.GetPostImageFingerprint(postID)
if err != nil {
return "", err
if postInfo.OriginalFilename != "deleted" {
postInfo.Fingerprint, err = uploads.GetPostImageFingerprint(postID)
if err != nil {
errEv.Err(err).Caller().Msg("Unable to get image fingerprint")
return "", err
}
}
}
return postInfo, nil