github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/models/page.go (about) 1 package models 2 3 import ( 4 "errors" 5 "fmt" 6 "log" 7 "os" 8 "strconv" 9 "time" 10 "github.com/insionng/yougam/helper" 11 ) 12 13 type Page struct { 14 Id int64 15 Pid int64 `xorm:"index"` //小于等于0 代表页面本身是顶层页面, pid大于0 代表属于某节点id的下级 本系统围绕node设计,页面亦可以是节点的下级 16 Uid int64 `xorm:"index"` 17 Sort int64 18 Ctype int64 `xorm:"index"` 19 Title string `xorm:"index"` 20 Content string `xorm:"text"` 21 Attachment string `xorm:"text"` 22 Created int64 `xorm:"created index"` 23 Updated int64 `xorm:"updated"` 24 Hotness float64 `xorm:"index"` 25 Hotup int64 `xorm:"index"` 26 Hotdown int64 `xorm:"index"` 27 Hotscore int64 `xorm:"index"` //Hotup - Hotdown 28 Hotvote int64 `xorm:"index"` //Hotup + Hotdown 29 Views int64 30 Author string `xorm:"index"` //这里指本页面创建者 31 Template string `xorm:"index"` 32 } 33 34 func AddPage(title string, content string, attachment string, nid int64) (int64, error) { 35 page := &Page{Pid: nid, Title: title, Content: content, Attachment: attachment, Created: time.Now().Unix()} 36 37 if _, err := Engine.Insert(page); err == nil { 38 39 return page.Id, err 40 } else { 41 return -1, err 42 } 43 44 } 45 46 func SetPage(pageid int64, p *Page) (int64, error) { 47 p.Id = pageid 48 return Engine.Insert(p) 49 } 50 51 func GetPages(offset int, limit int, field string) (*[]*Page, error) { 52 pagez := new([]*Page) 53 err := Engine.Table("page").Limit(limit, offset).Desc(field).Find(pagez) 54 return pagez, err 55 } 56 57 func GetPagesByNodeCount(offset int, limit int, nodecount int64, field string) (*[]*Page, error) { 58 pagez := new([]*Page) 59 err := Engine.Table("page").Where("node_count > ?", nodecount).Limit(limit, offset).Desc(field).Find(pagez) 60 return pagez, err 61 62 } 63 64 func GetPage(id int64) (*Page, error) { 65 66 obj := &Page{} 67 has, err := Engine.Id(id).Get(obj) 68 69 if has { 70 return obj, err 71 } else { 72 73 return nil, err 74 } 75 } 76 77 78 func GetPageByTitle(title string) (*Page, error) { 79 obj := &Page{} 80 obj.Title = title 81 has, err := Engine.Get(obj) 82 if has { 83 return obj, err 84 } else { 85 86 return nil, err 87 } 88 } 89 90 func PutPage(cid int64, obj *Page) (int64, error) { 91 //覆盖式更新 92 row, err := Engine.Update(obj, &Page{Id: cid}) 93 return row, err 94 95 } 96 97 //map[string]interface{}{"ctype": ctype} 98 func UpdatePage(cid int64, objmap *map[string]interface{}) error { 99 obj := &Page{} 100 if row, err := Engine.Table(obj).Where("id=?", cid).Update(objmap); err != nil || row == 0 { 101 return errors.New(fmt.Sprint("UpdatePage row:", row, "出现错误:", err)) 102 } else { 103 return nil 104 } 105 106 } 107 108 func DelPage(id int64, uid int64, role int64) error { 109 allow := false 110 if role < 0 { 111 allow = true 112 } 113 114 page := &Page{} 115 116 if has, err := Engine.Id(id).Get(page); has == true && err == nil { 117 118 if page.Uid == uid || allow { 119 //检查附件字段并尝试删除文件 120 if len(page.Attachment) > 0 { 121 122 if p := helper.URL2local(page.Attachment); helper.Exist(p) { 123 //验证是否管理员权限 124 if allow { 125 if err := os.Remove(p); err != nil { 126 //可以输出错误日志,但不要反回错误,以免陷入死循环无法删掉 127 log.Println("ROOT DEL PAGE Attachment, PAGE ID:", id, ",ERR:", err) 128 } 129 } else { //检查用户对本地文件的所有权 130 if helper.VerifyUserfile(p, strconv.FormatInt(uid, 10)) { 131 if err := os.Remove(p); err != nil { 132 log.Println("DEL PAGE Attachment, PAGE ID:", id, ",ERR:", err) 133 } 134 } 135 } 136 137 } 138 } 139 140 //检查内容字段并尝试删除文件 141 if len(page.Content) > 0 { 142 //若内容中存在图片则开始尝试删除图片 143 delfiles_local := []string{} 144 145 if m, n := helper.GetImages(page.Content); n > 0 { 146 147 for _, v := range m { 148 if helper.IsLocal(v) { 149 delfiles_local = append(delfiles_local, v) 150 //如果本地同时也存在banner缓存文件,则加入旧图集合中,等待后面一次性删除 151 if p := helper.URL2local(helper.SetSuffix(v, "_banner.jpg")); helper.Exist(p) { 152 delfiles_local = append(delfiles_local, p) 153 } 154 if p := helper.URL2local(helper.SetSuffix(v, "_large.jpg")); helper.Exist(p) { 155 delfiles_local = append(delfiles_local, p) 156 } 157 if p := helper.URL2local(helper.SetSuffix(v, "_medium.jpg")); helper.Exist(p) { 158 delfiles_local = append(delfiles_local, p) 159 } 160 if p := helper.URL2local(helper.SetSuffix(v, "_small.jpg")); helper.Exist(p) { 161 delfiles_local = append(delfiles_local, p) 162 } 163 } 164 } 165 for k, v := range delfiles_local { 166 if p := helper.URL2local(v); helper.Exist(p) { //如若文件存在,则处理,否则忽略 167 //先行判断是否缩略图 如果不是则执行删除image表记录的操作 因为缩略图是没有存到image表记录里面的 168 //isThumbnails := bool(true) //false代表不是缩略图 true代表是缩略图 169 /* 170 if (!strings.HasSuffix(v, "_large.jpg")) && 171 (!strings.HasSuffix(v, "_medium.jpg")) && 172 (!strings.HasSuffix(v, "_small.jpg")) { 173 isThumbnails = false 174 175 } 176 */ 177 //验证是否管理员权限 178 if allow { 179 if err := os.Remove(p); err != nil { 180 log.Println("#", k, ",ROOT DEL FILE ERROR:", err) 181 } 182 183 } else { //检查用户对文件的所有权 184 if helper.VerifyUserfile(p, strconv.FormatInt(uid, 10)) { 185 if err := os.Remove(p); err != nil { 186 log.Println("#", k, ",DEL FILE ERROR:", err) 187 } 188 189 } 190 } 191 192 } 193 } 194 } 195 196 } 197 //不管实际路径中是否存在文件均删除该数据库记录,以免数据库记录陷入死循环无法删掉 198 if page.Id == id { 199 200 if row, err := Engine.Id(id).Delete(new(Page)); err != nil || row == 0 { 201 return fmt.Errorf("E:删除页面错误 %v !", err.Error()) 202 } 203 return nil 204 } 205 } 206 return errors.New("你无权删除此页面:" + strconv.FormatInt(id, 10)) 207 } 208 return errors.New("无法删除不存在的PAGE ID:" + strconv.FormatInt(id, 10)) 209 } 210 211 func GetPageCountByPid(pid int64) int64 { 212 n, _ := Engine.Where("pid=?", pid).Count(&Page{Pid: pid}) 213 return n 214 }