Пример #1
0
// 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

}
Пример #2
0
// 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

}