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
|
|
|
|
|
|
|
|
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)
|
|
|
|
MiniTopicGet(id int) (alist []*MiniAttachment, err error)
|
|
|
|
Add(sectionID int, sectionTable string, originID int, originTable string, uploadedBy int, path string) (int, error)
|
|
|
|
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
|
|
|
|
getByTopic *sql.Stmt
|
|
|
|
add *sql.Stmt
|
|
|
|
count *sql.Stmt
|
|
|
|
countIn *sql.Stmt
|
|
|
|
countInPath *sql.Stmt
|
|
|
|
delete *sql.Stmt
|
2018-01-22 08:15:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewDefaultAttachmentStore() (*DefaultAttachmentStore, error) {
|
2018-08-04 11:46:36 +00:00
|
|
|
acc := qgen.NewAcc()
|
2018-01-22 08:15:45 +00:00
|
|
|
return &DefaultAttachmentStore{
|
2018-12-27 09:12:30 +00:00
|
|
|
get: acc.Select("attachments").Columns("originID, sectionID, uploadedBy, path").Where("attachID = ?").Prepare(),
|
|
|
|
getByTopic: acc.Select("attachments").Columns("attachID, sectionID, uploadedBy, path").Where("originTable = 'topics' AND originID = ?").Prepare(),
|
|
|
|
add: acc.Insert("attachments").Columns("sectionID, sectionTable, originID, originTable, uploadedBy, path").Fields("?,?,?,?,?,?").Prepare(),
|
|
|
|
count: acc.Count("attachments").Prepare(),
|
|
|
|
countIn: acc.Count("attachments").Where("originTable = ? and originID = ?").Prepare(),
|
|
|
|
countInPath: acc.Count("attachments").Where("path = ?").Prepare(),
|
|
|
|
delete: acc.Delete("attachments").Where("attachID = ?").Prepare(),
|
2018-01-22 08:15:45 +00:00
|
|
|
}, acc.FirstError()
|
|
|
|
}
|
|
|
|
|
2018-12-27 05:42:41 +00:00
|
|
|
// TODO: Make this more generic so we can use it for reply attachments too
|
|
|
|
func (store *DefaultAttachmentStore) MiniTopicGet(id int) (alist []*MiniAttachment, err error) {
|
|
|
|
rows, err := store.getByTopic.Query(id)
|
|
|
|
defer rows.Close()
|
|
|
|
for rows.Next() {
|
|
|
|
attach := &MiniAttachment{OriginID: id}
|
|
|
|
err := rows.Scan(&attach.ID, &attach.SectionID, &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)
|
|
|
|
alist = append(alist, attach)
|
|
|
|
}
|
|
|
|
return alist, rows.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
lid, err := res.LastInsertId()
|
|
|
|
return int(lid), err
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|