2018-01-22 08:15:45 +00:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2018-12-27 05:42:41 +00:00
|
|
|
"errors"
|
|
|
|
"strings"
|
2018-01-22 08:15:45 +00:00
|
|
|
|
2018-10-27 03:21:02 +00:00
|
|
|
"github.com/Azareal/Gosora/query_gen"
|
2018-01-22 08:15:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var Attachments AttachmentStore
|
|
|
|
|
2018-12-27 05:42:41 +00:00
|
|
|
type MiniAttachment struct {
|
|
|
|
ID int
|
|
|
|
SectionID int
|
|
|
|
OriginID int
|
|
|
|
UploadedBy int
|
|
|
|
Path string
|
2019-04-13 11:54:22 +00:00
|
|
|
Extra string
|
2018-12-27 05:42:41 +00:00
|
|
|
|
|
|
|
Image bool
|
|
|
|
Ext string
|
|
|
|
}
|
|
|
|
|
2018-01-22 08:15:45 +00:00
|
|
|
type AttachmentStore interface {
|
2018-12-27 05:42:41 +00:00
|
|
|
Get(id int) (*MiniAttachment, error)
|
2018-12-31 09:03:49 +00:00
|
|
|
MiniGetList(originTable string, originID int) (alist []*MiniAttachment, err error)
|
|
|
|
BulkMiniGetList(originTable string, ids []int) (amap map[int][]*MiniAttachment, err error)
|
2019-04-13 11:54:22 +00:00
|
|
|
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
|
2018-12-27 05:42:41 +00:00
|
|
|
GlobalCount() int
|
2018-12-27 09:12:30 +00:00
|
|
|
CountIn(originTable string, oid int) int
|
2018-12-27 05:42:41 +00:00
|
|
|
CountInPath(path string) int
|
|
|
|
Delete(aid int) error
|
2018-01-22 08:15:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type DefaultAttachmentStore struct {
|
2018-12-27 09:12:30 +00:00
|
|
|
get *sql.Stmt
|
2018-12-31 09:03:49 +00:00
|
|
|
getByObj *sql.Stmt
|
2018-12-27 09:12:30 +00:00
|
|
|
add *sql.Stmt
|
|
|
|
count *sql.Stmt
|
|
|
|
countIn *sql.Stmt
|
|
|
|
countInPath *sql.Stmt
|
2019-04-13 11:54:22 +00:00
|
|
|
move *sql.Stmt
|
|
|
|
moveByExtra *sql.Stmt
|
2018-12-27 09:12:30 +00:00
|
|
|
delete *sql.Stmt
|
2018-01-22 08:15:45 +00:00
|
|
|
}
|
|
|
|
|
2019-04-13 11:54:22 +00:00
|
|
|
func NewDefaultAttachmentStore(acc *qgen.Accumulator) (*DefaultAttachmentStore, error) {
|
2018-01-22 08:15:45 +00:00
|
|
|
return &DefaultAttachmentStore{
|
2019-04-13 11:54:22 +00:00
|
|
|
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(),
|
2018-12-27 09:12:30 +00:00
|
|
|
count: acc.Count("attachments").Prepare(),
|
|
|
|
countIn: acc.Count("attachments").Where("originTable = ? and originID = ?").Prepare(),
|
|
|
|
countInPath: acc.Count("attachments").Where("path = ?").Prepare(),
|
2019-04-13 11:54:22 +00:00
|
|
|
move: acc.Update("attachments").Set("sectionID = ?").Where("originID = ? AND originTable = ?").Prepare(),
|
|
|
|
moveByExtra: acc.Update("attachments").Set("sectionID = ?").Where("originTable = ? AND extra = ?").Prepare(),
|
2018-12-27 09:12:30 +00:00
|
|
|
delete: acc.Delete("attachments").Where("attachID = ?").Prepare(),
|
2018-01-22 08:15:45 +00:00
|
|
|
}, acc.FirstError()
|
|
|
|
}
|
|
|
|
|
2018-12-31 09:03:49 +00:00
|
|
|
func (store *DefaultAttachmentStore) MiniGetList(originTable string, originID int) (alist []*MiniAttachment, err error) {
|
|
|
|
rows, err := store.getByObj.Query(originTable, originID)
|
2018-12-27 05:42:41 +00:00
|
|
|
defer rows.Close()
|
|
|
|
for rows.Next() {
|
2018-12-31 09:03:49 +00:00
|
|
|
attach := &MiniAttachment{OriginID: originID}
|
2019-04-13 11:54:22 +00:00
|
|
|
err := rows.Scan(&attach.ID, &attach.SectionID, &attach.UploadedBy, &attach.Path, &attach.Extra)
|
2018-12-27 05:42:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
extarr := strings.Split(attach.Path, ".")
|
|
|
|
if len(extarr) < 2 {
|
|
|
|
return nil, errors.New("corrupt attachment path")
|
|
|
|
}
|
|
|
|
attach.Ext = extarr[len(extarr)-1]
|
|
|
|
attach.Image = ImageFileExts.Contains(attach.Ext)
|
|
|
|
alist = append(alist, attach)
|
|
|
|
}
|
|
|
|
return alist, rows.Err()
|
|
|
|
}
|
|
|
|
|
2018-12-31 09:03:49 +00:00
|
|
|
func (store *DefaultAttachmentStore) BulkMiniGetList(originTable string, ids []int) (amap map[int][]*MiniAttachment, err error) {
|
|
|
|
if len(ids) == 0 {
|
|
|
|
return nil, sql.ErrNoRows
|
|
|
|
}
|
|
|
|
if len(ids) == 1 {
|
|
|
|
res, err := store.MiniGetList(originTable, ids[0])
|
2019-04-10 07:40:47 +00:00
|
|
|
return map[int][]*MiniAttachment{ids[0]: res}, err
|
2018-12-31 09:03:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
amap = make(map[int][]*MiniAttachment)
|
|
|
|
var buffer []*MiniAttachment
|
|
|
|
var currentID int
|
|
|
|
rows, err := qgen.NewAcc().Select("attachments").Columns("attachID, sectionID, originID, uploadedBy, path").Where("originTable = ?").In("originID", ids).Orderby("originID ASC").Query(originTable)
|
|
|
|
defer rows.Close()
|
|
|
|
for rows.Next() {
|
|
|
|
attach := &MiniAttachment{}
|
|
|
|
err := rows.Scan(&attach.ID, &attach.SectionID, &attach.OriginID, &attach.UploadedBy, &attach.Path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
extarr := strings.Split(attach.Path, ".")
|
|
|
|
if len(extarr) < 2 {
|
|
|
|
return nil, errors.New("corrupt attachment path")
|
|
|
|
}
|
|
|
|
attach.Ext = extarr[len(extarr)-1]
|
|
|
|
attach.Image = ImageFileExts.Contains(attach.Ext)
|
|
|
|
if attach.ID != currentID {
|
|
|
|
if len(buffer) > 0 {
|
|
|
|
amap[currentID] = buffer
|
|
|
|
buffer = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
buffer = append(buffer, attach)
|
|
|
|
}
|
|
|
|
return amap, rows.Err()
|
|
|
|
}
|
|
|
|
|
2018-12-27 05:42:41 +00:00
|
|
|
func (store *DefaultAttachmentStore) Get(id int) (*MiniAttachment, error) {
|
|
|
|
attach := &MiniAttachment{ID: id}
|
2019-04-13 11:54:22 +00:00
|
|
|
err := store.get.QueryRow(id).Scan(&attach.OriginID, &attach.SectionID, &attach.UploadedBy, &attach.Path, &attach.Extra)
|
2018-12-27 05:42:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
extarr := strings.Split(attach.Path, ".")
|
|
|
|
if len(extarr) < 2 {
|
|
|
|
return nil, errors.New("corrupt attachment path")
|
|
|
|
}
|
|
|
|
attach.Ext = extarr[len(extarr)-1]
|
|
|
|
attach.Image = ImageFileExts.Contains(attach.Ext)
|
|
|
|
return attach, nil
|
|
|
|
}
|
|
|
|
|
2019-04-13 11:54:22 +00:00
|
|
|
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)
|
2018-12-27 05:42:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
lid, err := res.LastInsertId()
|
|
|
|
return int(lid), err
|
|
|
|
}
|
|
|
|
|
2019-04-13 11:54:22 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-12-27 05:42:41 +00:00
|
|
|
func (store *DefaultAttachmentStore) GlobalCount() (count int) {
|
|
|
|
err := store.count.QueryRow().Scan(&count)
|
|
|
|
if err != nil {
|
|
|
|
LogError(err)
|
|
|
|
}
|
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
2018-12-27 09:12:30 +00:00
|
|
|
func (store *DefaultAttachmentStore) CountIn(originTable string, oid int) (count int) {
|
|
|
|
err := store.countIn.QueryRow(originTable, oid).Scan(&count)
|
2018-12-27 05:42:41 +00:00
|
|
|
if err != nil {
|
|
|
|
LogError(err)
|
|
|
|
}
|
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
|
|
|
func (store *DefaultAttachmentStore) CountInPath(path string) (count int) {
|
|
|
|
err := store.countInPath.QueryRow(path).Scan(&count)
|
|
|
|
if err != nil {
|
|
|
|
LogError(err)
|
|
|
|
}
|
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
|
|
|
func (store *DefaultAttachmentStore) Delete(aid int) error {
|
|
|
|
_, err := store.delete.Exec(aid)
|
2018-01-22 08:15:45 +00:00
|
|
|
return err
|
|
|
|
}
|