mirror of
https://github.com/Eggbertx/gochan.git
synced 2025-08-05 12:56:22 -07:00
Finish migrating posting/post.go
This commit is contained in:
parent
585c54c772
commit
f5337896b2
2 changed files with 68 additions and 31 deletions
|
@ -43,6 +43,9 @@ func GetThreadFiles(post *Post) ([]Upload, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Post) AttachFile(upload *Upload) error {
|
func (p *Post) AttachFile(upload *Upload) error {
|
||||||
|
if upload == nil {
|
||||||
|
return nil //
|
||||||
|
}
|
||||||
const query = `INSERT INTO DBPREFIXfiles (
|
const query = `INSERT INTO DBPREFIXfiles (
|
||||||
post_id, file_order, original_filename, filename, checksum, file_size,
|
post_id, file_order, original_filename, filename, checksum, file_size,
|
||||||
is_spoilered, thumbnail_width, thumbnail_height, width, height)
|
is_spoilered, thumbnail_width, thumbnail_height, width, height)
|
||||||
|
|
|
@ -233,6 +233,7 @@ func MakePost(writer http.ResponseWriter, request *http.Request) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var upload *gcsql.Upload
|
||||||
file, handler, err := request.FormFile("imagefile")
|
file, handler, err := request.FormFile("imagefile")
|
||||||
var filePath, thumbPath, catalogThumbPath string
|
var filePath, thumbPath, catalogThumbPath string
|
||||||
if err != nil || handler.Size == 0 {
|
if err != nil || handler.Size == 0 {
|
||||||
|
@ -246,16 +247,31 @@ func MakePost(writer http.ResponseWriter, request *http.Request) {
|
||||||
Str("referredFrom", request.Referer()).
|
Str("referredFrom", request.Referer()).
|
||||||
Send()
|
Send()
|
||||||
} else {
|
} else {
|
||||||
|
upload = &gcsql.Upload{
|
||||||
|
OriginalFilename: html.EscapeString(handler.Filename),
|
||||||
|
}
|
||||||
|
|
||||||
|
if checkFilenameBan(upload, &post, postBoard, writer, request) {
|
||||||
|
// if checkFilenameBan returns true, a ban page or error was displayed
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
data, err := io.ReadAll(file)
|
data, err := io.ReadAll(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
gcutil.LogError(err).
|
gcutil.LogError(err).
|
||||||
|
Str("IP", post.IP).
|
||||||
Str("upload", "read").Send()
|
Str("upload", "read").Send()
|
||||||
serverutil.ServeErrorPage(writer, "Error while trying to read file: "+err.Error())
|
serverutil.ServeErrorPage(writer, "Error while trying to read file: "+err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
var upload gcsql.Upload
|
|
||||||
upload.OriginalFilename = html.EscapeString(handler.Filename)
|
// Calculate image checksum
|
||||||
|
upload.Checksum = fmt.Sprintf("%x", md5.Sum(data))
|
||||||
|
if checkChecksumBan(upload, &post, postBoard, writer, request) {
|
||||||
|
// checkChecksumBan returns true, a ban page or error was displayed
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
ext := strings.ToLower(filepath.Ext(upload.OriginalFilename))
|
ext := strings.ToLower(filepath.Ext(upload.OriginalFilename))
|
||||||
upload.Filename = getNewFilename() + ext
|
upload.Filename = getNewFilename() + ext
|
||||||
|
@ -274,21 +290,20 @@ func MakePost(writer http.ResponseWriter, request *http.Request) {
|
||||||
gcutil.LogError(err).
|
gcutil.LogError(err).
|
||||||
Str("posting", "upload").
|
Str("posting", "upload").
|
||||||
Str("IP", post.IP).
|
Str("IP", post.IP).
|
||||||
Str("filename", post.Filename).Send()
|
Str("filename", upload.Filename).
|
||||||
serverutil.ServeErrorPage(writer, fmt.Sprintf("Couldn't write file %q", post.FilenameOriginal))
|
Str("originalFilename", upload.OriginalFilename).
|
||||||
|
Send()
|
||||||
|
serverutil.ServeErrorPage(writer, fmt.Sprintf("Couldn't write file %q", upload.OriginalFilename))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calculate image checksum
|
|
||||||
post.FileChecksum = fmt.Sprintf("%x", md5.Sum(data))
|
|
||||||
|
|
||||||
if ext == "webm" || ext == "mp4" {
|
if ext == "webm" || ext == "mp4" {
|
||||||
gcutil.LogInfo().
|
gcutil.LogInfo().
|
||||||
Str("post", "withVideo").
|
Str("post", "withVideo").
|
||||||
Str("IP", post.IP).
|
Str("IP", post.IP).
|
||||||
Str("filename", handler.Filename).
|
Str("filename", handler.Filename).
|
||||||
Str("referer", request.Referer()).Send()
|
Str("referer", request.Referer()).Send()
|
||||||
if post.ParentID == 0 {
|
if post.IsTopPost {
|
||||||
if err := createVideoThumbnail(filePath, thumbPath, boardConfig.ThumbWidth); err != nil {
|
if err := createVideoThumbnail(filePath, thumbPath, boardConfig.ThumbWidth); err != nil {
|
||||||
gcutil.LogError(err).
|
gcutil.LogError(err).
|
||||||
Str("filePath", filePath).
|
Str("filePath", filePath).
|
||||||
|
@ -337,18 +352,19 @@ func MakePost(writer http.ResponseWriter, request *http.Request) {
|
||||||
value, _ := strconv.Atoi(lineArr[1])
|
value, _ := strconv.Atoi(lineArr[1])
|
||||||
switch lineArr[0] {
|
switch lineArr[0] {
|
||||||
case "width":
|
case "width":
|
||||||
post.ImageW = value
|
upload.Width = value
|
||||||
case "height":
|
case "height":
|
||||||
post.ImageH = value
|
upload.Height = value
|
||||||
case "size":
|
case "size":
|
||||||
post.Filesize = value
|
upload.FileSize = value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
thumbType := "reply"
|
thumbType := "reply"
|
||||||
if post.IsTopPost {
|
if post.IsTopPost {
|
||||||
thumbType = "op"
|
thumbType = "op"
|
||||||
}
|
}
|
||||||
post.ThumbW, post.ThumbH = getThumbnailSize(post.ImageW, post.ImageH, boardDir, thumbType)
|
upload.ThumbnailWidth, upload.ThumbnailHeight = getThumbnailSize(
|
||||||
|
upload.Width, upload.Height, postBoard.Dir, thumbType)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Attempt to load uploaded file with imaging library
|
// Attempt to load uploaded file with imaging library
|
||||||
|
@ -368,16 +384,17 @@ func MakePost(writer http.ResponseWriter, request *http.Request) {
|
||||||
serverutil.ServeErrorPage(writer, "Couldn't get image filesize: "+err.Error())
|
serverutil.ServeErrorPage(writer, "Couldn't get image filesize: "+err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
post.Filesize = int(stat.Size())
|
upload.FileSize = int(stat.Size())
|
||||||
|
|
||||||
// Get image width and height, as well as thumbnail width and height
|
// Get image width and height, as well as thumbnail width and height
|
||||||
post.ImageW = img.Bounds().Max.X
|
upload.Width = img.Bounds().Max.X
|
||||||
post.ImageH = img.Bounds().Max.Y
|
upload.Height = img.Bounds().Max.Y
|
||||||
thumbType := "reply"
|
thumbType := "reply"
|
||||||
if post.ParentID == 0 {
|
if post.IsTopPost {
|
||||||
thumbType = "op"
|
thumbType = "op"
|
||||||
}
|
}
|
||||||
post.ThumbW, post.ThumbH = getThumbnailSize(post.ImageW, post.ImageH, boardDir, thumbType)
|
upload.ThumbnailWidth, upload.ThumbnailHeight = getThumbnailSize(
|
||||||
|
upload.Width, upload.Height, postBoard.Dir, thumbType)
|
||||||
|
|
||||||
gcutil.LogAccess(request).
|
gcutil.LogAccess(request).
|
||||||
Bool("withFile", true).
|
Bool("withFile", true).
|
||||||
|
@ -399,14 +416,15 @@ func MakePost(writer http.ResponseWriter, request *http.Request) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
shouldThumb := shouldCreateThumbnail(filePath, post.ImageW, post.ImageH, post.ThumbW, post.ThumbH)
|
shouldThumb := shouldCreateThumbnail(filePath,
|
||||||
|
upload.Width, upload.Height, upload.ThumbnailWidth, upload.ThumbnailHeight)
|
||||||
if shouldThumb {
|
if shouldThumb {
|
||||||
var thumbnail image.Image
|
var thumbnail image.Image
|
||||||
var catalogThumbnail image.Image
|
var catalogThumbnail image.Image
|
||||||
if post.ParentID == 0 {
|
if post.IsTopPost {
|
||||||
// If this is a new thread, generate thumbnail and catalog thumbnail
|
// If this is a new thread, generate thumbnail and catalog thumbnail
|
||||||
thumbnail = createImageThumbnail(img, boardDir, "op")
|
thumbnail = createImageThumbnail(img, postBoard.Dir, "op")
|
||||||
catalogThumbnail = createImageThumbnail(img, boardDir, "catalog")
|
catalogThumbnail = createImageThumbnail(img, postBoard.Dir, "catalog")
|
||||||
if err = imaging.Save(catalogThumbnail, catalogThumbPath); err != nil {
|
if err = imaging.Save(catalogThumbnail, catalogThumbPath); err != nil {
|
||||||
gcutil.LogError(err).
|
gcutil.LogError(err).
|
||||||
Str("thumbPath", catalogThumbPath).
|
Str("thumbPath", catalogThumbPath).
|
||||||
|
@ -416,7 +434,7 @@ func MakePost(writer http.ResponseWriter, request *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
thumbnail = createImageThumbnail(img, boardDir, "reply")
|
thumbnail = createImageThumbnail(img, postBoard.Dir, "reply")
|
||||||
}
|
}
|
||||||
if err = imaging.Save(thumbnail, thumbPath); err != nil {
|
if err = imaging.Save(thumbnail, thumbPath); err != nil {
|
||||||
gcutil.LogError(err).
|
gcutil.LogError(err).
|
||||||
|
@ -428,8 +446,8 @@ func MakePost(writer http.ResponseWriter, request *http.Request) {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// If image fits in thumbnail size, symlink thumbnail to original
|
// If image fits in thumbnail size, symlink thumbnail to original
|
||||||
post.ThumbW = img.Bounds().Max.X
|
upload.ThumbnailWidth = img.Bounds().Max.X
|
||||||
post.ThumbH = img.Bounds().Max.Y
|
upload.ThumbnailHeight = img.Bounds().Max.Y
|
||||||
if err := syscall.Symlink(filePath, thumbPath); err != nil {
|
if err := syscall.Symlink(filePath, thumbPath); err != nil {
|
||||||
gcutil.LogError(err).
|
gcutil.LogError(err).
|
||||||
Str("thumbPath", thumbPath).
|
Str("thumbPath", thumbPath).
|
||||||
|
@ -438,9 +456,9 @@ func MakePost(writer http.ResponseWriter, request *http.Request) {
|
||||||
serverutil.ServeErrorPage(writer, "Couldn't create thumbnail: "+err.Error())
|
serverutil.ServeErrorPage(writer, "Couldn't create thumbnail: "+err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if post.ParentID == 0 {
|
if post.IsTopPost {
|
||||||
// Generate catalog thumbnail
|
// Generate catalog thumbnail
|
||||||
catalogThumbnail := createImageThumbnail(img, boardDir, "catalog")
|
catalogThumbnail := createImageThumbnail(img, postBoard.Dir, "catalog")
|
||||||
if err = imaging.Save(catalogThumbnail, catalogThumbPath); err != nil {
|
if err = imaging.Save(catalogThumbnail, catalogThumbPath); err != nil {
|
||||||
gcutil.LogError(err).
|
gcutil.LogError(err).
|
||||||
Str("thumbPath", catalogThumbPath).
|
Str("thumbPath", catalogThumbPath).
|
||||||
|
@ -454,14 +472,29 @@ func MakePost(writer http.ResponseWriter, request *http.Request) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = gcsql.InsertPost(&post, emailCommand != "sage"); err != nil {
|
if err = post.Insert(emailCommand != "sage", postBoard.ID, false, false, false, false); err != nil {
|
||||||
gcutil.LogError(err).
|
gcutil.LogError(err).
|
||||||
Str("sql", "postInsertion").Send()
|
Str("IP", post.IP).
|
||||||
if post.Filename != "" {
|
Str("sql", "postInsertion").
|
||||||
|
Msg("Unable to insert post")
|
||||||
|
if upload != nil {
|
||||||
os.Remove(filePath)
|
os.Remove(filePath)
|
||||||
os.Remove(thumbPath)
|
os.Remove(thumbPath)
|
||||||
os.Remove(catalogThumbPath)
|
os.Remove(catalogThumbPath)
|
||||||
}
|
}
|
||||||
|
serverutil.ServeErrorPage(writer, "Unable to insert post: "+err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = post.AttachFile(upload); err != nil {
|
||||||
|
gcutil.LogError(err).
|
||||||
|
Str("IP", post.IP).
|
||||||
|
Str("sql", "postInsertion").
|
||||||
|
Msg("Unable to attach upload to post")
|
||||||
|
os.Remove(filePath)
|
||||||
|
os.Remove(thumbPath)
|
||||||
|
os.Remove(catalogThumbPath)
|
||||||
|
serverutil.ServeErrorPage(writer, "Unable to attach upload: "+err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -470,10 +503,11 @@ func MakePost(writer http.ResponseWriter, request *http.Request) {
|
||||||
building.BuildFrontPage()
|
building.BuildFrontPage()
|
||||||
|
|
||||||
if emailCommand == "noko" {
|
if emailCommand == "noko" {
|
||||||
if post.ParentID < 1 {
|
if post.IsTopPost {
|
||||||
http.Redirect(writer, request, systemCritical.WebRoot+postBoard.Dir+"/res/"+strconv.Itoa(post.ID)+".html", http.StatusFound)
|
http.Redirect(writer, request, systemCritical.WebRoot+postBoard.Dir+"/res/"+strconv.Itoa(post.ID)+".html", http.StatusFound)
|
||||||
} else {
|
} else {
|
||||||
http.Redirect(writer, request, systemCritical.WebRoot+postBoard.Dir+"/res/"+strconv.Itoa(post.ParentID)+".html#"+strconv.Itoa(post.ID), http.StatusFound)
|
topPost, _ := post.TopPostID()
|
||||||
|
http.Redirect(writer, request, systemCritical.WebRoot+postBoard.Dir+"/res/"+strconv.Itoa(topPost)+".html#"+strconv.Itoa(post.ID), http.StatusFound)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
http.Redirect(writer, request, systemCritical.WebRoot+postBoard.Dir+"/", http.StatusFound)
|
http.Redirect(writer, request, systemCritical.WebRoot+postBoard.Dir+"/", http.StatusFound)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue