/
purgepost.go
154 lines (118 loc) · 2.59 KB
/
purgepost.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package models
import (
"database/sql"
"errors"
"fmt"
"os"
"path/filepath"
"github.com/eirka/eirka-libs/amazon"
"github.com/eirka/eirka-libs/db"
e "github.com/eirka/eirka-libs/errors"
local "github.com/eirka/eirka-admin/config"
)
// PurgePostModel holds request input
type PurgePostModel struct {
Thread uint
ID uint
Ib uint
Name string
}
// IsValid will check struct validity
func (m *PurgePostModel) IsValid() bool {
if m.Thread == 0 {
return false
}
if m.ID == 0 {
return false
}
if m.ID == 0 {
return false
}
if m.Name == "" {
return false
}
return true
}
type imageInfo struct {
ID uint
File string
Thumb string
}
// Status will return info
func (m *PurgePostModel) Status() (err error) {
// Get Database handle
dbase, err := db.GetDb()
if err != nil {
return
}
// get thread ib and title
err = dbase.QueryRow("SELECT thread_title FROM threads WHERE thread_id = ? AND ib_id = ? LIMIT 1", m.Thread, m.Ib).Scan(&m.Name)
if err == sql.ErrNoRows {
return e.ErrNotFound
} else if err != nil {
return
}
return
}
// Delete will remove the entry
func (m *PurgePostModel) Delete() (err error) {
// check model validity
if !m.IsValid() {
return errors.New("PurgePostModel is not valid")
}
// Get transaction handle
tx, err := db.GetTransaction()
if err != nil {
return
}
defer tx.Rollback()
image := imageInfo{}
img := true
// check if post has an image
err = tx.QueryRow(`SELECT image_id,image_file,image_thumbnail FROM posts
INNER JOIN images on posts.post_id = images.post_id
WHERE posts.thread_id = ? AND posts.post_num = ? LIMIT 1`, m.Thread, m.ID).Scan(&image.ID, &image.File, &image.Thumb)
if err == sql.ErrNoRows {
img = false
} else if err != nil {
return
}
// delete thread from database
ps1, err := tx.Prepare("DELETE FROM posts WHERE thread_id= ? AND post_num = ? LIMIT 1")
if err != nil {
return
}
defer ps1.Close()
_, err = ps1.Exec(m.Thread, m.ID)
if err != nil {
return
}
// Commit transaction
err = tx.Commit()
if err != nil {
return
}
// delete image file
if img {
// filename must exist to prevent deleting the directory ;D
if image.Thumb == "" {
return
}
if image.File == "" {
return
}
// delete from amazon s3
s3 := amazon.New()
s3.Delete(fmt.Sprintf("src/%s", image.File))
if err != nil {
return
}
s3.Delete(fmt.Sprintf("thumb/%s", image.Thumb))
if err != nil {
return
}
os.RemoveAll(filepath.Join(local.Settings.Directories.ImageDir, image.File))
os.RemoveAll(filepath.Join(local.Settings.Directories.ThumbnailDir, image.Thumb))
}
return
}