Attachments continue to function after the parent topic is moved now.
Changed the signature of AttachmentStore.Add Added the MoveTo method to AttachmentStore. Added the MoveToByExtra method to AttachmentStore. Don't forget to actually add the pre script for topic_c_edit_post x.x Added the extra column to the attachments table. You will need to run the updater / patcher for this commit.
This commit is contained in:
parent
6128325696
commit
cdb2f0711d
|
@ -277,7 +277,7 @@ func createTables(adapter qgen.Adapter) error {
|
|||
tblColumn{"originTable", "varchar", 200, false, false, "replies"},
|
||||
tblColumn{"uploadedBy", "int", 0, false, false, ""}, // TODO; Make this a foreign key
|
||||
tblColumn{"path", "varchar", 200, false, false, ""},
|
||||
//tblColumn{"extra", "varchar", 200, false, false, ""},
|
||||
tblColumn{"extra", "varchar", 200, false, false, ""},
|
||||
},
|
||||
[]tblKey{
|
||||
tblKey{"attachID", "primary"},
|
||||
|
|
|
@ -16,6 +16,7 @@ type MiniAttachment struct {
|
|||
OriginID int
|
||||
UploadedBy int
|
||||
Path string
|
||||
Extra string
|
||||
|
||||
Image bool
|
||||
Ext string
|
||||
|
@ -25,7 +26,9 @@ type AttachmentStore interface {
|
|||
Get(id int) (*MiniAttachment, error)
|
||||
MiniGetList(originTable string, originID int) (alist []*MiniAttachment, err error)
|
||||
BulkMiniGetList(originTable string, ids []int) (amap map[int][]*MiniAttachment, err error)
|
||||
Add(sectionID int, sectionTable string, originID int, originTable string, uploadedBy int, path string) (int, error)
|
||||
Add(sectionID int, sectionTable string, originID int, originTable string, uploadedBy int, path string, extra string) (int, error)
|
||||
MoveTo(sectionID int, originID int, originTable string) error
|
||||
MoveToByExtra(sectionID int, originTable string, extra string) error
|
||||
GlobalCount() int
|
||||
CountIn(originTable string, oid int) int
|
||||
CountInPath(path string) int
|
||||
|
@ -39,18 +42,21 @@ type DefaultAttachmentStore struct {
|
|||
count *sql.Stmt
|
||||
countIn *sql.Stmt
|
||||
countInPath *sql.Stmt
|
||||
move *sql.Stmt
|
||||
moveByExtra *sql.Stmt
|
||||
delete *sql.Stmt
|
||||
}
|
||||
|
||||
func NewDefaultAttachmentStore() (*DefaultAttachmentStore, error) {
|
||||
acc := qgen.NewAcc()
|
||||
func NewDefaultAttachmentStore(acc *qgen.Accumulator) (*DefaultAttachmentStore, error) {
|
||||
return &DefaultAttachmentStore{
|
||||
get: acc.Select("attachments").Columns("originID, sectionID, uploadedBy, path").Where("attachID = ?").Prepare(),
|
||||
getByObj: acc.Select("attachments").Columns("attachID, sectionID, uploadedBy, path").Where("originTable = ? AND originID = ?").Prepare(),
|
||||
add: acc.Insert("attachments").Columns("sectionID, sectionTable, originID, originTable, uploadedBy, path").Fields("?,?,?,?,?,?").Prepare(),
|
||||
get: acc.Select("attachments").Columns("originID, sectionID, uploadedBy, path, extra").Where("attachID = ?").Prepare(),
|
||||
getByObj: acc.Select("attachments").Columns("attachID, sectionID, uploadedBy, path, extra").Where("originTable = ? AND originID = ?").Prepare(),
|
||||
add: acc.Insert("attachments").Columns("sectionID, sectionTable, originID, originTable, uploadedBy, path, extra").Fields("?,?,?,?,?,?,?").Prepare(),
|
||||
count: acc.Count("attachments").Prepare(),
|
||||
countIn: acc.Count("attachments").Where("originTable = ? and originID = ?").Prepare(),
|
||||
countInPath: acc.Count("attachments").Where("path = ?").Prepare(),
|
||||
move: acc.Update("attachments").Set("sectionID = ?").Where("originID = ? AND originTable = ?").Prepare(),
|
||||
moveByExtra: acc.Update("attachments").Set("sectionID = ?").Where("originTable = ? AND extra = ?").Prepare(),
|
||||
delete: acc.Delete("attachments").Where("attachID = ?").Prepare(),
|
||||
}, acc.FirstError()
|
||||
}
|
||||
|
@ -60,7 +66,7 @@ func (store *DefaultAttachmentStore) MiniGetList(originTable string, originID in
|
|||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
attach := &MiniAttachment{OriginID: originID}
|
||||
err := rows.Scan(&attach.ID, &attach.SectionID, &attach.UploadedBy, &attach.Path)
|
||||
err := rows.Scan(&attach.ID, &attach.SectionID, &attach.UploadedBy, &attach.Path, &attach.Extra)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -114,7 +120,7 @@ func (store *DefaultAttachmentStore) BulkMiniGetList(originTable string, ids []i
|
|||
|
||||
func (store *DefaultAttachmentStore) Get(id int) (*MiniAttachment, error) {
|
||||
attach := &MiniAttachment{ID: id}
|
||||
err := store.get.QueryRow(id).Scan(&attach.OriginID, &attach.SectionID, &attach.UploadedBy, &attach.Path)
|
||||
err := store.get.QueryRow(id).Scan(&attach.OriginID, &attach.SectionID, &attach.UploadedBy, &attach.Path, &attach.Extra)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -127,8 +133,8 @@ func (store *DefaultAttachmentStore) Get(id int) (*MiniAttachment, error) {
|
|||
return attach, nil
|
||||
}
|
||||
|
||||
func (store *DefaultAttachmentStore) Add(sectionID int, sectionTable string, originID int, originTable string, uploadedBy int, path string) (int, error) {
|
||||
res, err := store.add.Exec(sectionID, sectionTable, originID, originTable, uploadedBy, path)
|
||||
func (store *DefaultAttachmentStore) Add(sectionID int, sectionTable string, originID int, originTable string, uploadedBy int, path string, extra string) (int, error) {
|
||||
res, err := store.add.Exec(sectionID, sectionTable, originID, originTable, uploadedBy, path, extra)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
@ -136,6 +142,16 @@ func (store *DefaultAttachmentStore) Add(sectionID int, sectionTable string, ori
|
|||
return int(lid), err
|
||||
}
|
||||
|
||||
func (store *DefaultAttachmentStore) MoveTo(sectionID int, originID int, originTable string) error {
|
||||
_, err := store.move.Exec(sectionID, originID, originTable)
|
||||
return err
|
||||
}
|
||||
|
||||
func (store *DefaultAttachmentStore) MoveToByExtra(sectionID int, originTable string, extra string) error {
|
||||
_, err := store.moveByExtra.Exec(sectionID, originTable, extra)
|
||||
return err
|
||||
}
|
||||
|
||||
func (store *DefaultAttachmentStore) GlobalCount() (count int) {
|
||||
err := store.count.QueryRow().Scan(&count)
|
||||
if err != nil {
|
||||
|
|
|
@ -265,6 +265,7 @@ func userCheck(w http.ResponseWriter, r *http.Request, user *User) (header *Head
|
|||
addPreScript("topics_topic")
|
||||
addPreScript("paginator")
|
||||
addPreScript("alert")
|
||||
addPreScript("topic_c_edit_post")
|
||||
|
||||
return header, nil
|
||||
}
|
||||
|
|
|
@ -251,8 +251,15 @@ func (topic *Topic) Unlock() (err error) {
|
|||
func (topic *Topic) MoveTo(destForum int) (err error) {
|
||||
_, err = topicStmts.moveTo.Exec(destForum, topic.ID)
|
||||
topic.cacheRemove()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = Attachments.MoveTo(destForum, topic.ID, "topics")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return Attachments.MoveToByExtra(destForum, "replies", strconv.Itoa(topic.ID))
|
||||
}
|
||||
|
||||
// TODO: We might want more consistent terminology rather than using stick in some places and pin in others. If you don't understand the difference, there is none, they are one and the same.
|
||||
func (topic *Topic) Stick() (err error) {
|
||||
|
|
2
main.go
2
main.go
|
@ -152,7 +152,7 @@ func afterDBInit() (err error) {
|
|||
if err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
common.Attachments, err = common.NewDefaultAttachmentStore()
|
||||
common.Attachments, err = common.NewDefaultAttachmentStore(acc)
|
||||
if err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package main
|
|||
|
||||
import (
|
||||
"bufio"
|
||||
"database/sql"
|
||||
"strconv"
|
||||
|
||||
"github.com/Azareal/Gosora/query_gen"
|
||||
|
@ -28,6 +29,7 @@ func init() {
|
|||
addPatch(14, patch14)
|
||||
addPatch(15, patch15)
|
||||
addPatch(16, patch16)
|
||||
addPatch(17, patch17)
|
||||
}
|
||||
|
||||
func patch0(scanner *bufio.Scanner) (err error) {
|
||||
|
@ -550,3 +552,39 @@ func patch16(scanner *bufio.Scanner) error {
|
|||
}, nil,
|
||||
))
|
||||
}
|
||||
|
||||
func patch17(scanner *bufio.Scanner) error {
|
||||
err := execStmt(qgen.Builder.AddColumn("attachments", tblColumn{"extra", "varchar", 200, false, false, ""}, nil))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = acc().Select("topics").Cols("tid, parentID").Where("attachCount > 0").Each(func(rows *sql.Rows) error {
|
||||
var tid, parentID int
|
||||
err := rows.Scan(&tid, &parentID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = acc().Update("attachments").Set("sectionID = ?").Where("originTable = 'topics' AND originID = ?").Exec(parentID, tid)
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return acc().Select("replies").Cols("rid, tid").Where("attachCount > 0").Each(func(rows *sql.Rows) error {
|
||||
var rid, tid, sectionID int
|
||||
err := rows.Scan(&rid, &tid)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = acc().Select("topics").Cols("parentID").Where("tid = ?").QueryRow(tid).Scan(§ionID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = acc().Update("attachments").Set("sectionID = ?, extra = ?").Where("originTable = 'replies' AND originID = ?").Exec(sectionID, tid, rid)
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
|
|
@ -32,7 +32,7 @@ func execStmt(stmt *sql.Stmt, err error) error {
|
|||
}*/
|
||||
|
||||
func eachUser(handle func(int) error) error {
|
||||
err := qgen.NewAcc().Select("users").Each(func(rows *sql.Rows) error {
|
||||
err := qgen.NewAcc().Select("users").Cols("uid").Each(func(rows *sql.Rows) error {
|
||||
var uid int
|
||||
err := rows.Scan(&uid)
|
||||
if err != nil {
|
||||
|
|
|
@ -117,7 +117,7 @@ func deleteAttachment(w http.ResponseWriter, r *http.Request, user common.User,
|
|||
// TODO: Stop duplicating this code
|
||||
// TODO: Use a transaction here
|
||||
// TODO: Move this function to neutral ground
|
||||
func uploadAttachment(w http.ResponseWriter, r *http.Request, user common.User, sid int, sectionTable string, oid int, originTable string) (pathMap map[string]string, rerr common.RouteError) {
|
||||
func uploadAttachment(w http.ResponseWriter, r *http.Request, user common.User, sid int, sectionTable string, oid int, originTable string, extra string) (pathMap map[string]string, rerr common.RouteError) {
|
||||
pathMap = make(map[string]string)
|
||||
files, rerr := uploadFilesWithHash(w, r, user, "./attachs/")
|
||||
if rerr != nil {
|
||||
|
@ -125,7 +125,7 @@ func uploadAttachment(w http.ResponseWriter, r *http.Request, user common.User,
|
|||
}
|
||||
|
||||
for _, filename := range files {
|
||||
aid, err := common.Attachments.Add(sid, sectionTable, oid, originTable, user.ID, filename)
|
||||
aid, err := common.Attachments.Add(sid, sectionTable, oid, originTable, user.ID, filename, extra)
|
||||
if err != nil {
|
||||
return nil, common.InternalError(err, w, r)
|
||||
}
|
||||
|
|
|
@ -79,7 +79,7 @@ func CreateReplySubmit(w http.ResponseWriter, r *http.Request, user common.User)
|
|||
// Handle the file attachments
|
||||
// TODO: Stop duplicating this code
|
||||
if user.Perms.UploadFiles {
|
||||
_, rerr := uploadAttachment(w, r, user, topic.ParentID, "forums", rid, "replies")
|
||||
_, rerr := uploadAttachment(w, r, user, topic.ParentID, "forums", rid, "replies", strconv.Itoa(topic.ID))
|
||||
if rerr != nil {
|
||||
return rerr
|
||||
}
|
||||
|
@ -380,7 +380,7 @@ func AddAttachToReplySubmit(w http.ResponseWriter, r *http.Request, user common.
|
|||
}
|
||||
|
||||
// Handle the file attachments
|
||||
pathMap, rerr := uploadAttachment(w, r, user, topic.ParentID, "forums", rid, "replies")
|
||||
pathMap, rerr := uploadAttachment(w, r, user, topic.ParentID, "forums", rid, "replies", strconv.Itoa(topic.ID))
|
||||
if rerr != nil {
|
||||
// TODO: This needs to be a JS error...
|
||||
return rerr
|
||||
|
|
|
@ -258,12 +258,18 @@ func ViewTopic(w http.ResponseWriter, r *http.Request, user common.User, header
|
|||
}
|
||||
|
||||
if user.Perms.EditReply && len(attachQueryList) > 0 {
|
||||
//log.Printf("attachQueryList: %+v\n", attachQueryList)
|
||||
amap, err := common.Attachments.BulkMiniGetList("replies", attachQueryList)
|
||||
if err != nil && err != sql.ErrNoRows {
|
||||
return common.InternalError(err, w, r)
|
||||
}
|
||||
//log.Printf("amap: %+v\n", amap)
|
||||
//log.Printf("attachMap: %+v\n", attachMap)
|
||||
for id, attach := range amap {
|
||||
tpage.ItemList[attachMap[id]].Attachments = attach
|
||||
/*for _, a := range attach {
|
||||
log.Printf("a: %+v\n", a)
|
||||
}*/
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -302,7 +308,7 @@ func AddAttachToTopicSubmit(w http.ResponseWriter, r *http.Request, user common.
|
|||
}
|
||||
|
||||
// Handle the file attachments
|
||||
pathMap, rerr := uploadAttachment(w, r, user, topic.ParentID, "forums", tid, "topics")
|
||||
pathMap, rerr := uploadAttachment(w, r, user, topic.ParentID, "forums", tid, "topics", "")
|
||||
if rerr != nil {
|
||||
// TODO: This needs to be a JS error...
|
||||
return rerr
|
||||
|
@ -529,7 +535,7 @@ func CreateTopicSubmit(w http.ResponseWriter, r *http.Request, user common.User)
|
|||
|
||||
// Handle the file attachments
|
||||
if user.Perms.UploadFiles {
|
||||
_, rerr := uploadAttachment(w, r, user, fid, "forums", tid, "topics")
|
||||
_, rerr := uploadAttachment(w, r, user, fid, "forums", tid, "topics", "")
|
||||
if rerr != nil {
|
||||
return rerr
|
||||
}
|
||||
|
|
|
@ -6,5 +6,6 @@ CREATE TABLE [attachments] (
|
|||
[originTable] nvarchar (200) DEFAULT 'replies' not null,
|
||||
[uploadedBy] int not null,
|
||||
[path] nvarchar (200) not null,
|
||||
[extra] nvarchar (200) not null,
|
||||
primary key([attachID])
|
||||
);
|
|
@ -6,5 +6,6 @@ CREATE TABLE `attachments` (
|
|||
`originTable` varchar(200) DEFAULT 'replies' not null,
|
||||
`uploadedBy` int not null,
|
||||
`path` varchar(200) not null,
|
||||
`extra` varchar(200) not null,
|
||||
primary key(`attachID`)
|
||||
) CHARSET=utf8mb4 COLLATE utf8mb4_general_ci;
|
|
@ -6,5 +6,6 @@ CREATE TABLE "attachments" (
|
|||
`originTable` varchar (200) DEFAULT 'replies' not null,
|
||||
`uploadedBy` int not null,
|
||||
`path` varchar (200) not null,
|
||||
`extra` varchar (200) not null,
|
||||
primary key(`attachID`)
|
||||
);
|
Loading…
Reference in New Issue