2018-11-20 04:07:36 +00:00
|
|
|
package routes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"net/http"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2019-04-19 06:36:26 +00:00
|
|
|
c "github.com/Azareal/Gosora/common"
|
2018-11-20 04:07:36 +00:00
|
|
|
)
|
|
|
|
|
2020-01-23 06:17:50 +00:00
|
|
|
var maxAgeYear = "max-age=" + strconv.Itoa(int(c.Year))
|
|
|
|
|
2020-06-08 02:18:17 +00:00
|
|
|
func ShowAttachment(w http.ResponseWriter, r *http.Request, u *c.User, filename string) c.RouteError {
|
2019-10-01 21:06:22 +00:00
|
|
|
sid, err := strconv.Atoi(r.FormValue("sid"))
|
2018-11-20 04:07:36 +00:00
|
|
|
if err != nil {
|
2020-06-08 02:18:17 +00:00
|
|
|
return c.LocalError("The sid is not an integer", w, r, u)
|
2018-11-20 04:07:36 +00:00
|
|
|
}
|
2019-07-25 07:00:49 +00:00
|
|
|
sectionTable := r.FormValue("stype")
|
2018-11-20 04:07:36 +00:00
|
|
|
|
2021-03-24 11:45:18 +00:00
|
|
|
filename = c.Stripslashes(filename)
|
|
|
|
if filename == "" {
|
|
|
|
return c.LocalError("Bad filename", w, r, u)
|
|
|
|
}
|
|
|
|
ext := filepath.Ext(filename)
|
|
|
|
if ext == "" || !c.AllowedFileExts.Contains(strings.TrimPrefix(ext, ".")) {
|
|
|
|
return c.LocalError("Bad extension", w, r, u)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Use the same hook table as upstream
|
|
|
|
hTbl := c.GetHookTable()
|
|
|
|
skip, rerr := c.H_route_attach_start_hook(hTbl, w, r, u, filename)
|
|
|
|
if skip || rerr != nil {
|
|
|
|
return rerr
|
|
|
|
}
|
|
|
|
|
|
|
|
a, err := c.Attachments.GetForRenderRoute(filename, sid, sectionTable)
|
|
|
|
// ErrCorruptAttachPath is a possibility now
|
2018-11-20 04:07:36 +00:00
|
|
|
if err == sql.ErrNoRows {
|
2019-04-19 06:36:26 +00:00
|
|
|
return c.NotFound(w, r, nil)
|
2018-11-20 04:07:36 +00:00
|
|
|
} else if err != nil {
|
2019-04-19 06:36:26 +00:00
|
|
|
return c.InternalError(err, w, r)
|
2018-11-20 04:07:36 +00:00
|
|
|
}
|
|
|
|
|
2021-03-24 11:45:18 +00:00
|
|
|
skip, rerr = c.H_route_attach_post_get_hook(hTbl, w, r, u, a)
|
|
|
|
if skip || rerr != nil {
|
|
|
|
return rerr
|
|
|
|
}
|
|
|
|
|
|
|
|
if a.SectionTable == "forums" {
|
2020-06-08 02:18:17 +00:00
|
|
|
_, ferr := c.SimpleForumUserCheck(w, r, u, sid)
|
2018-11-20 04:07:36 +00:00
|
|
|
if ferr != nil {
|
|
|
|
return ferr
|
|
|
|
}
|
2020-06-08 02:18:17 +00:00
|
|
|
if !u.Perms.ViewTopic {
|
|
|
|
return c.NoPermissions(w, r, u)
|
2018-11-20 04:07:36 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-06-08 02:18:17 +00:00
|
|
|
return c.LocalError("Unknown section", w, r, u)
|
2018-11-20 04:07:36 +00:00
|
|
|
}
|
2020-01-23 06:17:50 +00:00
|
|
|
|
2021-03-24 11:45:18 +00:00
|
|
|
if a.OriginTable != "topics" && a.OriginTable != "replies" {
|
2020-06-08 02:18:17 +00:00
|
|
|
return c.LocalError("Unknown origin", w, r, u)
|
2018-11-20 04:07:36 +00:00
|
|
|
}
|
|
|
|
|
2020-06-08 02:18:17 +00:00
|
|
|
if !u.Loggedin {
|
2020-01-23 06:17:50 +00:00
|
|
|
w.Header().Set("Cache-Control", maxAgeYear)
|
2018-11-20 04:07:36 +00:00
|
|
|
} else {
|
2019-04-19 06:36:26 +00:00
|
|
|
guest := c.GuestUser
|
2019-10-01 21:06:22 +00:00
|
|
|
_, ferr := c.SimpleForumUserCheck(w, r, &guest, sid)
|
2018-11-20 04:07:36 +00:00
|
|
|
if ferr != nil {
|
|
|
|
return ferr
|
|
|
|
}
|
2019-11-04 11:55:52 +00:00
|
|
|
h := w.Header()
|
2018-11-20 04:07:36 +00:00
|
|
|
if guest.Perms.ViewTopic {
|
2020-01-23 06:17:50 +00:00
|
|
|
h.Set("Cache-Control", maxAgeYear)
|
2018-11-20 04:07:36 +00:00
|
|
|
} else {
|
2019-11-04 11:55:52 +00:00
|
|
|
h.Set("Cache-Control", "private")
|
2018-11-20 04:07:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Fix the problem where non-existent files aren't greeted with custom 404s on ServeFile()'s side
|
|
|
|
http.ServeFile(w, r, "./attachs/"+filename)
|
|
|
|
return nil
|
|
|
|
}
|
2018-12-28 02:08:35 +00:00
|
|
|
|
2020-03-21 07:44:33 +00:00
|
|
|
func deleteAttachment(w http.ResponseWriter, r *http.Request, u *c.User, aid int, js bool) c.RouteError {
|
2021-03-24 11:45:18 +00:00
|
|
|
e := c.DeleteAttachment(aid)
|
|
|
|
if e == sql.ErrNoRows {
|
2019-04-19 06:36:26 +00:00
|
|
|
return c.NotFoundJSQ(w, r, nil, js)
|
2021-03-24 11:45:18 +00:00
|
|
|
} else if e != nil {
|
|
|
|
return c.InternalErrorJSQ(e, w, r, js)
|
2018-12-28 02:08:35 +00:00
|
|
|
}
|
2020-02-18 22:43:55 +00:00
|
|
|
return nil
|
2018-12-28 02:08:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Stop duplicating this code
|
|
|
|
// TODO: Use a transaction here
|
|
|
|
// TODO: Move this function to neutral ground
|
2020-03-21 07:44:33 +00:00
|
|
|
func uploadAttachment(w http.ResponseWriter, r *http.Request, u *c.User, sid int, stable string, oid int, otable, extra string) (pathMap map[string]string, rerr c.RouteError) {
|
2018-12-28 02:08:35 +00:00
|
|
|
pathMap = make(map[string]string)
|
2020-03-21 07:44:33 +00:00
|
|
|
files, rerr := uploadFilesWithHash(w, r, u, "./attachs/")
|
2018-12-28 02:08:35 +00:00
|
|
|
if rerr != nil {
|
|
|
|
return nil, rerr
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, filename := range files {
|
2020-03-21 07:44:33 +00:00
|
|
|
aid, err := c.Attachments.Add(sid, stable, oid, otable, u.ID, filename, extra)
|
2018-12-28 02:08:35 +00:00
|
|
|
if err != nil {
|
2019-04-19 06:36:26 +00:00
|
|
|
return nil, c.InternalError(err, w, r)
|
2018-12-28 02:08:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, ok := pathMap[filename]
|
|
|
|
if ok {
|
|
|
|
pathMap[filename] += "," + strconv.Itoa(aid)
|
|
|
|
} else {
|
|
|
|
pathMap[filename] = strconv.Itoa(aid)
|
|
|
|
}
|
|
|
|
|
2020-02-20 04:32:49 +00:00
|
|
|
err = c.Attachments.AddLinked(otable, oid)
|
2020-02-19 10:32:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, c.InternalError(err, w, r)
|
2018-12-28 02:08:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pathMap, nil
|
|
|
|
}
|