-
Notifications
You must be signed in to change notification settings - Fork 0
/
purgethread.go
153 lines (117 loc) · 2.65 KB
/
purgethread.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
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"
)
// PurgeThreadModel holds request input
type PurgeThreadModel struct {
ID uint
Name string
Ib uint
}
// IsValid will check struct validity
func (m *PurgeThreadModel) IsValid() bool {
if m.ID == 0 {
return false
}
if m.Name == "" {
return false
}
if m.Ib == 0 {
return false
}
return true
}
// Status will return info
func (m *PurgeThreadModel) Status() (err error) {
// Get Database handle
dbase, err := db.GetDb()
if err != nil {
return
}
// Check if favorite is already there
err = dbase.QueryRow("SELECT thread_title FROM threads WHERE thread_id = ? AND ib_id = ? LIMIT 1", m.ID, 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 *PurgeThreadModel) Delete() (err error) {
// check model validity
if !m.IsValid() {
return errors.New("PurgeThreadModel is not valid")
}
// Get Database handle
dbase, err := db.GetDb()
if err != nil {
return
}
images := []imageInfo{}
// Get thread images
rows, err := dbase.Query(`SELECT image_id,image_file,image_thumbnail FROM images
INNER JOIN posts on images.post_id = posts.post_id
INNER JOIN threads on threads.thread_id = posts.thread_id
WHERE threads.thread_id = ? AND ib_id = ?`, m.ID, m.Ib)
if err != nil {
return
}
defer rows.Close()
for rows.Next() {
image := imageInfo{}
err = rows.Scan(&image.ID, &image.File, &image.Thumb)
if err != nil {
return err
}
// Append rows to info struct
images = append(images, image)
}
err = rows.Err()
if err != nil {
return
}
// delete thread from database
ps1, err := dbase.Prepare("DELETE FROM threads WHERE thread_id= ? AND ib_id = ? LIMIT 1")
if err != nil {
return
}
defer ps1.Close()
_, err = ps1.Exec(m.ID, m.Ib)
if err != nil {
return
}
// delete image files
go func() {
for _, image := range images {
// 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
}